2013-11-27 21:32:12 +08:00
|
|
|
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
|
|
|
|
|
|
|
|
|
|
|
|
class PostGISSchemaEditor(DatabaseSchemaEditor):
|
|
|
|
geom_index_type = 'GIST'
|
|
|
|
geom_index_ops = 'GIST_GEOMETRY_OPS'
|
|
|
|
geom_index_ops_nd = 'GIST_GEOMETRY_OPS_ND'
|
|
|
|
|
|
|
|
sql_add_geometry_column = "SELECT AddGeometryColumn(%(table)s, %(column)s, %(srid)s, %(geom_type)s, %(dim)s)"
|
2014-04-21 20:04:53 +08:00
|
|
|
sql_drop_geometry_column = "SELECT DropGeometryColumn(%(table)s, %(column)s)"
|
2013-11-27 21:32:12 +08:00
|
|
|
sql_alter_geometry_column_not_null = "ALTER TABLE %(table)s ALTER COLUMN %(column)s SET NOT NULL"
|
|
|
|
sql_add_spatial_index = "CREATE INDEX %(index)s ON %(table)s USING %(index_type)s (%(column)s %(ops)s)"
|
|
|
|
|
2014-04-21 17:25:48 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(PostGISSchemaEditor, self).__init__(*args, **kwargs)
|
|
|
|
self.geometry_sql = []
|
|
|
|
|
2013-11-27 21:32:12 +08:00
|
|
|
def geo_quote_name(self, name):
|
|
|
|
return self.connection.ops.geo_quote_name(name)
|
|
|
|
|
2014-04-21 17:25:48 +08:00
|
|
|
def column_sql(self, model, field, include_default=False):
|
2013-11-27 21:32:12 +08:00
|
|
|
from django.contrib.gis.db.models.fields import GeometryField
|
2014-04-21 17:25:48 +08:00
|
|
|
if not isinstance(field, GeometryField):
|
|
|
|
return super(PostGISSchemaEditor, self).column_sql(model, field, include_default)
|
2013-11-27 21:32:12 +08:00
|
|
|
|
|
|
|
if field.geography or self.connection.ops.geometry:
|
|
|
|
# Geography and Geometry (PostGIS 2.0+) columns are
|
|
|
|
# created normally.
|
2014-04-21 17:25:48 +08:00
|
|
|
column_sql = super(PostGISSchemaEditor, self).column_sql(model, field, include_default)
|
2013-11-27 21:32:12 +08:00
|
|
|
else:
|
2014-04-21 17:25:48 +08:00
|
|
|
column_sql = None, None
|
2013-11-27 21:32:12 +08:00
|
|
|
# Geometry columns are created by the `AddGeometryColumn`
|
|
|
|
# stored procedure.
|
2014-04-21 17:25:48 +08:00
|
|
|
self.geometry_sql.append(
|
2013-11-27 21:32:12 +08:00
|
|
|
self.sql_add_geometry_column % {
|
|
|
|
"table": self.geo_quote_name(model._meta.db_table),
|
|
|
|
"column": self.geo_quote_name(field.column),
|
|
|
|
"srid": field.srid,
|
|
|
|
"geom_type": self.geo_quote_name(field.geom_type),
|
|
|
|
"dim": field.dim,
|
|
|
|
}
|
|
|
|
)
|
2014-04-21 17:25:48 +08:00
|
|
|
|
2013-11-27 21:32:12 +08:00
|
|
|
if not field.null:
|
2014-04-21 17:25:48 +08:00
|
|
|
self.geometry_sql.append(
|
2013-11-27 21:32:12 +08:00
|
|
|
self.sql_alter_geometry_column_not_null % {
|
|
|
|
"table": self.quote_name(model._meta.db_table),
|
|
|
|
"column": self.quote_name(field.column),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if field.spatial_index:
|
|
|
|
# Spatial indexes created the same way for both Geometry and
|
|
|
|
# Geography columns.
|
|
|
|
# PostGIS 2.0 does not support GIST_GEOMETRY_OPS. So, on 1.5
|
|
|
|
# we use GIST_GEOMETRY_OPS, on 2.0 we use either "nd" ops
|
|
|
|
# which are fast on multidimensional cases, or just plain
|
|
|
|
# gist index for the 2d case.
|
|
|
|
if field.geography:
|
|
|
|
index_ops = ''
|
|
|
|
elif self.connection.ops.geometry:
|
|
|
|
if field.dim > 2:
|
|
|
|
index_ops = self.geom_index_ops_nd
|
|
|
|
else:
|
|
|
|
index_ops = ''
|
|
|
|
else:
|
|
|
|
index_ops = self.geom_index_ops
|
2014-04-21 17:25:48 +08:00
|
|
|
self.geometry_sql.append(
|
2013-11-27 21:32:12 +08:00
|
|
|
self.sql_add_spatial_index % {
|
|
|
|
"index": self.quote_name('%s_%s_id' % (model._meta.db_table, field.column)),
|
|
|
|
"table": self.quote_name(model._meta.db_table),
|
|
|
|
"column": self.quote_name(field.column),
|
|
|
|
"index_type": self.geom_index_type,
|
|
|
|
"ops": index_ops,
|
|
|
|
}
|
|
|
|
)
|
2014-04-21 17:25:48 +08:00
|
|
|
return column_sql
|
2013-11-27 21:32:12 +08:00
|
|
|
|
2014-04-21 17:25:48 +08:00
|
|
|
def create_model(self, model):
|
|
|
|
super(PostGISSchemaEditor, self).create_model(model)
|
|
|
|
# Create geometry columns
|
|
|
|
for sql in self.geometry_sql:
|
|
|
|
self.execute(sql)
|
|
|
|
self.geometry_sql = []
|
|
|
|
|
|
|
|
def add_field(self, model, field):
|
|
|
|
super(PostGISSchemaEditor, self).add_field(model, field)
|
|
|
|
# Create geometry columns
|
|
|
|
for sql in self.geometry_sql:
|
|
|
|
self.execute(sql)
|
|
|
|
self.geometry_sql = []
|
2014-04-21 20:04:53 +08:00
|
|
|
|
|
|
|
def remove_field(self, model, field):
|
|
|
|
from django.contrib.gis.db.models.fields import GeometryField
|
|
|
|
if not isinstance(field, GeometryField) or self.connection.ops.spatial_version < (2, 0):
|
|
|
|
super(PostGISSchemaEditor, self).remove_field(model, field)
|
|
|
|
|
|
|
|
self.execute(
|
|
|
|
self.sql_drop_geometry_column % {
|
|
|
|
"table": self.geo_quote_name(model._meta.db_table),
|
|
|
|
"column": self.geo_quote_name(field.column),
|
|
|
|
}
|
|
|
|
)
|