| 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 |
|---|
| 4 |
|
|---|
| 5 |
# Quotename & geographic quotename, respectively. |
|---|
| 6 |
qn = connection.ops.quote_name |
|---|
| 7 |
|
|---|
| 8 |
class MySQLGeoField(Field): |
|---|
| 9 |
""" |
|---|
| 10 |
The backend-specific geographic field for MySQL. |
|---|
| 11 |
""" |
|---|
| 12 |
|
|---|
| 13 |
def _geom_index(self, style, db_table): |
|---|
| 14 |
""" |
|---|
| 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. |
|---|
| 20 |
""" |
|---|
| 21 |
|
|---|
| 22 |
# Getting the index name. |
|---|
| 23 |
idx_name = '%s_%s_id' % (db_table, self.column) |
|---|
| 24 |
|
|---|
| 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)) + ');' |
|---|
| 30 |
return sql |
|---|
| 31 |
|
|---|
| 32 |
def _post_create_sql(self, style, db_table): |
|---|
| 33 |
""" |
|---|
| 34 |
Returns SQL that will be executed after the model has been |
|---|
| 35 |
created. |
|---|
| 36 |
""" |
|---|
| 37 |
# Getting the geometric index for this Geometry column. |
|---|
| 38 |
if self._index: |
|---|
| 39 |
return (self._geom_index(style, db_table),) |
|---|
| 40 |
else: |
|---|
| 41 |
return () |
|---|
| 42 |
|
|---|
| 43 |
def db_type(self): |
|---|
| 44 |
"The OpenGIS name is returned for the MySQL database column type." |
|---|
| 45 |
return self._geom |
|---|
| 46 |
|
|---|
| 47 |
def get_placeholder(self, value): |
|---|
| 48 |
""" |
|---|
| 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. |
|---|
| 52 |
""" |
|---|
| 53 |
return '%s(%%s)' % GEOM_FROM_TEXT |
|---|