Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed #19028 -- Support GeoJSON output with SpatiaLite 3.0+
  • Loading branch information
bkg committed Sep 26, 2012
1 parent 1f84b04 commit 66b6242
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
2 changes: 2 additions & 0 deletions django/contrib/gis/db/backends/spatialite/operations.py
Expand Up @@ -146,6 +146,8 @@ def confirm_spatial_components_versions(self):
except DatabaseError: except DatabaseError:
# we are using < 2.4.0-RC4 # we are using < 2.4.0-RC4
pass pass
if version >= (3, 0, 0):
self.geojson = 'AsGeoJSON'


def check_aggregate_support(self, aggregate): def check_aggregate_support(self, aggregate):
""" """
Expand Down
5 changes: 3 additions & 2 deletions django/contrib/gis/db/models/query.py
Expand Up @@ -146,13 +146,14 @@ def geojson(self, precision=8, crs=False, bbox=False, **kwargs):
""" """
backend = connections[self.db].ops backend = connections[self.db].ops
if not backend.geojson: if not backend.geojson:
raise NotImplementedError('Only PostGIS 1.3.4+ supports GeoJSON serialization.') raise NotImplementedError('Only PostGIS 1.3.4+ and SpatiaLite 3.0+'
'supports GeoJSON serialization.')


if not isinstance(precision, six.integer_types): if not isinstance(precision, six.integer_types):
raise TypeError('Precision keyword must be set with an integer.') raise TypeError('Precision keyword must be set with an integer.')


# Setting the options flag -- which depends on which version of # Setting the options flag -- which depends on which version of
# PostGIS we're using. # PostGIS we're using. SpatiaLite only uses the first group of options.
if backend.spatial_version >= (1, 4, 0): if backend.spatial_version >= (1, 4, 0):
options = 0 options = 0
if crs and bbox: options = 3 if crs and bbox: options = 3
Expand Down
16 changes: 8 additions & 8 deletions django/contrib/gis/tests/geoapp/tests.py
Expand Up @@ -467,21 +467,21 @@ def test_geohash(self):


def test_geojson(self): def test_geojson(self):
"Testing GeoJSON output from the database using GeoQuerySet.geojson()." "Testing GeoJSON output from the database using GeoQuerySet.geojson()."
# Only PostGIS 1.3.4+ supports GeoJSON. # Only PostGIS 1.3.4+ and SpatiaLite 3.0+ support GeoJSON.
if not connection.ops.geojson: if not connection.ops.geojson:
self.assertRaises(NotImplementedError, Country.objects.all().geojson, field_name='mpoly') self.assertRaises(NotImplementedError, Country.objects.all().geojson, field_name='mpoly')
return return


if connection.ops.spatial_version >= (1, 4, 0): pueblo_json = '{"type":"Point","coordinates":[-104.609252,38.255001]}'
pueblo_json = '{"type":"Point","coordinates":[-104.609252,38.255001]}' houston_json = '{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[-95.363151,29.763374]}'
houston_json = '{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[-95.363151,29.763374]}' victoria_json = '{"type":"Point","bbox":[-123.30519600,48.46261100,-123.30519600,48.46261100],"coordinates":[-123.305196,48.462611]}'
victoria_json = '{"type":"Point","bbox":[-123.30519600,48.46261100,-123.30519600,48.46261100],"coordinates":[-123.305196,48.462611]}' chicago_json = '{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"bbox":[-87.65018,41.85039,-87.65018,41.85039],"coordinates":[-87.65018,41.85039]}'
chicago_json = '{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"bbox":[-87.65018,41.85039,-87.65018,41.85039],"coordinates":[-87.65018,41.85039]}' if postgis and connection.ops.spatial_version < (1, 4, 0):
else:
pueblo_json = '{"type":"Point","coordinates":[-104.60925200,38.25500100]}' pueblo_json = '{"type":"Point","coordinates":[-104.60925200,38.25500100]}'
houston_json = '{"type":"Point","crs":{"type":"EPSG","properties":{"EPSG":4326}},"coordinates":[-95.36315100,29.76337400]}' houston_json = '{"type":"Point","crs":{"type":"EPSG","properties":{"EPSG":4326}},"coordinates":[-95.36315100,29.76337400]}'
victoria_json = '{"type":"Point","bbox":[-123.30519600,48.46261100,-123.30519600,48.46261100],"coordinates":[-123.30519600,48.46261100]}' victoria_json = '{"type":"Point","bbox":[-123.30519600,48.46261100,-123.30519600,48.46261100],"coordinates":[-123.30519600,48.46261100]}'
chicago_json = '{"type":"Point","crs":{"type":"EPSG","properties":{"EPSG":4326}},"bbox":[-87.65018,41.85039,-87.65018,41.85039],"coordinates":[-87.65018,41.85039]}' elif spatialite:
victoria_json = '{"type":"Point","bbox":[-123.305196,48.462611,-123.305196,48.462611],"coordinates":[-123.305196,48.462611]}'


# Precision argument should only be an integer # Precision argument should only be an integer
self.assertRaises(TypeError, City.objects.geojson, precision='foo') self.assertRaises(TypeError, City.objects.geojson, precision='foo')
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/contrib/gis/db-api.txt
Expand Up @@ -282,7 +282,7 @@ Method PostGIS Oracle SpatiaLite
:meth:`GeoQuerySet.extent3d` X :meth:`GeoQuerySet.extent3d` X
:meth:`GeoQuerySet.force_rhr` X :meth:`GeoQuerySet.force_rhr` X
:meth:`GeoQuerySet.geohash` X :meth:`GeoQuerySet.geohash` X
:meth:`GeoQuerySet.geojson` X :meth:`GeoQuerySet.geojson` X X
:meth:`GeoQuerySet.gml` X X X :meth:`GeoQuerySet.gml` X X X
:meth:`GeoQuerySet.intersection` X X X :meth:`GeoQuerySet.intersection` X X X
:meth:`GeoQuerySet.kml` X X :meth:`GeoQuerySet.kml` X X
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/contrib/gis/geoquerysets.txt
Expand Up @@ -947,7 +947,7 @@ __ http://geohash.org/


.. method:: GeoQuerySet.geojson(**kwargs) .. method:: GeoQuerySet.geojson(**kwargs)


*Availability*: PostGIS *Availability*: PostGIS, SpatiaLite


Attaches a ``geojson`` attribute to every model in the queryset that contains the Attaches a ``geojson`` attribute to every model in the queryset that contains the
`GeoJSON`__ representation of the geometry. `GeoJSON`__ representation of the geometry.
Expand Down

0 comments on commit 66b6242

Please sign in to comment.