You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

introspection.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from django.contrib.gis.gdal import OGRGeomType
  2. from django.db.backends.postgresql.introspection import DatabaseIntrospection
  3. class GeoIntrospectionError(Exception):
  4. pass
  5. class PostGISIntrospection(DatabaseIntrospection):
  6. # Reverse dictionary for PostGIS geometry types not populated until
  7. # introspection is actually performed.
  8. postgis_types_reverse = {}
  9. ignored_tables = DatabaseIntrospection.ignored_tables + [
  10. 'geography_columns',
  11. 'geometry_columns',
  12. 'raster_columns',
  13. 'spatial_ref_sys',
  14. 'raster_overviews',
  15. ]
  16. def get_postgis_types(self):
  17. """
  18. Return a dictionary with keys that are the PostgreSQL object
  19. identification integers for the PostGIS geometry and/or
  20. geography types (if supported).
  21. """
  22. field_types = [
  23. ('geometry', 'GeometryField'),
  24. # The value for the geography type is actually a tuple
  25. # to pass in the `geography=True` keyword to the field
  26. # definition.
  27. ('geography', ('GeometryField', {'geography': True})),
  28. ]
  29. postgis_types = {}
  30. # The OID integers associated with the geometry type may
  31. # be different across versions; hence, this is why we have
  32. # to query the PostgreSQL pg_type table corresponding to the
  33. # PostGIS custom data types.
  34. oid_sql = 'SELECT "oid" FROM "pg_type" WHERE "typname" = %s'
  35. with self.connection.cursor() as cursor:
  36. for field_type in field_types:
  37. cursor.execute(oid_sql, (field_type[0],))
  38. for result in cursor.fetchall():
  39. postgis_types[result[0]] = field_type[1]
  40. return postgis_types
  41. def get_field_type(self, data_type, description):
  42. if not self.postgis_types_reverse:
  43. # If the PostGIS types reverse dictionary is not populated, do so
  44. # now. In order to prevent unnecessary requests upon connection
  45. # initialization, the `data_types_reverse` dictionary is not updated
  46. # with the PostGIS custom types until introspection is actually
  47. # performed -- in other words, when this function is called.
  48. self.postgis_types_reverse = self.get_postgis_types()
  49. self.data_types_reverse.update(self.postgis_types_reverse)
  50. return super().get_field_type(data_type, description)
  51. def get_geometry_type(self, table_name, geo_col):
  52. """
  53. The geometry type OID used by PostGIS does not indicate the particular
  54. type of field that a geometry column is (e.g., whether it's a
  55. PointField or a PolygonField). Thus, this routine queries the PostGIS
  56. metadata tables to determine the geometry type.
  57. """
  58. with self.connection.cursor() as cursor:
  59. try:
  60. # First seeing if this geometry column is in the `geometry_columns`
  61. cursor.execute('SELECT "coord_dimension", "srid", "type" '
  62. 'FROM "geometry_columns" '
  63. 'WHERE "f_table_name"=%s AND "f_geometry_column"=%s',
  64. (table_name, geo_col))
  65. row = cursor.fetchone()
  66. if not row:
  67. raise GeoIntrospectionError
  68. except GeoIntrospectionError:
  69. cursor.execute('SELECT "coord_dimension", "srid", "type" '
  70. 'FROM "geography_columns" '
  71. 'WHERE "f_table_name"=%s AND "f_geography_column"=%s',
  72. (table_name, geo_col))
  73. row = cursor.fetchone()
  74. if not row:
  75. raise Exception('Could not find a geometry or geography column for "%s"."%s"' %
  76. (table_name, geo_col))
  77. # OGRGeomType does not require GDAL and makes it easy to convert
  78. # from OGC geom type name to Django field.
  79. field_type = OGRGeomType(row[2]).django
  80. # Getting any GeometryField keyword arguments that are not the default.
  81. dim = row[0]
  82. srid = row[1]
  83. field_params = {}
  84. if srid != 4326:
  85. field_params['srid'] = srid
  86. if dim != 2:
  87. field_params['dim'] = dim
  88. return field_type, field_params