mirror of https://github.com/django/django.git
Simplified DatabaseIntrospection.get_geometry_type() for PostGIS.
This commit is contained in:
parent
e19f58fc01
commit
d82f212ec8
|
@ -2,10 +2,6 @@ from django.contrib.gis.gdal import OGRGeomType
|
||||||
from django.db.backends.postgresql.introspection import DatabaseIntrospection
|
from django.db.backends.postgresql.introspection import DatabaseIntrospection
|
||||||
|
|
||||||
|
|
||||||
class GeoIntrospectionError(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class PostGISIntrospection(DatabaseIntrospection):
|
class PostGISIntrospection(DatabaseIntrospection):
|
||||||
# Reverse dictionary for PostGIS geometry types not populated until
|
# Reverse dictionary for PostGIS geometry types not populated until
|
||||||
# introspection is actually performed.
|
# introspection is actually performed.
|
||||||
|
@ -65,33 +61,22 @@ class PostGISIntrospection(DatabaseIntrospection):
|
||||||
metadata tables to determine the geometry type.
|
metadata tables to determine the geometry type.
|
||||||
"""
|
"""
|
||||||
with self.connection.cursor() as cursor:
|
with self.connection.cursor() as cursor:
|
||||||
try:
|
cursor.execute("""
|
||||||
# First seeing if this geometry column is in the `geometry_columns`
|
SELECT t.coord_dimension, t.srid, t.type FROM (
|
||||||
cursor.execute('SELECT "coord_dimension", "srid", "type" '
|
SELECT * FROM geometry_columns
|
||||||
'FROM "geometry_columns" '
|
UNION ALL
|
||||||
'WHERE "f_table_name"=%s AND "f_geometry_column"=%s',
|
SELECT * FROM geography_columns
|
||||||
(table_name, geo_col))
|
) AS t WHERE t.f_table_name = %s AND t.f_geometry_column = %s
|
||||||
row = cursor.fetchone()
|
""", (table_name, geo_col))
|
||||||
if not row:
|
row = cursor.fetchone()
|
||||||
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:
|
if not row:
|
||||||
raise Exception('Could not find a geometry or geography column for "%s"."%s"' %
|
raise Exception('Could not find a geometry or geography column for "%s"."%s"' %
|
||||||
(table_name, geo_col))
|
(table_name, geo_col))
|
||||||
|
dim, srid, field_type = row
|
||||||
# OGRGeomType does not require GDAL and makes it easy to convert
|
# OGRGeomType does not require GDAL and makes it easy to convert
|
||||||
# from OGC geom type name to Django field.
|
# from OGC geom type name to Django field.
|
||||||
field_type = OGRGeomType(row[2]).django
|
field_type = OGRGeomType(field_type).django
|
||||||
|
|
||||||
# Getting any GeometryField keyword arguments that are not the default.
|
# Getting any GeometryField keyword arguments that are not the default.
|
||||||
dim = row[0]
|
|
||||||
srid = row[1]
|
|
||||||
field_params = {}
|
field_params = {}
|
||||||
if srid != 4326:
|
if srid != 4326:
|
||||||
field_params['srid'] = srid
|
field_params['srid'] = srid
|
||||||
|
|
Loading…
Reference in New Issue