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