2015-08-05 22:08:56 +08:00
|
|
|
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
|
2013-11-27 21:32:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
class PostGISSchemaEditor(DatabaseSchemaEditor):
|
|
|
|
geom_index_type = 'GIST'
|
|
|
|
geom_index_ops_nd = 'GIST_GEOMETRY_OPS_ND'
|
2015-06-19 23:46:03 +08:00
|
|
|
rast_index_wrapper = 'ST_ConvexHull(%s)'
|
2013-11-27 21:32:12 +08:00
|
|
|
|
|
|
|
sql_add_spatial_index = "CREATE INDEX %(index)s ON %(table)s USING %(index_type)s (%(column)s %(ops)s)"
|
2014-04-23 04:49:52 +08:00
|
|
|
sql_clear_geometry_columns = "DELETE FROM geometry_columns WHERE f_table_name = %(table)s"
|
2013-11-27 21:32:12 +08:00
|
|
|
|
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):
|
2015-06-19 23:46:03 +08:00
|
|
|
from django.contrib.gis.db.models.fields import BaseSpatialField
|
|
|
|
if not isinstance(field, BaseSpatialField):
|
2014-04-21 17:25:48 +08:00
|
|
|
return super(PostGISSchemaEditor, self).column_sql(model, field, include_default)
|
2013-11-27 21:32:12 +08:00
|
|
|
|
2015-03-17 23:16:50 +08:00
|
|
|
column_sql = super(PostGISSchemaEditor, self).column_sql(model, field, include_default)
|
2013-11-27 21:32:12 +08:00
|
|
|
|
|
|
|
if field.spatial_index:
|
|
|
|
# Spatial indexes created the same way for both Geometry and
|
|
|
|
# Geography columns.
|
2015-06-19 23:46:03 +08:00
|
|
|
field_column = self.quote_name(field.column)
|
|
|
|
if field.geom_type == 'RASTER':
|
|
|
|
# For raster fields, wrap index creation SQL statement with ST_ConvexHull.
|
|
|
|
# Indexes on raster columns are based on the convex hull of the raster.
|
|
|
|
field_column = self.rast_index_wrapper % field_column
|
|
|
|
index_ops = ''
|
|
|
|
elif field.geography:
|
2013-11-27 21:32:12 +08:00
|
|
|
index_ops = ''
|
2015-03-17 23:16:50 +08:00
|
|
|
else:
|
|
|
|
# Use either "nd" ops which are fast on multidimensional cases
|
|
|
|
# or just plain gist index for the 2d case.
|
2013-11-27 21:32:12 +08:00
|
|
|
if field.dim > 2:
|
|
|
|
index_ops = self.geom_index_ops_nd
|
|
|
|
else:
|
|
|
|
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),
|
2015-06-19 23:46:03 +08:00
|
|
|
"column": field_column,
|
2013-11-27 21:32:12 +08:00
|
|
|
"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 = []
|
|
|
|
|
2014-04-23 04:49:52 +08:00
|
|
|
def delete_model(self, model):
|
|
|
|
super(PostGISSchemaEditor, self).delete_model(model)
|
|
|
|
self.execute(self.sql_clear_geometry_columns % {
|
|
|
|
"table": self.geo_quote_name(model._meta.db_table),
|
|
|
|
})
|
|
|
|
|
2014-04-21 17:25:48 +08:00
|
|
|
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 = []
|