A wrapper for the OGRSpatialReference object. According to the GDAL web site, the SpatialReference object "provide[s] services to represent coordinate systems (projections and datums) and to transform between them."
| 43 | |
| 44 | |
| 45 | class SpatialReference(GDALBase): |
| 46 | """ |
| 47 | A wrapper for the OGRSpatialReference object. According to the GDAL web |
| 48 | site, the SpatialReference object "provide[s] services to represent |
| 49 | coordinate 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: |
no outgoing calls