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.

models.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from django.contrib.gis import gdal
  2. class SpatialRefSysMixin:
  3. """
  4. The SpatialRefSysMixin is a class used by the database-dependent
  5. SpatialRefSys objects to reduce redundant code.
  6. """
  7. @property
  8. def srs(self):
  9. """
  10. Return a GDAL SpatialReference object.
  11. """
  12. # TODO: Is caching really necessary here? Is complexity worth it?
  13. if hasattr(self, '_srs'):
  14. # Returning a clone of the cached SpatialReference object.
  15. return self._srs.clone()
  16. else:
  17. # Attempting to cache a SpatialReference object.
  18. # Trying to get from WKT first.
  19. try:
  20. self._srs = gdal.SpatialReference(self.wkt)
  21. return self.srs
  22. except Exception as e:
  23. msg = e
  24. try:
  25. self._srs = gdal.SpatialReference(self.proj4text)
  26. return self.srs
  27. except Exception as e:
  28. msg = e
  29. raise Exception('Could not get OSR SpatialReference from WKT: %s\nError:\n%s' % (self.wkt, msg))
  30. @property
  31. def ellipsoid(self):
  32. """
  33. Return a tuple of the ellipsoid parameters:
  34. (semimajor axis, semiminor axis, and inverse flattening).
  35. """
  36. return self.srs.ellipsoid
  37. @property
  38. def name(self):
  39. "Return the projection name."
  40. return self.srs.name
  41. @property
  42. def spheroid(self):
  43. "Return the spheroid name for this spatial reference."
  44. return self.srs['spheroid']
  45. @property
  46. def datum(self):
  47. "Return the datum for this spatial reference."
  48. return self.srs['datum']
  49. @property
  50. def projected(self):
  51. "Is this Spatial Reference projected?"
  52. return self.srs.projected
  53. @property
  54. def local(self):
  55. "Is this Spatial Reference local?"
  56. return self.srs.local
  57. @property
  58. def geographic(self):
  59. "Is this Spatial Reference geographic?"
  60. return self.srs.geographic
  61. @property
  62. def linear_name(self):
  63. "Return the linear units name."
  64. return self.srs.linear_name
  65. @property
  66. def linear_units(self):
  67. "Return the linear units."
  68. return self.srs.linear_units
  69. @property
  70. def angular_name(self):
  71. "Return the name of the angular units."
  72. return self.srs.angular_name
  73. @property
  74. def angular_units(self):
  75. "Return the angular units."
  76. return self.srs.angular_units
  77. @property
  78. def units(self):
  79. "Return a tuple of the units and the name."
  80. if self.projected or self.local:
  81. return (self.linear_units, self.linear_name)
  82. elif self.geographic:
  83. return (self.angular_units, self.angular_name)
  84. else:
  85. return (None, None)
  86. @classmethod
  87. def get_units(cls, wkt):
  88. """
  89. Return a tuple of (unit_value, unit_name) for the given WKT without
  90. using any of the database fields.
  91. """
  92. return gdal.SpatialReference(wkt).units
  93. @classmethod
  94. def get_spheroid(cls, wkt, string=True):
  95. """
  96. Class method used by GeometryField on initialization to
  97. retrieve the `SPHEROID[..]` parameters from the given WKT.
  98. """
  99. srs = gdal.SpatialReference(wkt)
  100. sphere_params = srs.ellipsoid
  101. sphere_name = srs['spheroid']
  102. if not string:
  103. return sphere_name, sphere_params
  104. else:
  105. # `string` parameter used to place in format acceptable by PostGIS
  106. if len(sphere_params) == 3:
  107. radius, flattening = sphere_params[0], sphere_params[2]
  108. else:
  109. radius, flattening = sphere_params
  110. return 'SPHEROID["%s",%s,%s]' % (sphere_name, radius, flattening)
  111. def __str__(self):
  112. """
  113. Return the string representation, a 'pretty' OGC WKT.
  114. """
  115. return str(self.srs)