PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/db/backends/postgis/introspection.py

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