models.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """
  2. The GeometryColumns and SpatialRefSys models for the SpatiaLite backend.
  3. """
  4. from django.db import connection, models
  5. from django.contrib.gis.db.backends.base import SpatialRefSysMixin
  6. from django.utils.encoding import python_2_unicode_compatible
  7. @python_2_unicode_compatible
  8. class SpatialiteGeometryColumns(models.Model):
  9. """
  10. The 'geometry_columns' table from SpatiaLite.
  11. """
  12. f_table_name = models.CharField(max_length=256)
  13. f_geometry_column = models.CharField(max_length=256)
  14. if connection.ops.spatial_version[0] >= 4:
  15. type = models.IntegerField(db_column='geometry_type')
  16. else:
  17. type = models.CharField(max_length=30)
  18. coord_dimension = models.IntegerField()
  19. srid = models.IntegerField(primary_key=True)
  20. spatial_index_enabled = models.IntegerField()
  21. class Meta:
  22. app_label = 'gis'
  23. db_table = 'geometry_columns'
  24. managed = False
  25. @classmethod
  26. def table_name_col(cls):
  27. """
  28. Returns the name of the metadata column used to store the feature table
  29. name.
  30. """
  31. return 'f_table_name'
  32. @classmethod
  33. def geom_col_name(cls):
  34. """
  35. Returns the name of the metadata column used to store the feature
  36. geometry column.
  37. """
  38. return 'f_geometry_column'
  39. def __str__(self):
  40. return "%s.%s - %dD %s field (SRID: %d)" % \
  41. (self.f_table_name, self.f_geometry_column,
  42. self.coord_dimension, self.type, self.srid)
  43. class SpatialiteSpatialRefSys(models.Model, SpatialRefSysMixin):
  44. """
  45. The 'spatial_ref_sys' table from SpatiaLite.
  46. """
  47. srid = models.IntegerField(primary_key=True)
  48. auth_name = models.CharField(max_length=256)
  49. auth_srid = models.IntegerField()
  50. ref_sys_name = models.CharField(max_length=256)
  51. proj4text = models.CharField(max_length=2048)
  52. if connection.ops.spatial_version[0] >= 4:
  53. srtext = models.CharField(max_length=2048)
  54. @property
  55. def wkt(self):
  56. if hasattr(self, 'srtext'):
  57. return self.srtext
  58. from django.contrib.gis.gdal import SpatialReference
  59. return SpatialReference(self.proj4text).wkt
  60. class Meta:
  61. app_label = 'gis'
  62. db_table = 'spatial_ref_sys'
  63. managed = False