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

https://code.google.com/p/mango-py/ · Python · 51 lines · 37 code · 6 blank · 8 comment · 6 complexity · b6777ad2ac79a17b196703495afd9211 MD5 · raw file

  1. from django.contrib.gis.gdal import OGRGeomType
  2. from django.db.backends.sqlite3.introspection import DatabaseIntrospection, FlexibleFieldLookupDict
  3. class GeoFlexibleFieldLookupDict(FlexibleFieldLookupDict):
  4. """
  5. Sublcass that includes updates the `base_data_types_reverse` dict
  6. for geometry field types.
  7. """
  8. base_data_types_reverse = FlexibleFieldLookupDict.base_data_types_reverse.copy()
  9. base_data_types_reverse.update(
  10. {'point' : 'GeometryField',
  11. 'linestring' : 'GeometryField',
  12. 'polygon' : 'GeometryField',
  13. 'multipoint' : 'GeometryField',
  14. 'multilinestring' : 'GeometryField',
  15. 'multipolygon' : 'GeometryField',
  16. 'geometrycollection' : 'GeometryField',
  17. })
  18. class SpatiaLiteIntrospection(DatabaseIntrospection):
  19. data_types_reverse = GeoFlexibleFieldLookupDict()
  20. def get_geometry_type(self, table_name, geo_col):
  21. cursor = self.connection.cursor()
  22. try:
  23. # Querying the `geometry_columns` table to get additional metadata.
  24. cursor.execute('SELECT "coord_dimension", "srid", "type" '
  25. 'FROM "geometry_columns" '
  26. 'WHERE "f_table_name"=%s AND "f_geometry_column"=%s',
  27. (table_name, geo_col))
  28. row = cursor.fetchone()
  29. if not row:
  30. raise Exception('Could not find a geometry column for "%s"."%s"' %
  31. (table_name, geo_col))
  32. # OGRGeomType does not require GDAL and makes it easy to convert
  33. # from OGC geom type name to Django field.
  34. field_type = OGRGeomType(row[2]).django
  35. # Getting any GeometryField keyword arguments that are not the default.
  36. dim = row[0]
  37. srid = row[1]
  38. field_params = {}
  39. if srid != 4326:
  40. field_params['srid'] = srid
  41. if isinstance(dim, basestring) and 'Z' in dim:
  42. field_params['dim'] = 3
  43. finally:
  44. cursor.close()
  45. return field_type, field_params