| 1 |
from django.contrib.gis.gdal import OGRGeomType |
| 2 |
from django.db.backends.postgresql.introspection import DatabaseIntrospection |
| 3 |
|
| 4 |
|
| 5 |
class PostGISIntrospection(DatabaseIntrospection): |
| 6 |
postgis_oid_lookup = {} # Populated when introspection is performed. |
| 7 |
|
| 8 |
ignored_tables = DatabaseIntrospection.ignored_tables + [ |
| 9 |
"geography_columns", |
| 10 |
"geometry_columns", |
| 11 |
"raster_columns", |
| 12 |
"spatial_ref_sys", |
| 13 |
"raster_overviews", |
| 14 |
] |
| 15 |
|
| 16 |
def get_field_type(self, data_type, description): |
| 17 |
if not self.postgis_oid_lookup: |
| 18 |
# Query PostgreSQL's pg_type table to determine the OID integers |
| 19 |
# for the PostGIS data types used in reverse lookup (the integers |
| 20 |
# may be different across versions). To prevent unnecessary |
| 21 |
# requests upon connection initialization, the `data_types_reverse` |
| 22 |
# dictionary isn't updated until introspection is performed here. |
| 23 |
with self.connection.cursor() as cursor: |
| 24 |
cursor.execute( |
| 25 |
"SELECT oid, typname " |
| 26 |
"FROM pg_type " |
| 27 |
"WHERE typname IN ('geometry', 'geography')" |
| 28 |
) |
| 29 |
self.postgis_oid_lookup = dict(cursor.fetchall()) |
| 30 |
self.data_types_reverse.update( |
| 31 |
(oid, "GeometryField") for oid in self.postgis_oid_lookup |
| 32 |
) |
| 33 |
return super().get_field_type(data_type, description) |
| 34 |
|
| 35 |
def get_geometry_type(self, table_name, description): |
| 36 |
""" |
| 37 |
The geometry type OID used by PostGIS does not indicate the particular |
| 38 |
type of field that a geometry column is (e.g., whether it's a |
| 39 |
PointField or a PolygonField). Thus, this routine queries the PostGIS |
| 40 |
metadata tables to determine the geometry type. |
| 41 |
""" |
| 42 |
with self.connection.cursor() as cursor: |
| 43 |
cursor.execute( |
| 44 |
""" |
| 45 |
SELECT t.coord_dimension, t.srid, t.type FROM ( |
| 46 |
SELECT * FROM geometry_columns |
| 47 |
UNION ALL |
| 48 |
SELECT * FROM geography_columns |
| 49 |
) AS t WHERE t.f_table_name = %s AND t.f_geometry_column = %s |
| 50 |
""", |
| 51 |
(table_name, description.name), |
| 52 |
) |
| 53 |
row = cursor.fetchone() |
| 54 |
if not row: |
| 55 |
raise Exception( |
| 56 |
'Could not find a geometry or geography column for "%s"."%s"' |
| 57 |
% (table_name, description.name) |
| 58 |
) |
| 59 |
dim, srid, field_type = row |
| 60 |
# OGRGeomType does not require GDAL and makes it easy to convert |
| 61 |
# from OGC geom type name to Django field. |
| 62 |
field_type = OGRGeomType(field_type).django |
| 63 |
# Getting any GeometryField keyword arguments that are not the default. |
| 64 |
field_params = {} |
| 65 |
if self.postgis_oid_lookup.get(description.type_code) == "geography": |
| 66 |
field_params["geography"] = True |
| 67 |
if srid != 4326: |
| 68 |
field_params["srid"] = srid |
| 69 |
if dim != 2: |
| 70 |
field_params["dim"] = dim |
| 71 |
return field_type, field_params |