Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Closes #16455. Added support for PostGIS 2.0 #249

Closed
wants to merge 10 commits into from

3 participants

Flavio Curella Reiner Marquez Thiago Avelino
Flavio Curella

Adds support for postgis 2.0 using a template database.
ref: Ticket #16455

Reiner Marquez

good work!!, eager for it is included.

Thiago Avelino
Flavio Curella

this patch seems pretty much done to me.

Let me know if you'd rather have everything squashed to a single commit.

Flavio Curella

committed in 92b5341

Flavio Curella fcurella closed this October 01, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
27  django/contrib/gis/db/backends/postgis/creation.py
... ...
@@ -1,4 +1,5 @@
1 1
 from django.conf import settings
  2
+from django.core.exceptions import ImproperlyConfigured
2 3
 from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation
3 4
 
4 5
 class PostGISCreation(DatabaseCreation):
@@ -38,12 +39,20 @@ def sql_indexes_for_field(self, model, f, style):
38 39
                                   style.SQL_FIELD(qn(f.column)) +
39 40
                                   style.SQL_KEYWORD(' SET NOT NULL') + ';')
40 41
 
41  
-
42 42
             if f.spatial_index:
43 43
                 # Spatial indexes created the same way for both Geometry and
44  
-                # Geography columns
  44
+                # Geography columns.
  45
+                # PostGIS 2.0 does not support GIST_GEOMETRY_OPS. So, on 1.5
  46
+                # we use GIST_GEOMETRY_OPS, on 2.0 we use either "nd" ops
  47
+                # which are fast on multidimensional cases, or just plain
  48
+                # gist index for the 2d case.
45 49
                 if f.geography:
46 50
                     index_opts = ''
  51
+                elif self.connection.ops.spatial_version >= (2, 0):
  52
+                    if f.dim > 2:
  53
+                        index_opts = ' ' + style.SQL_KEYWORD('gist_geometry_ops_nd')
  54
+                    else:
  55
+                        index_opts = ''
47 56
                 else:
48 57
                     index_opts = ' ' + style.SQL_KEYWORD(self.geom_index_opts)
