From 1da170a203819ffda7764e53e3c268b8fc2ab452 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 18 Jul 2015 14:50:08 +0200 Subject: Fixed #25141 -- Diminished GDAL dependence during geojson serialization Only require GDAL if contained geometries need coordinate transformations. Thanks drepo for the report and Tim Graham for the review. --- django/contrib/gis/serializers/geojson.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'django/contrib/gis/serializers') diff --git a/django/contrib/gis/serializers/geojson.py b/django/contrib/gis/serializers/geojson.py index 9c15b49676..f26147a59a 100644 --- a/django/contrib/gis/serializers/geojson.py +++ b/django/contrib/gis/serializers/geojson.py @@ -17,17 +17,14 @@ class Serializer(JSONSerializer): def _init_options(self): super(Serializer, self)._init_options() self.geometry_field = self.json_kwargs.pop('geometry_field', None) - self.srs = SpatialReference(self.json_kwargs.pop('srid', 4326)) + self.srid = self.json_kwargs.pop('srid', 4326) def start_serialization(self): - if not HAS_GDAL: - # GDAL is needed for the geometry.geojson call - raise SerializationError("The geojson serializer requires the GDAL library.") self._init_options() self._cts = {} # cache of CoordTransform's self.stream.write( '{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:%d"}},' - ' "features": [' % self.srs.srid) + ' "features": [' % self.srid) def end_serialization(self): self.stream.write(']}') @@ -48,10 +45,15 @@ class Serializer(JSONSerializer): "properties": self._current, } if self._geometry: - if self._geometry.srid != self.srs.srid: + if self._geometry.srid != self.srid: # If needed, transform the geometry in the srid of the global geojson srid + if not HAS_GDAL: + raise SerializationError( + 'Unable to convert geometry to SRID %s when GDAL is not installed.' % self.srid + ) if self._geometry.srid not in self._cts: - self._cts[self._geometry.srid] = CoordTransform(self._geometry.srs, self.srs) + srs = SpatialReference(self.srid) + self._cts[self._geometry.srid] = CoordTransform(self._geometry.srs, srs) self._geometry.transform(self._cts[self._geometry.srid]) data["geometry"] = eval(self._geometry.geojson) else: -- cgit v1.3