Commits

tcarobruce  committed ce24032

adding serialize_geometry for GeometryField types; emitters can override for their format

  • Participants
  • Parent commits cbe9f56

Comments (0)

Files changed (1)

File piston/emitters.py

 
 from django.db.models.query import QuerySet
 from django.db.models import Model, permalink
+from django.contrib.gis.geos.geometry import GEOSGeometry
+from django.contrib.gis.gdal.geometries import OGRGeometry
 from django.utils import simplejson
 from django.utils.xmlutils import SimplerXMLGenerator
 from django.utils.encoding import smart_unicode
                 ret = _model(thing, fields=fields)
             elif isinstance(thing, HttpResponse):
                 raise HttpStatusCode(thing)
+            elif isinstance(thing, (GEOSGeometry,OGRGeometry)):
+                ret = self.serialize_geometry(thing)
             elif inspect.isfunction(thing):
                 if not inspect.getargspec(thing)[0]:
                     ret = _any(thing())
             
         # Kickstart the seralizin'.
         return _any(self.data, self.fields)
+
+    def serialize_geometry(self, geometry):
+        return geometry.wkt
     
     def in_typemapper(self, model, anonymous):
         for klass, (km, is_anon) in self.typemapper.iteritems():
             return '%s(%s)' % (cb, seria)
 
         return seria
+
+    def serialize_geometry(self, geometry):
+        return geometry.geojson
+
     
 Emitter.register('json', JSONEmitter, 'application/json; charset=utf-8')
 Mimer.register(simplejson.loads, ('application/json',))