49 58
                 output.append(style.SQL_KEYWORD('CREATE INDEX ') +
@@ -56,5 +65,15 @@ def sql_indexes_for_field(self, model, f, style):
56 65
         return output
57 66
 
58 67
     def sql_table_creation_suffix(self):
59  
-        qn = self.connection.ops.quote_name
60  
-        return ' TEMPLATE %s' % qn(getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis'))
  68
+        cursor = self.connection.cursor()
  69
+        cursor.execute('select datname from pg_database;')
  70
+        db_names = [row[0] for row in cursor.fetchall()]
  71
+        postgis_template = getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis')
  72
+
  73
+        if postgis_template in db_names:
  74
+            qn = self.connection.ops.quote_name
  75
+            return ' TEMPLATE %s' % qn(postgis_template)
  76
+        elif self.connection.ops.spatial_version < (2, 0):
  77
+            raise ImproperlyConfigured("Template database '%s' does not exist." % postgis_template)
  78
+        else:
  79
+            return ''
129  docs/ref/contrib/gis/install.txt
@@ -63,7 +63,7 @@ supported versions, and any notes for each of the supported database backends:
63 63
 ==================  ==============================  ==================  ==========================================================
64 64
 Database            Library Requirements            Supported Versions  Notes
65 65
 ==================  ==============================  ==================  ==========================================================
66  
-PostgreSQL          GEOS, PROJ.4, PostGIS           8.1+                Requires PostGIS.
  66
+PostgreSQL          GEOS, GDAL, PROJ.4, PostGIS     8.2+                Requires PostGIS.
67 67
 MySQL               GEOS                            5.x                 Not OGC-compliant; limited functionality.
68 68
 Oracle              GEOS                            10.2, 11            XE not supported; not tested with 9.
69 69
 SQLite              GEOS, GDAL, PROJ.4, SpatiaLite  3.6.+               Requires SpatiaLite 2.3+, pysqlite2 2.5+, and Django 1.1.
@@ -81,9 +81,10 @@ Program                   Description                           Required
81 81
 ========================  ====================================  ================================  ==========================
82 82
 :ref:`GEOS <ref-geos>`    Geometry Engine Open Source           Yes                               3.3, 3.2, 3.1, 3.0
83 83
 `PROJ.4`_                 Cartographic Projections library      Yes (PostgreSQL and SQLite only)  4.7, 4.6, 4.5, 4.4
84  
-:ref:`GDAL <ref-gdal>`    Geospatial Data Abstraction Library   No (but, required for SQLite)     1.9, 1.8, 1.7, 1.6, 1.5
  84
+:ref:`GDAL <ref-gdal>`    Geospatial Data Abstraction Library   No (but, required for SQLite
  85
+                                                                and PostgreSQL)                   1.9, 1.8, 1.7, 1.6, 1.5
85 86
 :ref:`GeoIP <ref-geoip>`  IP-based geolocation library          No                                1.4
86  
-`PostGIS`__               Spatial extensions for PostgreSQL     Yes (PostgreSQL only)             1.5, 1.4, 1.3
  87
+`PostGIS`__               Spatial extensions for PostgreSQL     Yes (PostgreSQL only)             2.0, 1.5, 1.4, 1.3
87 88
 `SpatiaLite`__            Spatial extensions for SQLite         Yes (SQLite only)                 3.0, 2.4, 2.3
88 89
 ========================  ====================================  ================================  ==========================
89 90
 
@@ -140,16 +141,16 @@ internal geometry representation used by GeoDjango (it's behind the "lazy"
140 141
 geometries).  Specifically, the C API library is called (e.g., ``libgeos_c.so``)
141 142
 directly from Python using ctypes.
142 143
 
143  
-First, download GEOS 3.2 from the refractions Web site and untar the source
  144
+First, download GEOS 3.3.5 from the refractions Web site and untar the source
144 145
 archive::
145 146
 
146  
-    $ wget http://download.osgeo.org/geos/geos-3.3.0.tar.bz2
147  
-    $ tar xjf geos-3.3.0.tar.bz2
  147
+    $ wget http://download.osgeo.org/geos/geos-3.3.5.tar.bz2
  148
+    $ tar xjf geos-3.3.5.tar.bz2
148 149
 
149 150
 Next, change into the directory where GEOS was unpacked, run the configure
150 151
 script, compile, and install::
151 152
 
152  
-    $ cd geos-3.3.0
  153
+    $ cd geos-3.3.5
153 154
     $ ./configure
154 155
     $ make
155 156
     $ sudo make install
@@ -221,45 +222,6 @@ Finally, configure, make and install PROJ.4::
221 222
     $ sudo make install
222 223
     $ cd ..
223 224
 
224  
-.. _postgis:
225  
-
226  
-PostGIS
227  
--------
228  
-
229  
-`PostGIS`__ adds geographic object support to PostgreSQL, turning it
230  
-into a spatial database. :ref:`geosbuild` and :ref:`proj4` should be
231  
-installed prior to building PostGIS.
232  
-
233  
-.. note::
234  
-
235  
-    The `psycopg2`_ module is required for use as the database adaptor
236  
-    when using GeoDjango with PostGIS.
237  
-
238  
-.. _psycopg2: http://initd.org/psycopg/
239  
-
240  
-First download the source archive, and extract::
241  
-
242  
-    $ wget http://postgis.refractions.net/download/postgis-1.5.2.tar.gz
243  
-    $ tar xzf postgis-1.5.2.tar.gz
244  
-    $ cd postgis-1.5.2
245  
-
246  
-Next, configure, make and install PostGIS::
247  
-
248  
-    $ ./configure
249  
-
250  
-Finally, make and install::
251  
-
252  
-    $ make
253  
-    $ sudo make install
254  
-    $ cd ..
255  
-
256  
-.. note::
257  
-
258  
-    GeoDjango does not automatically create a spatial database.  Please
259  
-    consult the section on :ref:`spatialdb_template` for more information.
260  
-
261  
-__ http://postgis.refractions.net/
262  
-
263 225
 .. _gdalbuild:
264 226
 
265 227
 GDAL
@@ -359,6 +321,48 @@ file:
359 321
 
360 322
     SetEnv GDAL_DATA /usr/local/share
361 323
 
  324
+.. _postgis:
  325
+
  326
+PostGIS
  327
+-------
  328
+
  329
+`PostGIS`__ adds geographic object support to PostgreSQL, turning it
  330
+into a spatial database. :ref:`geosbuild`, :ref:`proj4` and
  331
+:ref:`gdalbuild` should be installed prior to building PostGIS. You
  332
+might also need additional libraries, see `PostGIS requirements`_.
  333
+
  334
+.. note::
  335
+
  336
+    The `psycopg2`_ module is required for use as the database adaptor
  337
+    when using GeoDjango with PostGIS.
  338
+
  339
+.. _psycopg2: http://initd.org/psycopg/
  340
+.. _PostGIS requirements: http://www.postgis.org/documentation/manual-2.0/postgis_installation.html#id2711662
  341
+
  342
+First download the source archive, and extract::
  343
+
  344
+    $ wget http://postgis.refractions.net/download/postgis-2.0.1.tar.gz
  345
+    $ tar xzf postgis-2.0.1.tar.gz
  346
+    $ cd postgis-2.0.1
  347
+
  348
+Next, configure, make and install PostGIS::
  349
+
  350
+    $ ./configure
  351
+
  352
+Finally, make and install::
  353
+
  354
+    $ make
  355
+    $ sudo make install
  356
+    $ cd ..
  357
+
  358
+.. note::
  359
+
  360
+    GeoDjango does not automatically create a spatial database.  Please consult
  361
+    the section on :ref:`spatialdb_template91` or
  362
+    :ref:`spatialdb_template_earlier` for more information.
  363
+
  364
+__ http://postgis.refractions.net/
  365
+
362 366
 .. _spatialite:
363 367
 
364 368
 SpatiaLite
@@ -421,7 +425,7 @@ After SQLite has been built with the R*Tree module enabled, get the latest
421 425
 SpatiaLite library source and tools bundle from the `download page`__::
422 426
 
423 427
     $ wget http://www.gaia-gis.it/gaia-sins/libspatialite-sources/libspatialite-amalgamation-2.3.1.tar.gz
424  
-    $ wget http://www.gaia-gis.it/gaia-sins/libspatialite-sources/spatialite-tools-2.3.1.tar.gz
  428
+    $ wget http://www.gaia-gis.it/gaia-sins/spatialite-tools-sources/spatialite-tools-2.3.1.tar.gz
425 429
     $ tar xzf libspatialite-amalgamation-2.3.1.tar.gz
426 430
     $ tar xzf spatialite-tools-2.3.1.tar.gz
427 431
 
@@ -502,10 +506,27 @@ to build and install::
502 506
 Post-installation
503 507
 =================
504 508
 
505  
-.. _spatialdb_template:
  509
+.. _spatialdb_template91:
  510
+
  511
+Creating a spatial database with PostGIS 2.0 and PostgreSQL 9.1
  512
+---------------------------------------------------------------
  513
+
  514
+PostGIS 2 includes an extension for Postgres 9.1 that can be used to enable
  515
+spatial functionality::
  516
+
  517
+    $ createdb  <db name>
  518
+    $ psql <db name>
  519
+    > CREATE EXTENSION postgis;
  520
+    > CREATE EXTENSION postgis_topology;
  521
+
  522
+.. _spatialdb_template_earlier:
  523
+
  524
+Creating a spatial database template for earlier versions
  525
+---------------------------------------------------------
506 526
 
507  
-Creating a spatial database template for PostGIS
508  
-------------------------------------------------
  527
+If you have an earlier version of PostGIS or PostgreSQL, the CREATE
  528
+EXTENSION isn't available and you need to create the spatial database
  529
+using the following instructions.
509 530
 
510 531
 Creating a spatial database with PostGIS is different than normal because
511 532
 additional SQL must be loaded to enable spatial functionality.  Because of
@@ -535,7 +556,7 @@ user.  For example, you can use the following to become the ``postgres`` user::
535 556
 Once you're a database super user, then you may execute the following commands
536 557
 to create a PostGIS spatial database template::
537 558
 
538  
-    $ POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib/postgis-1.5
  559
+    $ POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib/postgis-2.0
539 560
     # Creating the template spatial database.
540 561
     $ createdb -E UTF8 template_postgis
541 562
     $ createlang -d template_postgis plpgsql # Adding PLPGSQL language support.
@@ -605,7 +626,7 @@ http://www.gaia-gis.it/spatialite-2.4.0/ for 2.4)::
605 626
 
606 627
 Then, use the ``spatialite`` command to initialize a spatial database::
607 628
 
608  
-   $ spatialite geodjango.db < init_spatialite-2.X.sql
  629
+   $ spatialite geodjango.db < init_spatialite-2.3.sql
609 630
 
610 631
 .. note::
611 632
 
@@ -1080,7 +1101,7 @@ Afterwards, the ``/etc/init.d/postgresql-8.3`` script should be used to manage
1080 1101
 the starting and stopping of PostgreSQL.
1081 1102
 
1082 1103
 In addition, the SQL files for PostGIS are placed in a different location on
1083  
-Debian 5.0 . Thus when :ref:`spatialdb_template` either:
  1104
+Debian 5.0 . Thus when :ref:`spatialdb_template_earlier` either:
1084 1105
 
1085 1106
 * Create a symbolic link to these files:
1086 1107
 
@@ -1266,4 +1287,4 @@ may be executed from the SQL Shell as the ``postgres`` user::
1266 1287
 .. [#] GeoDjango uses the :func:`~ctypes.util.find_library` routine from
1267 1288
        :mod:`ctypes.util` to locate shared libraries.
1268 1289
 .. [#] The ``psycopg2`` Windows installers are packaged and maintained by
1269  
-       `Jason Erickson <http://www.stickpeople.com/projects/python/win-psycopg/>`_.
  1290
+       `Jason Erickson <http://www.stickpeople.com/projects/python/win-psycopg/>`_.
2  docs/releases/1.5.txt
@@ -252,6 +252,8 @@ on the form.
252 252
 Miscellaneous
253 253
 ~~~~~~~~~~~~~
254 254
 
  255
+* GeoDjango added support for PostGIS 2.0
  256
+
255 257
 * GeoDjango dropped support for GDAL < 1.5
256 258
 
257 259
 * :func:`~django.utils.http.int_to_base36` properly raises a :exc:`TypeError`
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.