Skip to content

Commit

Permalink
Add __geo_interface__ to QgsFeature and QgsGeometry
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanW2 committed Sep 23, 2013
1 parent 770feee commit c55c2ab
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions python/__init__.py
Expand Up @@ -24,6 +24,7 @@
__revision__ = '$Format:%H$'

import sip
from qgis.core import QgsFeature, QgsGeometry

try:
apis = ["QDate", "QDateTime", "QString", "QTextStream", "QTime", "QUrl", "QVariant"]
Expand Down Expand Up @@ -64,3 +65,23 @@ def __ne__(self, other):
core.NULL = QPyNullVariant( int )
except ImportError:
pass


def mapping_feature(feature):
geom = feature.geometry()
properties = {}
fields = [field.name() for field in feature.fields()]
properties = dict(zip(fields, feature.attributes()))
return {
'type' : 'Feature',
'properties' : properties,
'geometry' : geom.__geo_interface__}

def mapping_geometry(geometry):
geo = geometry.exportToGeoJSON()
# We have to use eval because exportToGeoJSON() gives us
# back a string that looks like a dictionary.
return eval(geo)

QgsFeature.__geo_interface__ = property(mapping_feature)
QgsGeometry.__geo_interface__ = property(mapping_geometry)

0 comments on commit c55c2ab

Please sign in to comment.