|
0
|
1 |
from django.db import connection |
|
|
2 |
from django.db.models.fields import Field # Django base Field class |
|
|
3 |
from django.contrib.gis.db.backend.util import gqn |
|
|
4 |
from django.contrib.gis.db.backend.postgis.query import TRANSFORM |
|
|
5 |
|
|
|
6 |
# Quotename & geographic quotename, respectively |
|
|
7 |
qn = connection.ops.quote_name |
|
|
8 |
|
|
|
9 |
class PostGISField(Field): |
|
|
10 |
""" |
|
|
11 |
The backend-specific geographic field for PostGIS. |
|
|
12 |
""" |
|
|
13 |
|
|
|
14 |
def _add_geom(self, style, db_table): |
|
|
15 |
""" |
|
|
16 |
Constructs the addition of the geometry to the table using the |
|
|
17 |
AddGeometryColumn(...) PostGIS (and OGC standard) stored procedure. |
|
|
18 |
|
|
|
19 |
Takes the style object (provides syntax highlighting) and the |
|
|
20 |
database table as parameters. |
|
|
21 |
""" |
|
|
22 |
sql = (style.SQL_KEYWORD('SELECT ') + |
|
|
23 |
style.SQL_TABLE('AddGeometryColumn') + '(' + |
|
|
24 |
style.SQL_TABLE(gqn(db_table)) + ', ' + |
|
|
25 |
style.SQL_FIELD(gqn(self.column)) + ', ' + |
|
|
26 |
style.SQL_FIELD(str(self.srid)) + ', ' + |
|
|
27 |
style.SQL_COLTYPE(gqn(self.geom_type)) + ', ' + |
|
|
28 |
style.SQL_KEYWORD(str(self.dim)) + ');') |
|
|
29 |
|
|
|
30 |
if not self.null: |
|
|
31 |
# Add a NOT NULL constraint to the field |
|
|
32 |
sql += ('\n' + |
|
|
33 |
style.SQL_KEYWORD('ALTER TABLE ') + |
|
|
34 |
style.SQL_TABLE(qn(db_table)) + |
|
|
35 |
style.SQL_KEYWORD(' ALTER ') + |
|
|
36 |
style.SQL_FIELD(qn(self.column)) + |
|
|
37 |
style.SQL_KEYWORD(' SET NOT NULL') + ';') |
|
|
38 |
return sql |
|
|
39 |
|
|
|
40 |
def _geom_index(self, style, db_table, |
|
|
41 |
index_type='GIST', index_opts='GIST_GEOMETRY_OPS'): |
|
|
42 |
"Creates a GiST index for this geometry field." |
|
|
43 |
sql = (style.SQL_KEYWORD('CREATE INDEX ') + |
|
|
44 |
style.SQL_TABLE(qn('%s_%s_id' % (db_table, self.column))) + |
|
|
45 |
style.SQL_KEYWORD(' ON ') + |
|
|
46 |
style.SQL_TABLE(qn(db_table)) + |
|
|
47 |
style.SQL_KEYWORD(' USING ') + |
|
|
48 |
style.SQL_COLTYPE(index_type) + ' ( ' + |
|
|
49 |
style.SQL_FIELD(qn(self.column)) + ' ' + |
|
|
50 |
style.SQL_KEYWORD(index_opts) + ' );') |
|
|
51 |
return sql |
|
|
52 |
|
|
|
53 |
def post_create_sql(self, style, db_table): |
|
|
54 |
""" |
|
|
55 |
Returns SQL that will be executed after the model has been |
|
|
56 |
created. Geometry columns must be added after creation with the |
|
|
57 |
PostGIS AddGeometryColumn() function. |
|
|
58 |
""" |
|
|
59 |
|
|
|
60 |
# Getting the AddGeometryColumn() SQL necessary to create a PostGIS |
|
|
61 |
# geometry field. |
|
|
62 |
post_sql = self._add_geom(style, db_table) |
|
|
63 |
|
|
|
64 |
# If the user wants to index this data, then get the indexing SQL as well. |
|
|
65 |
if self.spatial_index: |
|
|
66 |
return (post_sql, self._geom_index(style, db_table)) |
|
|
67 |
else: |
|
|
68 |
return (post_sql,) |
|
|
69 |
|
|
|
70 |
def _post_delete_sql(self, style, db_table): |
|
|
71 |
"Drops the geometry column." |
|
|
72 |
sql = (style.SQL_KEYWORD('SELECT ') + |
|
|
73 |
style.SQL_KEYWORD('DropGeometryColumn') + '(' + |
|
|
74 |
style.SQL_TABLE(gqn(db_table)) + ', ' + |
|
|
75 |
style.SQL_FIELD(gqn(self.column)) + ');') |
|
|
76 |
return sql |
|
|
77 |
|
|
|
78 |
def db_type(self): |
|
|
79 |
""" |
|
|
80 |
PostGIS geometry columns are added by stored procedures, should be |
|
|
81 |
None. |
|
|
82 |
""" |
|
|
83 |
return None |
|
|
84 |
|
|
|
85 |
def get_placeholder(self, value): |
|
|
86 |
""" |
|
|
87 |
Provides a proper substitution value for Geometries that are not in the |
|
|
88 |
SRID of the field. Specifically, this routine will substitute in the |
|
|
89 |
ST_Transform() function call. |
|
|
90 |
""" |
|
|
91 |
if value is None or value.srid == self.srid: |
|
|
92 |
return '%s' |
|
|
93 |
else: |
|
|
94 |
# Adding Transform() to the SQL placeholder. |
|
|
95 |
return '%s(%%s, %s)' % (TRANSFORM, self.srid) |