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.mysql.query import GEOM_FROM_TEXT
5
# Quotename & geographic quotename, respectively.
6
qn = connection.ops.quote_name
8
class MySQLGeoField(Field):
10
The backend-specific geographic field for MySQL.
13
def _geom_index(self, style, db_table):
15
Creates a spatial index for the geometry column. If MyISAM tables are
16
used an R-Tree index is created, otherwise a B-Tree index is created.
17
Thus, for best spatial performance, you should use MyISAM tables
18
(which do not support transactions). For more information, see Ch.
19
16.6.1 of the MySQL 5.0 documentation.
22
# Getting the index name.
23
idx_name = '%s_%s_id' % (db_table, self.column)
25
sql = style.SQL_KEYWORD('CREATE SPATIAL INDEX ') + \
26
style.SQL_TABLE(qn(idx_name)) + \
27
style.SQL_KEYWORD(' ON ') + \
28
style.SQL_TABLE(qn(db_table)) + '(' + \
29
style.SQL_FIELD(qn(self.column)) + ');'
32
def _post_create_sql(self, style, db_table):
34
Returns SQL that will be executed after the model has been
37
# Getting the geometric index for this Geometry column.
39
return (self._geom_index(style, db_table),)
44
"The OpenGIS name is returned for the MySQL database column type."
47
def get_placeholder(self, value):
49
The placeholder here has to include MySQL's WKT constructor. Because
50
MySQL does not support spatial transformations, there is no need to
51
modify the placeholder based on the contents of the given value.
53
return '%s(%%s)' % GEOM_FROM_TEXT