# Coverage for pygeodesy/epsg.py : 98%

## 101 statements 99 run 2 missing 7 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | # -*- coding: utf-8 -*- u'''European Petroleum Survey Group (EPSG) en-/decoding. Classes L{Epsg} and L{EPSGError} and functions to L{encode} and L{decode2} (U{EPSG<https://www.EPSG-Registry.org>}) codes from and to U{UTM <https://WikiPedia.org/wiki/Universal_Transverse_Mercator_coordinate_system>} and U{UPS<https://WikiPedia.org/wiki/Universal_polar_stereographic_coordinate_system>} zones. A pure Python implementation transcoded from I{Charles Karney}'s C++ class U{UTMUPS<https://GeographicLib.SourceForge.io/html/classGeographicLib_1_1UTMUPS.html>}, including coverage of UPS as zone C{0}. ''' from pygeodesy.basics import isint, isstr, _xinstanceof from pygeodesy.errors import _ValueError from pygeodesy.interns import NN, _N_, _NS_, _S_, _SPACE_ from pygeodesy.lazily import _ALL_LAZY, _ALL_OTHER from pygeodesy.namedTuples import UtmUps2Tuple from pygeodesy.props import Property_RO from pygeodesy.streprs import Fmt from pygeodesy.units import Int from pygeodesy.ups import Ups from pygeodesy.utm import Utm from pygeodesy.utmupsBase import _to3zBhp, _UPS_ZONE, _UTM_ZONE_MIN, \ _UTM_ZONE_MAX, _UTMUPS_ZONE_INVALID __all__ = _ALL_LAZY.epsg __version__ = '21.11.08' # _EPSG_INVALID = _UTMUPS_ZONE_INVALID _EPSG_N_01 = 32601 # EPSG code for UTM zone 01 N _EPSG_N_60 = 32660 # EPSG code for UTM zone 60 N _EPSG_N = 32661 # EPSG code for UPS pole N _EPSG_S_01 = 32701 # EPSG code for UTM zone 01 S _EPSG_S_60 = 32760 # EPSG code for UTM zone 60 S _EPSG_S = 32761 # EPSG code for UPS pole S class Epsg(Int): '''U{EPSG<https://www.EPSG-Registry.org>} class, a named C{int}. ''' _band = NN _epsg = None _hemisphere = NN _utmups = None _zone = _UTMUPS_ZONE_INVALID def __new__(cls, eisu, name=NN): '''New L{Epsg} (I{European Petroleum Survey Group}) code from a UTM/USP coordinate or other EPSG code. @arg eisu: Other code (L{Epsg}, C{int}, C{str}, L{Utm} or L{Ups}). @return: New L{Epsg}. @raise TypeError: Invalid B{C{eisu}}. @raise EPSGError: Invalid B{C{eisu}}. ''' if isinstance(eisu, Epsg): self = int.__new__(cls, int(eisu)) self._band = eisu.band self._epsg = self # XXX eisu self._hemisphere = eisu.hemisphere self._utmups = eisu.utmups self._zone = eisu.zone if eisu.name: self.name = eisu.name elif isint(eisu): self = int.__new__(cls, eisu) self._epsg = eisu self._zone, self._hemisphere = decode2(eisu) # PYCHOK UtmUps2Tuple elif isstr(eisu): self = encode(eisu) else: u = eisu _xinstanceof(Utm, Ups, eisu=u) self = encode(u.zone, hemipole=u.hemisphere, band=u.band) # PYCHOK **kwds self._utmups = u if u.name: self.name = u.name if name: self.name = name return self def __repr__(self): return Fmt.PAREN(self.named, int.__repr__(self)) def __str__(self): return int.__str__(self) @Property_RO def band(self): '''Get the I{latitudinal} UTM or I{polar} UPS Band (C{'A'|'B'|'C'|'D'|..|'W'|'X'|'Y'|'Z'} or C{""}). ''' return self._band @Property_RO def hemisphere(self): '''Get the UTM/UPS hemisphere/-pole (C{'N'|'S'}). ''' return self._hemisphere @Property_RO def utmups(self): '''Get the UTM/UPS original (L{Utm}, L{Ups}). ''' return self._utmups def utmupsStr(self, B=False): '''Get the UTM/UPS zone, band and hemisphere/-pole (C{str}). ''' b = self.band if B else NN h = s = self.hemisphere if h: s = _SPACE_ return NN(Fmt.zone(self.zone), b, s, h) @Property_RO def zone(self): '''Get the (longitudinal) UTM/UPS zone (C{int}, C{1..60} for UTM, C{0} for UPS). ''' return self._zone class EPSGError(_ValueError): '''EPSG encode, decode or other L{Epsg} issue. ''' pass def decode2(epsg): '''Determine the UTM/USP zone and hemisphere from a given U{EPSG<https://www.EPSG-Registry.org>}. @arg epsg: The EPSG (L{Epsg}, C{str} or C{scalar}). @return: A L{UtmUps2Tuple}C{(zone, hemipole)}. @raise EPSGError: Invalid B{C{epsg}}. @note: Coverage of UPS as zone C{0} follows I{Karney}'s function U{UTMUPS::DecodeEPSG <https://GeographicLib.SourceForge.io/html/classGeographicLib_1_1UTMUPS.html>}. ''' if isinstance(epsg, Epsg): z, h = epsg.zone, epsg.hemisphere else: try: e = int(epsg) # int(long) OK if _EPSG_N_01 <= e <= _EPSG_N_60: z, h = int(e - _EPSG_N_01 + _UTM_ZONE_MIN), _N_ elif _EPSG_S_01 <= e <= _EPSG_S_60: z, h = int(e - _EPSG_S_01 + _UTM_ZONE_MIN), _S_ elif e == _EPSG_N: z, h = _UPS_ZONE, _N_ elif e == _EPSG_S: z, h = _UPS_ZONE, _S_ else: raise ValueError except (TypeError, ValueError) as x: raise EPSGError(epsg=epsg, txt=str(x)) return UtmUps2Tuple(z, h) def encode(zone, hemipole=NN, band=NN): '''Determine the U{EPSG<https://www.EPSG-Registry.org>} code for a given UTM/UPS zone number, hemisphere/pole and/or Band. @arg zone: The (longitudinal) UTM zone (C{int}, 1..60) or UPS zone (C{int}, 0) or UTM zone with/-out I{latitudinal} Band letter (C{str}, '01C'..'60X') or UPS zone with/-out I{polar} Band letter (C{str}, '00A', '00B', '00Y' or '00Z'). @kwarg hemipole: UTM/UPS hemisphere or UPS projection top/center pole (C{str}, C{'N[orth]'} or C{'S[outh]'}). @kwarg band: Optional I{latitudinal} UTM or I{polar} UPS Band letter (C{str}). @return: C{EPSG} code (L{Epsg}). @raise EPSGError: Invalid B{C{zone}}, B{C{hemipole}} or B{C{band}}. @note: Coverage of UPS as zone C{0} follows I{Karney}'s function U{UTMUPS::EncodeEPSG <https://GeographicLib.SourceForge.io/html/classGeographicLib_1_1UTMUPS.html>}. ''' try: z, B, hp = _to3zBhp(zone, band, hemipole=hemipole) # in .utmupsBase if hp not in _NS_: raise ValueError except (TypeError, ValueError) as x: raise EPSGError(zone=zone, hemipole=hemipole, band=band, txt=str(x)) if _UTM_ZONE_MIN <= z <= _UTM_ZONE_MAX: e = z - _UTM_ZONE_MIN + (_EPSG_N_01 if hp == _N_ else _EPSG_S_01) elif z == _UPS_ZONE: e = _EPSG_N if hp == _N_ else _EPSG_S else: raise EPSGError(zone=zone) e = Epsg(e) e._band = B # e._hemisphere = hp return e __all__ += _ALL_OTHER(decode2, encode) # **) MIT License # # Copyright (C) 2016-2022 -- mrJean1 at Gmail -- All Rights Reserved. # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE.