Skip to content
Permalink
Browse files

Add __geo_interface__ to QgsFeature and QgsGeometry

  • Loading branch information
NathanW2 committed Sep 23, 2013
1 parent 770feee commit c55c2ab82fb61cc131dce5f13a7ea07180c24123
Showing with 21 additions and 0 deletions.
  1. +21 −0 python/__init__.py
@@ -24,6 +24,7 @@
__revision__ = '$Format:%H$'

import sip
from qgis.core import QgsFeature, QgsGeometry

try:
apis = ["QDate", "QDateTime", "QString", "QTextStream", "QTime", "QUrl", "QVariant"]
@@ -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.
You can’t perform that action at this time.