python Django Tastypie Geojson Serializer

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Django Tastypie Geojson Serializer相关的知识,希望对你有一定的参考价值。

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)    
        

以上是关于python Django Tastypie Geojson Serializer的主要内容,如果未能解决你的问题,请参考以下文章

Django RESTful API - django-piston 与 django-tastypie

Django-Tastypie 过滤所有字段

如何通知应用程序凭据在 Django/Tastypie/REST 中成功

Django Tastypie,运行一个动作

包含过滤条件的 Django-tastypie REST url

Django & TastyPie:request.POST 为空