From 28cb5bf1622cd8fa6f324cc516974e3badc31a51 Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Sun, 11 Sep 2011 00:52:08 +0000 Subject: [PATCH] Fixed #16231 -- Added support for GML and KML on the SpatiaLite backend. Thanks, steko for the bug report and jpaulett for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16800 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + .../gis/db/backends/spatialite/operations.py | 12 ++++++++++++ django/contrib/gis/tests/geoapp/tests.py | 18 +++++++++++------- docs/ref/contrib/gis/db-api.txt | 4 ++-- docs/ref/contrib/gis/geoquerysets.txt | 4 ++-- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/AUTHORS b/AUTHORS index d12b5fb346..6faa171e31 100644 --- a/AUTHORS +++ b/AUTHORS @@ -393,6 +393,7 @@ answer newbie questions, and generally made Django that much better: Jay Parlar Claude Paroz Carlos Eduardo de Paula + John Paulett pavithran s Barry Pederson Andreas Pelme diff --git a/django/contrib/gis/db/backends/spatialite/operations.py b/django/contrib/gis/db/backends/spatialite/operations.py index 449c527187..a0efb99527 100644 --- a/django/contrib/gis/db/backends/spatialite/operations.py +++ b/django/contrib/gis/db/backends/spatialite/operations.py @@ -133,6 +133,18 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations): gis_terms += self.geometry_functions.keys() self.gis_terms = dict([(term, None) for term in gis_terms]) + if version >= (2, 4, 0): + # Spatialite 2.4.0-RC4 added AsGML and AsKML, however both + # RC2 (shipped in popular Debian/Ubuntu packages) and RC4 + # report version as '2.4.0', so we fall back to feature detection + try: + self._get_spatialite_func("AsGML(GeomFromText('POINT(1 1)'))") + self.gml = 'AsGML' + self.kml = 'AsKML' + except DatabaseError: + # we are using < 2.4.0-RC4 + pass + def check_aggregate_support(self, aggregate): """ Checks if the given aggregate name is supported (that is, if it's diff --git a/django/contrib/gis/tests/geoapp/tests.py b/django/contrib/gis/tests/geoapp/tests.py index b7ce3b7111..25023d7bf5 100644 --- a/django/contrib/gis/tests/geoapp/tests.py +++ b/django/contrib/gis/tests/geoapp/tests.py @@ -1,5 +1,6 @@ import re from django.db import connection +from django.db.utils import DatabaseError from django.contrib.gis import gdal from django.contrib.gis.geos import (fromstr, GEOSGeometry, Point, LineString, LinearRing, Polygon, GeometryCollection) @@ -92,8 +93,8 @@ class GeoModelTest(TestCase): def test03a_kml(self): "Testing KML output from the database using GeoQuerySet.kml()." - # Only PostGIS supports KML serialization - if not postgis: + # Only PostGIS and Spatialite (>=2.4.0-RC4) support KML serialization + if not (postgis or (spatialite and connection.ops.kml)): self.assertRaises(NotImplementedError, State.objects.all().kml, field_name='poly') return @@ -117,7 +118,7 @@ class GeoModelTest(TestCase): def test03b_gml(self): "Testing GML output from the database using GeoQuerySet.gml()." - if mysql or spatialite: + if mysql or (spatialite and not connection.ops.gml) : self.assertRaises(NotImplementedError, Country.objects.all().gml, field_name='mpoly') return @@ -131,12 +132,15 @@ class GeoModelTest(TestCase): if oracle: # No precision parameter for Oracle :-/ gml_regex = re.compile(r'^-104.60925\d+,38.25500\d+ ') - for ptown in [ptown1, ptown2]: - self.assertTrue(gml_regex.match(ptown.gml)) + elif spatialite: + # Spatialite has extra colon in SrsName + gml_regex = re.compile(r'^-104.609251\d+,38.255001') else: gml_regex = re.compile(r'^-104\.60925\d+,38\.255001') - for ptown in [ptown1, ptown2]: - self.assertTrue(gml_regex.match(ptown.gml)) + + for ptown in [ptown1, ptown2]: + self.assertTrue(gml_regex.match(ptown.gml)) + def test03c_geojson(self): "Testing GeoJSON output from the database using GeoQuerySet.geojson()." diff --git a/docs/ref/contrib/gis/db-api.txt b/docs/ref/contrib/gis/db-api.txt index 9bd5eccf14..a75a04ee4f 100644 --- a/docs/ref/contrib/gis/db-api.txt +++ b/docs/ref/contrib/gis/db-api.txt @@ -287,9 +287,9 @@ Method PostGIS Oracle SpatiaLite :meth:`GeoQuerySet.force_rhr` X :meth:`GeoQuerySet.geohash` X :meth:`GeoQuerySet.geojson` X -:meth:`GeoQuerySet.gml` X X +:meth:`GeoQuerySet.gml` X X X :meth:`GeoQuerySet.intersection` X X X -:meth:`GeoQuerySet.kml` X +:meth:`GeoQuerySet.kml` X X :meth:`GeoQuerySet.length` X X X :meth:`GeoQuerySet.make_line` X :meth:`GeoQuerySet.mem_size` X diff --git a/docs/ref/contrib/gis/geoquerysets.txt b/docs/ref/contrib/gis/geoquerysets.txt index f96929ea47..a58f472005 100644 --- a/docs/ref/contrib/gis/geoquerysets.txt +++ b/docs/ref/contrib/gis/geoquerysets.txt @@ -982,7 +982,7 @@ __ http://geojson.org/ .. method:: GeoQuerySet.gml(**kwargs) -*Availability*: PostGIS, Oracle +*Availability*: PostGIS, Oracle, SpatiaLite Attaches a ``gml`` attribute to every model in the queryset that contains the `Geographic Markup Language (GML)`__ representation of the geometry. @@ -1013,7 +1013,7 @@ __ http://en.wikipedia.org/wiki/Geography_Markup_Language .. method:: GeoQuerySet.kml(**kwargs) -*Availability*: PostGIS +*Availability*: PostGIS, SpatiaLite Attaches a ``kml`` attribute to every model in the queryset that contains the `Keyhole Markup Language (KML)`__ representation of the geometry fields. It