Skip to content

Latest commit

 

History

History
111 lines (84 loc) · 4.46 KB

How to install Fiona to read Shapefile attributes with OSGeo4W?.md

File metadata and controls

111 lines (84 loc) · 4.46 KB

From How to install Fiona to read Shapefile attributes with OSGeo4W?

  1. Why would you want install Fiona if you can use PyQGIS to read the shapefile attributes ?
  2. Fiona is for reading geometries and attributes of a shapefile file (as PyShp), therefore you don't need dbfpy (look at Python Script examples for geoprocessing shapefiles without using arcpy).
  3. Fiona is a Python module so you must install it as any Python module in the site-packages folder of your Python installation as SaultDon says.
  4. but as Fiona needs to compile C++ code (from GDAL/OGR) to install, you cannot use pip or easy-install on Windows (no compiler).You can try to install the "ready to use" version of Christoph Gohlke but it is linked to his version of GDAL)
  5. this procedure works with the standalone version of QGIS.

###Examples of reading attributes of a shapefile:

With Fiona:

    import fiona
    features = fiona.open("strati.shp")
    features.schema
    {'geometry': 'Point', 'properties': OrderedDict([(u'PENDAGE', 'int:2'), (u'DIRECTION', 'int:3'), (u'TYPE', 'str:10')])}
    for feat in features:
         print feat['properties']

    OrderedDict([(u'PENDAGE', 30), (u'DIRECTION', 120), (u'TYPE', u'incl')])
    OrderedDict([(u'PENDAGE', 45), (u'DIRECTION', 145), (u'TYPE', u'incl')])
    OrderedDict([(u'PENDAGE', 78), (u'DIRECTION', 148), (u'TYPE', u'incl')])
    

With PyShp:

    import shapefile
    reader = shapefile.Reader("strati.shp") 
    fields = reader.fields[1:]
    print fields
    [['PENDAGE', 'N', 2, 0], ['DIRECTION', 'N', 3, 0], ['TYPE', 'C', 10, 0]]
    field_names = [field[0] for field in fields]
    schema = dict((d[0],d[1:]) for d in reader.fields[1:])
    print schema
    {'DIRECTION': ['N', 3, 0], 'PENDAGE': ['N', 2, 0], 'TYPE': ['C', 10, 0]}
    for feat in reader.shapeRecords():
        print dict(zip(field_names, sr.record))  

    {'DIRECTION': 148, 'PENDAGE': 78, 'TYPE': 'incl'}
    {'DIRECTION': 148, 'PENDAGE': 78, 'TYPE': 'incl'}
    {'DIRECTION': 148, 'PENDAGE': 78, 'TYPE': 'incl'}

with PyQGIS in the Python console:

    layer = qgis.utils.iface.activeLayer()  
    fields = layer.pendingFields()  
    field_names = [field.name() for field in fields]  
    field_types = [field.typeName() for field in fields]
    field_precision = [field.precision() for field in fields]
    print field_precision
    [0,0,0]
    print dict(zip(field_names,field_types))
    {u'DIRECTION': u'Integer', u'PENDAGE': u'Integer', u'TYPE': u'String'}
    schema = dict((field_names[i], {field_types[i]:field_precision[i] if  field_precision[i] > 0 else field_types[i]:'' }) for i in range(len(field_names)))
    print schema
    {u'DIRECTION': {u'Integer': ''}, u'PENDAGE': {u'Integer': ''}, u'TYPE': {u'String': ''}}
    for feat in layer.getFeatures():  
        print dict(zip(field_names, feat.attributes()))
 
    {u'DIRECTION': 120, u'PENDAGE': 30, u'TYPE': u'incl'}
    {u'DIRECTION': 145, u'PENDAGE': 45, u'TYPE': u'incl'}
    {u'DIRECTION': 148, u'PENDAGE': 78, u'TYPE': u'incl'} 

With dbfpy:

    from dbfpy import dbf
    db = dbf.Dbf("strati.dbf")
    print db.fieldNames
    ['PENDAGE', 'DIRECTION', 'TYPE']
    print db.fieldDefs
    [PENDAGE    N   2   0, DIRECTION  N   3   0, TYPE       C  10   0]  
    schema = dict(zip(db.fieldNames,db.fieldDefs))
    print schema
    {'DIRECTION': DIRECTION  N   3   0, 'PENDAGE': PENDAGE    N   2   0, 'TYPE': TYPE       C  10   0} 
    for feat in db:
        print dict(zip(db.fieldNames, feat.asList()))  

    {'DIRECTION': 120, 'PENDAGE': 30, 'TYPE': 'incl'}
    {'DIRECTION': 145, 'PENDAGE': 45, 'TYPE': 'incl'}
    {'DIRECTION': 148, 'PENDAGE': 78, 'TYPE': 'incl'}

So you don't need dbfpy or other dbf Python module (PyPI:dbf)