2008-08-06 02:13:06 +08:00
|
|
|
from django.db import connection
|
|
|
|
from django.db.models.fields import Field # Django base Field class
|
|
|
|
from django.contrib.gis.db.backend.mysql.query import GEOM_FROM_TEXT
|
|
|
|
|
|
|
|
# Quotename & geographic quotename, respectively.
|
|
|
|
qn = connection.ops.quote_name
|
|
|
|
|
|
|
|
class MySQLGeoField(Field):
|
|
|
|
"""
|
|
|
|
The backend-specific geographic field for MySQL.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _geom_index(self, style, db_table):
|
|
|
|
"""
|
|
|
|
Creates a spatial index for the geometry column. If MyISAM tables are
|
2009-03-31 01:15:49 +08:00
|
|
|
used an R-Tree index is created, otherwise a B-Tree index is created.
|
2008-08-06 02:13:06 +08:00
|
|
|
Thus, for best spatial performance, you should use MyISAM tables
|
2009-03-31 01:15:49 +08:00
|
|
|
(which do not support transactions). For more information, see Ch.
|
2008-08-06 02:13:06 +08:00
|
|
|
16.6.1 of the MySQL 5.0 documentation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Getting the index name.
|
|
|
|
idx_name = '%s_%s_id' % (db_table, self.column)
|
2009-03-31 01:15:49 +08:00
|
|
|
|
|
|
|
sql = (style.SQL_KEYWORD('CREATE SPATIAL INDEX ') +
|
|
|
|
style.SQL_TABLE(qn(idx_name)) +
|
|
|
|
style.SQL_KEYWORD(' ON ') +
|
|
|
|
style.SQL_TABLE(qn(db_table)) + '(' +
|
|
|
|
style.SQL_FIELD(qn(self.column)) + ');')
|
2008-08-06 02:13:06 +08:00
|
|
|
return sql
|
|
|
|
|
2008-10-28 08:39:23 +08:00
|
|
|
def post_create_sql(self, style, db_table):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
Returns SQL that will be executed after the model has been
|
|
|
|
created.
|
|
|
|
"""
|
|
|
|
# Getting the geometric index for this Geometry column.
|
2009-03-31 01:15:49 +08:00
|
|
|
if self.spatial_index:
|
2008-08-06 02:13:06 +08:00
|
|
|
return (self._geom_index(style, db_table),)
|
|
|
|
else:
|
|
|
|
return ()
|
|
|
|
|
|
|
|
def db_type(self):
|
|
|
|
"The OpenGIS name is returned for the MySQL database column type."
|
2009-03-31 01:15:49 +08:00
|
|
|
return self.geom_type
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def get_placeholder(self, value):
|
|
|
|
"""
|
2009-03-31 01:15:49 +08:00
|
|
|
The placeholder here has to include MySQL's WKT constructor. Because
|
|
|
|
MySQL does not support spatial transformations, there is no need to
|
2008-08-06 02:13:06 +08:00
|
|
|
modify the placeholder based on the contents of the given value.
|
|
|
|
"""
|
|
|
|
return '%s(%%s)' % GEOM_FROM_TEXT
|