From c406b554c76ec57bbeddea090a7ca1a4f1f6daa9 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Sat, 11 Feb 2012 00:40:18 +0000 Subject: [PATCH] Added, documented support for SpatiaLite 3.0 to GeoDjango. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17496 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../gis/db/backends/spatialite/creation.py | 37 +++++++++++------- docs/ref/contrib/gis/install.txt | 39 +++++++++++++++---- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/django/contrib/gis/db/backends/spatialite/creation.py b/django/contrib/gis/db/backends/spatialite/creation.py index 03ea45da44..ce46d5b7a3 100644 --- a/django/contrib/gis/db/backends/spatialite/creation.py +++ b/django/contrib/gis/db/backends/spatialite/creation.py @@ -102,21 +102,30 @@ class SpatiaLiteCreation(DatabaseCreation): """ This routine loads up the SpatiaLite SQL file. """ - # Getting the location of the SpatiaLite SQL file, and confirming - # it exists. - spatialite_sql = self.spatialite_init_file() - if not os.path.isfile(spatialite_sql): - raise ImproperlyConfigured('Could not find the required SpatiaLite initialization ' - 'SQL file (necessary for testing): %s' % spatialite_sql) - - # Opening up the SpatiaLite SQL initialization file and executing - # as a script. - sql_fh = open(spatialite_sql, 'r') - try: + if self.connection.ops.spatial_version[:2] >= (3, 0): + # Spatialite >= 3.0.x -- No ned to load any SQL file, calling + # InitSpatialMetaData() transparently creates the spatial metadata + # tables cur = self.connection._cursor() - cur.executescript(sql_fh.read()) - finally: - sql_fh.close() + cur.execute("SELECT InitSpatialMetaData()") + else: + # Spatialite < 3.0.x -- Load the initial SQL + + # Getting the location of the SpatiaLite SQL file, and confirming + # it exists. + spatialite_sql = self.spatialite_init_file() + if not os.path.isfile(spatialite_sql): + raise ImproperlyConfigured('Could not find the required SpatiaLite initialization ' + 'SQL file (necessary for testing): %s' % spatialite_sql) + + # Opening up the SpatiaLite SQL initialization file and executing + # as a script. + sql_fh = open(spatialite_sql, 'r') + try: + cur = self.connection._cursor() + cur.executescript(sql_fh.read()) + finally: + sql_fh.close() def spatialite_init_file(self): # SPATIALITE_SQL may be placed in settings to tell GeoDjango diff --git a/docs/ref/contrib/gis/install.txt b/docs/ref/contrib/gis/install.txt index c7fd080142..209bce5b3e 100644 --- a/docs/ref/contrib/gis/install.txt +++ b/docs/ref/contrib/gis/install.txt @@ -80,7 +80,7 @@ Program Description Required :ref:`GDAL ` Geospatial Data Abstraction Library No (but, required for SQLite) 1.8, 1.7, 1.6, 1.5, 1.4 :ref:`GeoIP ` IP-based geolocation library No 1.4 `PostGIS`__ Spatial extensions for PostgreSQL Yes (PostgreSQL only) 1.5, 1.4, 1.3 -`SpatiaLite`__ Spatial extensions for SQLite Yes (SQLite only) 2.4, 2.3 +`SpatiaLite`__ Spatial extensions for SQLite Yes (SQLite only) 3.0, 2.4, 2.3 ======================== ==================================== ================================ ========================== .. admonition:: Install GDAL @@ -560,15 +560,42 @@ Creating a Spatial Database for SpatiaLite ------------------------------------------- After the SpatiaLite library and tools have been installed, it is now possible -to create spatial database for use with GeoDjango. In order to do this, download -the spatial database initialization SQL from the `SpatiaLite Resources`__ page:: +to create a spatial database for use with GeoDjango. + +For this, a number of spatial metadata tables must be created in the database +before any spatial query is performed against it. + +If you are using SpatiaLite 3.0 or newer then use the ``spatialite`` utility to +call the ``InitSpatiaMetaData()`` function whch will take care of that (you can +safely ignore the error messages shown) then you can skip the rest of this +section:: + + $ spatialite geodjango.db "SELECT InitSpatialMetaData();" + the SPATIAL_REF_SYS table already contains some row(s) + InitSpatiaMetaData ()error:"table spatial_ref_sys already exists" + 0 + +If you re using a version of Spatialite older than 3.0 then to achieve the same +result you need to download a database initialization file and execute the SQL +queries it contains against your database. + +First, get it from the appropiate SpatiaLite Resources page (i.e. +http://www.gaia-gis.it/spatialite-2.3.1/resources.html for 2.3 or +http://www.gaia-gis.it/spatialite-2.4.0/ for 2.4):: $ wget http://www.gaia-gis.it/spatialite-2.3.1/init_spatialite-2.3.sql.gz $ gunzip init_spatialite-2.3.sql.gz +(Or, if you are using SpatiaLite 2.4 then do:: + + $ wget http://www.gaia-gis.it/spatialite-2.4.0/init_spatialite-2.4.sql.gz + $ gunzip init_spatialite-2.4.sql.gz + +) + Now, the ``spatialite`` command can be used to initialize a spatial database:: - $ spatialite geodjango.db < init_spatialite-2.3.sql + $ spatialite geodjango.db < init_spatialite-2.X.sql .. note:: @@ -576,10 +603,6 @@ Now, the ``spatialite`` command can be used to initialize a spatial database:: you want to use. Use the same in the :setting:`DATABASES` ``"name"`` key inside your ``settings.py``. - -__ http://www.gaia-gis.it/spatialite-2.3.1/resources.html - - Add ``django.contrib.gis`` to :setting:`INSTALLED_APPS` -------------------------------------------------------