123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- from django.contrib.gis.gdal import OGRGeomType
- from django.db.backends.postgresql.introspection import DatabaseIntrospection
- class GeoIntrospectionError(Exception):
- pass
- class PostGISIntrospection(DatabaseIntrospection):
-
-
- postgis_types_reverse = {}
- ignored_tables = DatabaseIntrospection.ignored_tables + [
- 'geography_columns',
- 'geometry_columns',
- 'raster_columns',
- 'spatial_ref_sys',
- 'raster_overviews',
- ]
-
-
-
-
- _get_indexes_query = """
- SELECT DISTINCT attr.attname, idx.indkey, idx.indisunique, idx.indisprimary
- FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index idx,
- pg_catalog.pg_attribute attr, pg_catalog.pg_type t
- WHERE
- c.oid = idx.indrelid
- AND idx.indexrelid = c2.oid
- AND attr.attrelid = c.oid
- AND t.oid = attr.atttypid
- AND (
- attr.attnum = idx.indkey[0] OR
- (t.typname LIKE 'raster' AND idx.indkey = '0')
- )
- AND attr.attnum > 0
- AND c.relname = %s"""
- def get_postgis_types(self):
- """
- Return a dictionary with keys that are the PostgreSQL object
- identification integers for the PostGIS geometry and/or
- geography types (if supported).
- """
- field_types = [
- ('geometry', 'GeometryField'),
-
-
-
- ('geography', ('GeometryField', {'geography': True})),
- ]
- postgis_types = {}
-
-
-
-
- oid_sql = 'SELECT "oid" FROM "pg_type" WHERE "typname" = %s'
- with self.connection.cursor() as cursor:
- for field_type in field_types:
- cursor.execute(oid_sql, (field_type[0],))
- for result in cursor.fetchall():
- postgis_types[result[0]] = field_type[1]
- return postgis_types
- def get_field_type(self, data_type, description):
- if not self.postgis_types_reverse:
-
-
-
-
-
- self.postgis_types_reverse = self.get_postgis_types()
- self.data_types_reverse.update(self.postgis_types_reverse)
- return super().get_field_type(data_type, description)
- def get_geometry_type(self, table_name, geo_col):
- """
- The geometry type OID used by PostGIS does not indicate the particular
- type of field that a geometry column is (e.g., whether it's a
- PointField or a PolygonField). Thus, this routine queries the PostGIS
- metadata tables to determine the geometry type.
- """
- with self.connection.cursor() as cursor:
- try:
-
- cursor.execute('SELECT "coord_dimension", "srid", "type" '
- 'FROM "geometry_columns" '
- 'WHERE "f_table_name"=%s AND "f_geometry_column"=%s',
- (table_name, geo_col))
- row = cursor.fetchone()
- if not row:
- raise GeoIntrospectionError
- except GeoIntrospectionError:
- cursor.execute('SELECT "coord_dimension", "srid", "type" '
- 'FROM "geography_columns" '
- 'WHERE "f_table_name"=%s AND "f_geography_column"=%s',
- (table_name, geo_col))
- row = cursor.fetchone()
- if not row:
- raise Exception('Could not find a geometry or geography column for "%s"."%s"' %
- (table_name, geo_col))
-
-
- field_type = OGRGeomType(row[2]).django
-
- dim = row[0]
- srid = row[1]
- field_params = {}
- if srid != 4326:
- field_params['srid'] = srid
- if dim != 2:
- field_params['dim'] = dim
- return field_type, field_params
|