| 1 |
""" |
| 2 |
The Spatial Reference class, represents OGR Spatial Reference objects. |
| 3 |
|
| 4 |
Example: |
| 5 |
>>> from django.contrib.gis.gdal import SpatialReference |
| 6 |
>>> srs = SpatialReference('WGS84') |
| 7 |
>>> print(srs) |
| 8 |
GEOGCS["WGS 84", |
| 9 |
DATUM["WGS_1984", |
| 10 |
SPHEROID["WGS 84",6378137,298.257223563, |
| 11 |
AUTHORITY["EPSG","7030"]], |
| 12 |
TOWGS84[0,0,0,0,0,0,0], |
| 13 |
AUTHORITY["EPSG","6326"]], |
| 14 |
PRIMEM["Greenwich",0, |
| 15 |
AUTHORITY["EPSG","8901"]], |
| 16 |
UNIT["degree",0.01745329251994328, |
| 17 |
AUTHORITY["EPSG","9122"]], |
| 18 |
AUTHORITY["EPSG","4326"]] |
| 19 |
>>> print(srs.proj) |
| 20 |
+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs |
| 21 |
>>> print(srs.ellipsoid) |
| 22 |
(6378137.0, 6356752.3142451793, 298.25722356300003) |
| 23 |
>>> print(srs.projected, srs.geographic) |
| 24 |
False True |
| 25 |
>>> srs.import_epsg(32140) |
| 26 |
>>> print(srs.name) |
| 27 |
NAD83 / Texas South Central |
| 28 |
""" |
| 29 |
|
| 30 |
from ctypes import byref, c_char_p, c_int |
| 31 |
from enum import IntEnum |
| 32 |
from types import NoneType |
| 33 |
|
| 34 |
from django.contrib.gis.gdal.base import GDALBase |
| 35 |
from django.contrib.gis.gdal.error import SRSException |
| 36 |
from django.contrib.gis.gdal.prototypes import srs as capi |
| 37 |
from django.utils.encoding import force_bytes, force_str |
| 38 |
|
| 39 |
|
| 40 |
class AxisOrder(IntEnum): |
| 41 |
TRADITIONAL = 0 |
| 42 |
AUTHORITY = 1 |
| 43 |
|
| 44 |
|
| 45 |
class SpatialReference(GDALBase): |
| 46 |
""" |
| 47 |
A wrapper for the OGRSpatialReference object. According to the GDAL web site, |
| 48 |
the SpatialReference object "provide[s] services to represent coordinate |
| 49 |
systems (projections and datums) and to transform between them." |
| 50 |
""" |
| 51 |
|
| 52 |
destructor = capi.release_srs |
| 53 |
|
| 54 |
def __init__(self, srs_input="", srs_type="user", axis_order=None): |
| 55 |
""" |
| 56 |
Create a GDAL OSR Spatial Reference object from the given input. |
| 57 |
The input may be string of OGC Well Known Text (WKT), an integer |
| 58 |
EPSG code, a PROJ string, and/or a projection "well known" shorthand |
| 59 |
string (one of 'WGS84', 'WGS72', 'NAD27', 'NAD83'). |
| 60 |
""" |
| 61 |
if not isinstance(axis_order, (NoneType, AxisOrder)): |
| 62 |
raise ValueError( |
| 63 |
"SpatialReference.axis_order must be an AxisOrder instance." |
| 64 |
) |
| 65 |
self.axis_order = axis_order or AxisOrder.TRADITIONAL |
| 66 |
if srs_type == "wkt": |
| 67 |
self.ptr = capi.new_srs(c_char_p(b"")) |
| 68 |
self.import_wkt(srs_input) |
| 69 |
if self.axis_order == AxisOrder.TRADITIONAL: |
| 70 |
capi.set_axis_strategy(self.ptr, self.axis_order) |
| 71 |
return |
| 72 |
elif isinstance(srs_input, str): |
| 73 |
try: |
| 74 |
# If SRID is a string, e.g., '4326', then make acceptable |
| 75 |
# as user input. |
| 76 |
srid = int(srs_input) |
| 77 |
srs_input = "EPSG:%d" % srid |
| 78 |
except ValueError: |
| 79 |
pass |
| 80 |
elif isinstance(srs_input, int): |
| 81 |
# EPSG integer code was input. |
| 82 |
srs_type = "epsg" |
| 83 |
elif isinstance(srs_input, self.ptr_type): |
| 84 |
srs = srs_input |
| 85 |
srs_type = "ogr" |
| 86 |
else: |
| 87 |
raise TypeError('Invalid SRS type "%s"' % srs_type) |
| 88 |
|
| 89 |
if srs_type == "ogr": |
| 90 |
# Input is already an SRS pointer. |
| 91 |
srs = srs_input |
| 92 |
else: |
| 93 |
# Creating a new SRS pointer, using the string buffer. |
| 94 |
buf = c_char_p(b"") |
| 95 |
srs = capi.new_srs(buf) |
| 96 |
|
| 97 |
# If the pointer is NULL, throw an exception. |
| 98 |
if not srs: |
| 99 |
raise SRSException( |
| 100 |
"Could not create spatial reference from: %s" % srs_input |
| 101 |
) |
| 102 |
else: |
| 103 |
self.ptr = srs |
| 104 |
|
| 105 |
if self.axis_order == AxisOrder.TRADITIONAL: |
| 106 |
capi.set_axis_strategy(self.ptr, self.axis_order) |
| 107 |
# Importing from either the user input string or an integer SRID. |
| 108 |
if srs_type == "user": |
| 109 |
self.import_user_input(srs_input) |
| 110 |
elif srs_type == "epsg": |
| 111 |
self.import_epsg(srs_input) |
| 112 |
|
| 113 |
def __getitem__(self, target): |
| 114 |
""" |
| 115 |
Return the value of the given string attribute node, None if the node |
| 116 |
doesn't exist. Can also take a tuple as a parameter, (target, child), |
| 117 |
where child is the index of the attribute in the WKT. For example: |
| 118 |
|
| 119 |
>>> wkt = 'GEOGCS["WGS 84", DATUM["WGS_1984, ... AUTHORITY["EPSG","4326"]]' |
| 120 |
>>> srs = SpatialReference(wkt) # could also use 'WGS84', or 4326 |
| 121 |
>>> print(srs['GEOGCS']) |
| 122 |
WGS 84 |
| 123 |
>>> print(srs['DATUM']) |
| 124 |
WGS_1984 |
| 125 |
>>> print(srs['AUTHORITY']) |
| 126 |
EPSG |
| 127 |
>>> print(srs['AUTHORITY', 1]) # The authority value |
| 128 |
4326 |
| 129 |
>>> print(srs['TOWGS84', 4]) # the fourth value in this wkt |
| 130 |
0 |
| 131 |
>>> # For the units authority, have to use the pipe symbole. |
| 132 |
>>> print(srs['UNIT|AUTHORITY']) |
| 133 |
EPSG |
| 134 |
>>> print(srs['UNIT|AUTHORITY', 1]) # The authority value for the units |
| 135 |
9122 |
| 136 |
""" |
| 137 |
if isinstance(target, tuple): |
| 138 |
return self.attr_value(*target) |
| 139 |
else: |
| 140 |
return self.attr_value(target) |
| 141 |
|
| 142 |
def __str__(self): |
| 143 |
"Use 'pretty' WKT." |
| 144 |
return self.pretty_wkt |
| 145 |
|
| 146 |
# #### SpatialReference Methods #### |
| 147 |
def attr_value(self, target, index=0): |
| 148 |
""" |
| 149 |
The attribute value for the given target node (e.g. 'PROJCS'). The index |
| 150 |
keyword specifies an index of the child node to return. |
| 151 |
""" |
| 152 |
if not isinstance(target, str) or not isinstance(index, int): |
| 153 |
raise TypeError |
| 154 |
return capi.get_attr_value(self.ptr, force_bytes(target), index) |
| 155 |
|
| 156 |
def auth_name(self, target): |
| 157 |
"Return the authority name for the given string target node." |
| 158 |
return capi.get_auth_name( |
| 159 |
self.ptr, target if target is None else force_bytes(target) |
| 160 |
) |
| 161 |
|
| 162 |
def auth_code(self, target): |
| 163 |
"Return the authority code for the given string target node." |
| 164 |
return capi.get_auth_code( |
| 165 |
self.ptr, target if target is None else force_bytes(target) |
| 166 |
) |
| 167 |
|
| 168 |
def clone(self): |
| 169 |
"Return a clone of this SpatialReference object." |
| 170 |
return SpatialReference(capi.clone_srs(self.ptr), axis_order=self.axis_order) |
| 171 |
|
| 172 |
def from_esri(self): |
| 173 |
"Morph this SpatialReference from ESRI's format to EPSG." |
| 174 |
capi.morph_from_esri(self.ptr) |
| 175 |
|
| 176 |
def identify_epsg(self): |
| 177 |
""" |
| 178 |
This method inspects the WKT of this SpatialReference, and will |
| 179 |
add EPSG authority nodes where an EPSG identifier is applicable. |
| 180 |
""" |
| 181 |
capi.identify_epsg(self.ptr) |
| 182 |
|
| 183 |
def to_esri(self): |
| 184 |
"Morph this SpatialReference to ESRI's format." |
| 185 |
capi.morph_to_esri(self.ptr) |
| 186 |
|
| 187 |
def validate(self): |
| 188 |
"Check to see if the given spatial reference is valid." |
| 189 |
capi.srs_validate(self.ptr) |
| 190 |
|
| 191 |
# #### Name & SRID properties #### |
| 192 |
@property |
| 193 |
def name(self): |
| 194 |
"Return the name of this Spatial Reference." |
| 195 |
if self.projected: |
| 196 |
return self.attr_value("PROJCS") |
| 197 |
elif self.geographic: |
| 198 |
return self.attr_value("GEOGCS") |
| 199 |
elif self.local: |
| 200 |
return self.attr_value("LOCAL_CS") |
| 201 |
else: |
| 202 |
return None |
| 203 |
|
| 204 |
@property |
| 205 |
def srid(self): |
| 206 |
"Return the SRID of top-level authority, or None if undefined." |
| 207 |
try: |
| 208 |
return int(self.auth_code(target=None)) |
| 209 |
except (TypeError, ValueError): |
| 210 |
return None |
| 211 |
|
| 212 |
# #### Unit Properties #### |
| 213 |
@property |
| 214 |
def linear_name(self): |
| 215 |
"Return the name of the linear units." |
| 216 |
units, name = capi.linear_units(self.ptr, byref(c_char_p())) |
| 217 |
return name |
| 218 |
|
| 219 |
@property |
| 220 |
def linear_units(self): |
| 221 |
"Return the value of the linear units." |
| 222 |
units, name = capi.linear_units(self.ptr, byref(c_char_p())) |
| 223 |
return units |
| 224 |
|
| 225 |
@property |
| 226 |
def angular_name(self): |
| 227 |
"Return the name of the angular units." |
| 228 |
units, name = capi.angular_units(self.ptr, byref(c_char_p())) |
| 229 |
return name |
| 230 |
|
| 231 |
@property |
| 232 |
def angular_units(self): |
| 233 |
"Return the value of the angular units." |
| 234 |
units, name = capi.angular_units(self.ptr, byref(c_char_p())) |
| 235 |
return units |
| 236 |
|
| 237 |
@property |
| 238 |
def units(self): |
| 239 |
""" |
| 240 |
Return a 2-tuple of the units value and the units name. Automatically |
| 241 |
determine whether to return the linear or angular units. |
| 242 |
""" |
| 243 |
units, name = None, None |
| 244 |
if self.projected or self.local: |
| 245 |
units, name = capi.linear_units(self.ptr, byref(c_char_p())) |
| 246 |
elif self.geographic: |
| 247 |
units, name = capi.angular_units(self.ptr, byref(c_char_p())) |
| 248 |
if name is not None: |
| 249 |
name = force_str(name) |
| 250 |
return (units, name) |
| 251 |
|
| 252 |
# #### Spheroid/Ellipsoid Properties #### |
| 253 |
@property |
| 254 |
def ellipsoid(self): |
| 255 |
""" |
| 256 |
Return a tuple of the ellipsoid parameters: |
| 257 |
(semimajor axis, semiminor axis, and inverse flattening) |
| 258 |
""" |
| 259 |
return (self.semi_major, self.semi_minor, self.inverse_flattening) |
| 260 |
|
| 261 |
@property |
| 262 |
def semi_major(self): |
| 263 |
"Return the Semi Major Axis for this Spatial Reference." |
| 264 |
return capi.semi_major(self.ptr, byref(c_int())) |
| 265 |
|
| 266 |
@property |
| 267 |
def semi_minor(self): |
| 268 |
"Return the Semi Minor Axis for this Spatial Reference." |
| 269 |
return capi.semi_minor(self.ptr, byref(c_int())) |
| 270 |
|
| 271 |
@property |
| 272 |
def inverse_flattening(self): |
| 273 |
"Return the Inverse Flattening for this Spatial Reference." |
| 274 |
return capi.invflattening(self.ptr, byref(c_int())) |
| 275 |
|
| 276 |
# #### Boolean Properties #### |
| 277 |
@property |
| 278 |
def geographic(self): |
| 279 |
""" |
| 280 |
Return True if this SpatialReference is geographic |
| 281 |
(root node is GEOGCS). |
| 282 |
""" |
| 283 |
return bool(capi.isgeographic(self.ptr)) |
| 284 |
|
| 285 |
@property |
| 286 |
def local(self): |
| 287 |
"Return True if this SpatialReference is local (root node is LOCAL_CS)." |
| 288 |
return bool(capi.islocal(self.ptr)) |
| 289 |
|
| 290 |
@property |
| 291 |
def projected(self): |
| 292 |
""" |
| 293 |
Return True if this SpatialReference is a projected coordinate system |
| 294 |
(root node is PROJCS). |
| 295 |
""" |
| 296 |
return bool(capi.isprojected(self.ptr)) |
| 297 |
|
| 298 |
# #### Import Routines ##### |
| 299 |
def import_epsg(self, epsg): |
| 300 |
"Import the Spatial Reference from the EPSG code (an integer)." |
| 301 |
capi.from_epsg(self.ptr, epsg) |
| 302 |
|
| 303 |
def import_proj(self, proj): |
| 304 |
"""Import the Spatial Reference from a PROJ string.""" |
| 305 |
capi.from_proj(self.ptr, proj) |
| 306 |
|
| 307 |
def import_user_input(self, user_input): |
| 308 |
"Import the Spatial Reference from the given user input string." |
| 309 |
capi.from_user_input(self.ptr, force_bytes(user_input)) |
| 310 |
|
| 311 |
def import_wkt(self, wkt): |
| 312 |
"Import the Spatial Reference from OGC WKT (string)" |
| 313 |
capi.from_wkt(self.ptr, byref(c_char_p(force_bytes(wkt)))) |
| 314 |
|
| 315 |
def import_xml(self, xml): |
| 316 |
"Import the Spatial Reference from an XML string." |
| 317 |
capi.from_xml(self.ptr, xml) |
| 318 |
|
| 319 |
# #### Export Properties #### |
| 320 |
@property |
| 321 |
def wkt(self): |
| 322 |
"Return the WKT representation of this Spatial Reference." |
| 323 |
return capi.to_wkt(self.ptr, byref(c_char_p())) |
| 324 |
|
| 325 |
@property |
| 326 |
def pretty_wkt(self, simplify=0): |
| 327 |
"Return the 'pretty' representation of the WKT." |
| 328 |
return capi.to_pretty_wkt(self.ptr, byref(c_char_p()), simplify) |
| 329 |
|
| 330 |
@property |
| 331 |
def proj(self): |
| 332 |
"""Return the PROJ representation for this Spatial Reference.""" |
| 333 |
return capi.to_proj(self.ptr, byref(c_char_p())) |
| 334 |
|
| 335 |
@property |
| 336 |
def proj4(self): |
| 337 |
"Alias for proj()." |
| 338 |
return self.proj |
| 339 |
|
| 340 |
@property |
| 341 |
def xml(self, dialect=""): |
| 342 |
"Return the XML representation of this Spatial Reference." |
| 343 |
return capi.to_xml(self.ptr, byref(c_char_p()), force_bytes(dialect)) |
| 344 |
|
| 345 |
|
| 346 |
class CoordTransform(GDALBase): |
| 347 |
"The coordinate system transformation object." |
| 348 |
|
| 349 |
destructor = capi.destroy_ct |
| 350 |
|
| 351 |
def __init__(self, source, target): |
| 352 |
"Initialize on a source and target SpatialReference objects." |
| 353 |
if not isinstance(source, SpatialReference) or not isinstance( |
| 354 |
target, SpatialReference |
| 355 |
): |
| 356 |
raise TypeError("source and target must be of type SpatialReference") |
| 357 |
self.ptr = capi.new_ct(source._ptr, target._ptr) |
| 358 |
self._srs1_name = source.name |
| 359 |
self._srs2_name = target.name |
| 360 |
|
| 361 |
def __str__(self): |
| 362 |
return 'Transform from "%s" to "%s"' % (self._srs1_name, self._srs2_name) |