2

I am in the process of building a website on geodjango. On the front end I have a map on openlayers and I want to be able to fire ajax queries at some django url and get back geojson.

The problem is that the standard django json serializer doesn't do geojson. So I have a model like:

class Asset (models.Model):
objects = models.GeoManager()

url_name = models.CharField(max_length=200)
name = models.CharField(max_length=200)
point = models.PointField(srid=4326)

def __unicode__(self):
    return self.name

And I want to create a view:

def geojson_query(request):

#geographic query

...

assets = Asset.objects.all().filter(point__contained=bb_4326)
json = serializers.serialize("json", assets)
return HttpResponse(json)

But then I get back a response that looks more like this:

"url_name": "Upper_Svaneti", 
"name": "Upper Svaneti", 
"point": "POINT (43.0113899999999987 42.9163899999999998)"

Whereas I want something that looks like this:

"url_name": "Upper_Svaneti", 
"name": "Upper Svaneti", 
"geometry": {
"type": "Point", 
"coordinates": [43.0113899999999987 42.9163899999999998]
}

So I guess my question is, what is the best way to implement a geojson serializer in django without totally rolling my own? And if it comes to rolling my own, where do I start?

prauchfuss
  • 1,842
  • 2
  • 17
  • 19

2 Answers2

5

You need to write your own serializer. Just inherit from the DjangoJSONEncoder, here's one I created that supports the Point type:

from django.core.serializers.json import DjangoJSONEncoder
from django.contrib.gis.geos import Point

class GeoJSONEncoder(DjangoJSONEncoder):
    def default(self, obj):
        if isinstance(obj, Point):
            return obj.coords
        return super(GeoJSONEncoder, self).default(obj)

You can then use it like so:

from my_app.serializers import GeoJSONEncoder
from django.utils import simplejson

json = simplejson.dumps(data, cls=GeoJSONEncoder)
DaveJ
  • 2,150
  • 4
  • 26
  • 31
1

So... I have done something slightly unpretty. I hardcoded the non-geojson parts of the serializer and used the json function from GEOS to get the geojson part.

So the method in the model looks like:

def get_footprint_json(self):
    geojson=self.footprint.json
    json='{"type": "Feature","geometry": %s,"properties": {"name":"%s","url_name":"%s"}}'%(geojson,self.name,self.url_name)
    return json

And... I have a view that looks like this:

json='{ "srid":4326, "type": "FeatureCollection","features": ['+','.join([asset.get_footprint_json() for asset in assets])+'] }'
return HttpResponse(json)

I'd be curious to see if anyone else has a better way or if django has updated their serializer to include geojson.

prauchfuss
  • 1,842
  • 2
  • 17
  • 19