Last active
August 29, 2015 14:07
-
-
Save samtux/29c8ee3bf36e12420348 to your computer and use it in GitHub Desktop.
Django Tastypie Geojson Serializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GeoJsonSerializer(Serializer): | |
def to_geojson(self, data, options=None): | |
""" | |
Given some Python data, produces GeoJSON output. | |
""" | |
def _build_feature(obj): | |
f = { | |
"type": "Feature", | |
"properties": {}, | |
"geometry": {} | |
} | |
def recurse(key, value): | |
if key == 'id': | |
f[key] = value | |
return | |
if key in ['coordinates', 'type']: | |
f['geometry'][key] = value | |
if type(value)==type({}): | |
for k in value: | |
recurse(k, value[k]) | |
else: | |
f['properties'][key] = value | |
for key, value in obj.iteritems(): | |
recurse(key, value) | |
return f | |
def _build_feature_collection(objs, meta): | |
fc = { | |
"type": "FeatureCollection", | |
"features": [] | |
} | |
if(meta): | |
fc["meta"] = meta | |
for obj in objs: | |
fc['features'].append(_build_feature(obj)) | |
return fc | |
options = options or {} | |
data = self.to_simple(data, options) | |
meta = data.get('meta') | |
if 'objects' in data: | |
data = _build_feature_collection(data['objects'], meta) | |
else: | |
data = _build_feature(data) | |
return json.json.dumps(data, cls=json.DjangoJSONEncoder, sort_keys=True, ensure_ascii=False) | |
def to_json(self, data, options=None): | |
""" | |
Override to enable GeoJSON generation when the geojson option is passed. | |
""" | |
options = options or {} | |
if options.get('geojson'): | |
return self.to_geojson(data, options) | |
else: | |
return super(GeoJsonSerializer, self).to_json(data, options) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment