geo_interfaces with Python geospatial modules
Python
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
PostGIS_geointerface.py
PyQGIS_geointerface.py
PyShp_geointerface.py
PyShp_schema.py
Pyshp_mapping.py
QGIS_add_geo_interface.py
README.md
mapnik_geointerface.py
ogr_geointerface.py
ogr_gml_to_geojson.py
pyshp_geojson.py
spatialite_geointerface.py

README.md

Python Geo_interface applications

The geo_interface (GeoJSON-like) protocol was proposed by Sean Gillies and can be used in Python with:

Applications

One big advantage of the protocol is its ability to quickly examine the contents of a shapefile as dictionaries:

with Fiona:

    >>> import fiona   
    >>> f = fiona.open('point.shp')  
    >>> f.next()  
    {'geometry': {'type': 'Point', 'coordinates': (161821.09375, 79076.0703125)}, 'id': '0', 'properties': {u'DIP_DIR': 120, u'STRATI_TYP': 1, u'DIP': 30}}
    >>> f.next()['geometry']['coordinates']  
    (161485.09375, 79272.34375)  
    >>> f.next()['properties']['DIP']  
    55  

###with PyShp:

    def records(filename):  
        # generator 
        reader = shapefile.Reader(filename)  
        fields = reader.fields[1:]  
        field_names = [field[0] for field in fields]  
        for sr in reader.shapeRecords():  
            geom = sr.shape.__geo_interface__  
            atr = dict(zip(field_names, sr.record))  
            yield dict(geometry=geom,properties=atr)    
        

    >>> import shapefile
    >>> a = records('point.shp')
    >>> a.next()
    {'geometry': {'type': 'Point', 'coordinates': (161821.09375, 79076.0703125)}, 'properties': {'DIP_DIR': 120, 'STRATI_TYP': 1, 'DIP': 30}}
    >>> a.next()['geometry']['coordinates']
    (161485.09375, 79272.34375)
    >>> a.next()['properties']['DIP']
    55
    

###with osgeo.ogr

    def records(file):  
        # generator 
        reader = ogr.Open(file)
        layer = reader.GetLayer(0)
        for i in range(layer.GetFeatureCount()):
            feature = layer.GetFeature(i)
            yield json.loads(feature.ExportToJson())
            
    >>> from osgeo import ogr
    >>> a = records('point.shp')
    >>> a.next()
    {'geometry': {'type': 'Point', 'coordinates': (161821.09375, 79076.0703125)}, 'properties': {'DIP_DIR': 120, 'STRATI_TYP': 1, 'DIP': 30}}

###with PyQGIS API2:

    layer = qgis.utils.iface.activeLayer()  
    def records(layer):  
        fields = layer.pendingFields()   
        field_names = [field.name() for field in fields]   
        for elem in layer.getFeatures():  
              geom= elem.geometry()  
              atr = dict(zip(field_names, elem.attributes()))  
              yield dict(geometry=geom.exportToGeoJSON(),properties=atr)  
              
    c = records(layer) 
    c.next() 
    {'geometry': {'type': 'Point', 'coordinates': (161821.09375, 79076.0703125)}, 'id': '0', 'properties': {u'DIP_DIR': 120, u'STRATI_TYP': 1, u'DIP': 30}}

###conversion to shapely or pygeoif geometry

with shapely (Sean Gillies, as with pygeoif of Christian Lederman):

    >>> from shapely.geometry import shape    
    >>> a = records('point.shp') 
    >>> print shape( a.next()['geometry'])
    POINT (161821.09375 79076.0703125)