summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Pope <nick.pope@flightdataservices.com>2019-01-19 15:09:44 +0000
committerTim Graham <timograham@gmail.com>2019-01-22 18:23:37 -0500
commitd82f212ec8a55aaeb9d1fe893b902c648d0e0065 (patch)
tree50b2686db56ff901349038da4dcc9bb9b2c74f7e
parente19f58fc01a9326c28071f2dee3089bf50cd5ea8 (diff)
Simplified DatabaseIntrospection.get_geometry_type() for PostGIS.
-rw-r--r--django/contrib/gis/db/backends/postgis/introspection.py35
1 files changed, 10 insertions, 25 deletions
diff --git a/django/contrib/gis/db/backends/postgis/introspection.py b/django/contrib/gis/db/backends/postgis/introspection.py
index 9b4458efea..82455d4c18 100644
--- a/django/contrib/gis/db/backends/postgis/introspection.py
+++ b/django/contrib/gis/db/backends/postgis/introspection.py
@@ -2,10 +2,6 @@ from django.contrib.gis.gdal import OGRGeomType
from django.db.backends.postgresql.introspection import DatabaseIntrospection
-class GeoIntrospectionError(Exception):
- pass
-
-
class PostGISIntrospection(DatabaseIntrospection):
# Reverse dictionary for PostGIS geometry types not populated until
# introspection is actually performed.
@@ -65,33 +61,22 @@ class PostGISIntrospection(DatabaseIntrospection):
metadata tables to determine the geometry type.
"""
with self.connection.cursor() as cursor:
- try:
- # First seeing if this geometry column is in the `geometry_columns`
- 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()
-
+ cursor.execute("""
+ SELECT t.coord_dimension, t.srid, t.type FROM (
+ SELECT * FROM geometry_columns
+ UNION ALL
+ SELECT * FROM geography_columns
+ ) AS t WHERE t.f_table_name = %s AND t.f_geometry_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))
-
+ dim, srid, field_type = row
# OGRGeomType does not require GDAL and makes it easy to convert
# 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.
- dim = row[0]
- srid = row[1]
field_params = {}
if srid != 4326:
field_params['srid'] = srid