2017-05-03 09:27:11 +08:00
|
|
|
from django.contrib.gis.gdal import CoordTransform, SpatialReference
|
2016-06-14 22:18:33 +08:00
|
|
|
from django.core.serializers.base import SerializerDoesNotExist
|
2014-11-09 00:08:12 +08:00
|
|
|
from django.core.serializers.json import Serializer as JSONSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class Serializer(JSONSerializer):
|
|
|
|
"""
|
|
|
|
Convert a queryset to GeoJSON, http://geojson.org/
|
|
|
|
"""
|
|
|
|
def _init_options(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
super()._init_options()
|
2014-11-09 00:08:12 +08:00
|
|
|
self.geometry_field = self.json_kwargs.pop('geometry_field', None)
|
2015-07-18 20:50:08 +08:00
|
|
|
self.srid = self.json_kwargs.pop('srid', 4326)
|
2016-04-04 08:37:32 +08:00
|
|
|
if (self.selected_fields is not None and self.geometry_field is not None and
|
|
|
|
self.geometry_field not in self.selected_fields):
|
2016-01-26 18:06:29 +08:00
|
|
|
self.selected_fields = list(self.selected_fields) + [self.geometry_field]
|
2014-11-09 00:08:12 +08:00
|
|
|
|
|
|
|
def start_serialization(self):
|
|
|
|
self._init_options()
|
|
|
|
self._cts = {} # cache of CoordTransform's
|
|
|
|
self.stream.write(
|
|
|
|
'{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:%d"}},'
|
2015-07-18 20:50:08 +08:00
|
|
|
' "features": [' % self.srid)
|
2014-11-09 00:08:12 +08:00
|
|
|
|
|
|
|
def end_serialization(self):
|
|
|
|
self.stream.write(']}')
|
|
|
|
|
|
|
|
def start_object(self, obj):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().start_object(obj)
|
2014-11-09 00:08:12 +08:00
|
|
|
self._geometry = None
|
|
|
|
if self.geometry_field is None:
|
|
|
|
# Find the first declared geometry field
|
|
|
|
for field in obj._meta.fields:
|
|
|
|
if hasattr(field, 'geom_type'):
|
|
|
|
self.geometry_field = field.name
|
|
|
|
break
|
|
|
|
|
|
|
|
def get_dump_object(self, obj):
|
|
|
|
data = {
|
|
|
|
"type": "Feature",
|
|
|
|
"properties": self._current,
|
|
|
|
}
|
2016-02-24 02:57:53 +08:00
|
|
|
if ((self.selected_fields is None or 'pk' in self.selected_fields) and
|
|
|
|
'pk' not in data["properties"]):
|
|
|
|
data["properties"]["pk"] = obj._meta.pk.value_to_string(obj)
|
2014-11-09 00:08:12 +08:00
|
|
|
if self._geometry:
|
2015-07-18 20:50:08 +08:00
|
|
|
if self._geometry.srid != self.srid:
|
2014-11-09 00:08:12 +08:00
|
|
|
# If needed, transform the geometry in the srid of the global geojson srid
|
|
|
|
if self._geometry.srid not in self._cts:
|
2015-07-18 20:50:08 +08:00
|
|
|
srs = SpatialReference(self.srid)
|
|
|
|
self._cts[self._geometry.srid] = CoordTransform(self._geometry.srs, srs)
|
2014-11-09 00:08:12 +08:00
|
|
|
self._geometry.transform(self._cts[self._geometry.srid])
|
|
|
|
data["geometry"] = eval(self._geometry.geojson)
|
|
|
|
else:
|
|
|
|
data["geometry"] = None
|
|
|
|
return data
|
|
|
|
|
|
|
|
def handle_field(self, obj, field):
|
|
|
|
if field.name == self.geometry_field:
|
2015-04-26 14:30:46 +08:00
|
|
|
self._geometry = field.value_from_object(obj)
|
2014-11-09 00:08:12 +08:00
|
|
|
else:
|
2017-01-21 21:13:44 +08:00
|
|
|
super().handle_field(obj, field)
|
2014-11-09 00:08:12 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Deserializer:
|
2014-11-09 00:08:12 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
raise SerializerDoesNotExist("geojson is a serialization-only serializer")
|