| 1 |
from django.contrib.gis.gdal import OGRGeomType |
| 2 |
from django.db.backends.sqlite3.introspection import ( |
| 3 |
DatabaseIntrospection, |
| 4 |
FlexibleFieldLookupDict, |
| 5 |
) |
| 6 |
|
| 7 |
|
| 8 |
class GeoFlexibleFieldLookupDict(FlexibleFieldLookupDict): |
| 9 |
""" |
| 10 |
Subclass that includes updates the `base_data_types_reverse` dict |
| 11 |
for geometry field types. |
| 12 |
""" |
| 13 |
|
| 14 |
base_data_types_reverse = { |
| 15 |
**FlexibleFieldLookupDict.base_data_types_reverse, |
| 16 |
"point": "GeometryField", |
| 17 |
"linestring": "GeometryField", |
| 18 |
"polygon": "GeometryField", |
| 19 |
"multipoint": "GeometryField", |
| 20 |
"multilinestring": "GeometryField", |
| 21 |
"multipolygon": "GeometryField", |
| 22 |
"geometrycollection": "GeometryField", |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
class SpatiaLiteIntrospection(DatabaseIntrospection): |
| 27 |
data_types_reverse = GeoFlexibleFieldLookupDict() |
| 28 |
|
| 29 |
def get_geometry_type(self, table_name, description): |
| 30 |
with self.connection.cursor() as cursor: |
| 31 |
# Querying the `geometry_columns` table to get additional metadata. |
| 32 |
cursor.execute( |
| 33 |
"SELECT coord_dimension, srid, geometry_type " |
| 34 |
"FROM geometry_columns " |
| 35 |
"WHERE f_table_name=%s AND f_geometry_column=%s", |
| 36 |
(table_name, description.name), |
| 37 |
) |
| 38 |
row = cursor.fetchone() |
| 39 |
if not row: |
| 40 |
raise Exception( |
| 41 |
'Could not find a geometry column for "%s"."%s"' |
| 42 |
% (table_name, description.name) |
| 43 |
) |
| 44 |
|
| 45 |
# OGRGeomType does not require GDAL and makes it easy to convert |
| 46 |
# from OGC geom type name to Django field. |
| 47 |
ogr_type = row[2] |
| 48 |
if isinstance(ogr_type, int) and ogr_type > 1000: |
| 49 |
# SpatiaLite uses SFSQL 1.2 offsets 1000 (Z), 2000 (M), and |
| 50 |
# 3000 (ZM) to indicate the presence of higher dimensional |
| 51 |
# coordinates (M not yet supported by Django). |
| 52 |
ogr_type = ogr_type % 1000 + OGRGeomType.wkb25bit |
| 53 |
field_type = OGRGeomType(ogr_type).django |
| 54 |
|
| 55 |
# Getting any GeometryField keyword arguments that are not the default. |
| 56 |
dim = row[0] |
| 57 |
srid = row[1] |
| 58 |
field_params = {} |
| 59 |
if srid != 4326: |
| 60 |
field_params["srid"] = srid |
| 61 |
if (isinstance(dim, str) and "Z" in dim) or dim == 3: |
| 62 |
field_params["dim"] = 3 |
| 63 |
return field_type, field_params |
| 64 |
|
| 65 |
def get_constraints(self, cursor, table_name): |
| 66 |
constraints = super().get_constraints(cursor, table_name) |
| 67 |
cursor.execute( |
| 68 |
"SELECT f_geometry_column " |
| 69 |
"FROM geometry_columns " |
| 70 |
"WHERE f_table_name=%s AND spatial_index_enabled=1", |
| 71 |
(table_name,), |
| 72 |
) |
| 73 |
for row in cursor.fetchall(): |
| 74 |
constraints["%s__spatial__index" % row[0]] = { |
| 75 |
"columns": [row[0]], |
| 76 |
"primary_key": False, |
| 77 |
"unique": False, |
| 78 |
"foreign_key": None, |
| 79 |
"check": False, |
| 80 |
"index": True, |
| 81 |
} |
| 82 |
return constraints |