|
29
|
1 |
from django.contrib.gis.gdal import OGRGeomType |
|
|
2 |
from django.db.backends.sqlite3.introspection import DatabaseIntrospection, FlexibleFieldLookupDict |
|
|
3 |
|
|
|
4 |
class GeoFlexibleFieldLookupDict(FlexibleFieldLookupDict): |
|
|
5 |
""" |
|
|
6 |
Sublcass that includes updates the `base_data_types_reverse` dict |
|
|
7 |
for geometry field types. |
|
|
8 |
""" |
|
|
9 |
base_data_types_reverse = FlexibleFieldLookupDict.base_data_types_reverse.copy() |
|
|
10 |
base_data_types_reverse.update( |
|
|
11 |
{'point' : 'GeometryField', |
|
|
12 |
'linestring' : 'GeometryField', |
|
|
13 |
'polygon' : 'GeometryField', |
|
|
14 |
'multipoint' : 'GeometryField', |
|
|
15 |
'multilinestring' : 'GeometryField', |
|
|
16 |
'multipolygon' : 'GeometryField', |
|
|
17 |
'geometrycollection' : 'GeometryField', |
|
|
18 |
}) |
|
|
19 |
|
|
|
20 |
class SpatiaLiteIntrospection(DatabaseIntrospection): |
|
|
21 |
data_types_reverse = GeoFlexibleFieldLookupDict() |
|
|
22 |
|
|
|
23 |
def get_geometry_type(self, table_name, geo_col): |
|
|
24 |
cursor = self.connection.cursor() |
|
|
25 |
try: |
|
|
26 |
# Querying the `geometry_columns` table to get additional metadata. |
|
|
27 |
cursor.execute('SELECT "coord_dimension", "srid", "type" ' |
|
|
28 |
'FROM "geometry_columns" ' |
|
|
29 |
'WHERE "f_table_name"=%s AND "f_geometry_column"=%s', |
|
|
30 |
(table_name, geo_col)) |
|
|
31 |
row = cursor.fetchone() |
|
|
32 |
if not row: |
|
|
33 |
raise Exception('Could not find a geometry column for "%s"."%s"' % |
|
|
34 |
(table_name, geo_col)) |
|
|
35 |
|
|
|
36 |
# OGRGeomType does not require GDAL and makes it easy to convert |
|
|
37 |
# from OGC geom type name to Django field. |
|
|
38 |
field_type = OGRGeomType(row[2]).django |
|
|
39 |
|
|
|
40 |
# Getting any GeometryField keyword arguments that are not the default. |
|
|
41 |
dim = row[0] |
|
|
42 |
srid = row[1] |
|
|
43 |
field_params = {} |
|
|
44 |
if srid != 4326: |
|
|
45 |
field_params['srid'] = srid |
|
|
46 |
if isinstance(dim, basestring) and 'Z' in dim: |
|
|
47 |
field_params['dim'] = 3 |
|
|
48 |
finally: |
|
|
49 |
cursor.close() |
|
|
50 |
|
|
|
51 |
return field_type, field_params |