Geo interface and Python 2.6

Hey, somebody discovered __geo_interface__. If you'd rather use Python's standard json module than geojson, you'll need to implement a custom encoder as documented:

>>> import json
>>> class GeoEncoder(json.JSONEncoder):
...     def default(self, obj):
...         if hasattr(obj, '__geo_interface__'):
...             return obj.__geo_interface__
...         return json.JSONEncoder.default(self, obj)
...
>>> class GeoThing(object):
...     __geo_interface__ = {'type': 'Point', 'coordinates': [0.0, 0.0]}
...
>>> json.dumps(GeoThing(), cls=GeoEncoder)
'{"type": "Point", "coordinates": [0.0, 0.0]}'

The geojson module does this for you, and more, but clearly needs to work with json from 2.6 as well as simplejson.

Comments

Re: Geo interface and Python 2.6

Author: Kurt Schwhr

:)

Thanks for the tip on how to use the standard json.

-kurt