Coverage for pygeodesy/wgrs.py : 85%

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
# -*- coding: utf-8 -*-
decode and inspect I{World Geographic Reference System (WGRS)} references.
Transcribed from C++ class U{Georef <https://GeographicLib.SourceForge.io/html/classGeographicLib_1_1Georef.html>} by I{Charles Karney}, but with modified C{precision} and extended with C{height} and C{radius}. See also U{World Geographic Reference System <https://WikiPedia.org/wiki/World_Geographic_Reference_System>}.
@newfield example: Example, Examples '''
LatLonPrec5Tuple, _NamedStr _Strs
# all public contants, classes and functions 'encode', 'precision', 'resolution')
i -= 1
'''(INTERNAL) Convert lat, lon, height. '''
'''(INTERNAL) Check or create a L{Georef} instance. ''' if not isinstance(georef, Georef): try: georef = Georef(georef) except (TypeError, ValueError): raise _IsNotError(Georef.__name__, str.__name__, 'LatLon', georef=georef) return georef
'''(INTERNAL) Check a georef string. ''' or geostr[:3] == 'INV' \ or not geostr.isalnum(): raise ValueError except (AttributeError, TypeError, ValueError): raise WGRSError('%s: %r[%s]' % (Georef.__name__, georef, len(georef)))
'''World Geographic Reference System (WGRS) encode, decode or other L{Georef} issue. '''
'''Georef class, a C{_NamedStr}. '''
# no str.__init__ in Python 3 '''New L{Georef} from an other L{Georef} instance or georef C{str} or from a C{LatLon} instance or lat-/longitude C{str}.
@param cll: Cell or location (L{Georef} or C{str}, C{LatLon} or C{str}). @keyword precision: Optional, the desired georef resolution and length (C{int} 0..11), see function L{wgrs.encode} for more details. @keyword name: Optional name (C{str}).
@return: New L{Georef}.
@raise RangeError: Invalid B{C{cll}} lat- or longitude.
@raise TypeError: Invalid B{C{cll}}.
@raise WGRSError: INValid or non-alphanumeric B{C{cll}}. '''
self._height = h else:
else: # assume LatLon try: lat, lon, h = _2fllh(cll.lat, cll.lon) h = getattr(cll, 'height', h) except AttributeError: raise _IsNotError('valid', **{Georef.__name__: cll}) g = encode(lat, lon, precision=precision, height=h) # PYCHOK false self = str.__new__(cls, g) self._latlon = LatLon2Tuple(lat, lon) self._precision = precision if h not in (None, _MISSING): self._height = h
self.name = name
# cache all decoded attrs
def height(self): '''Get this georef's height in C{meter} or C{None} if missing. '''
def latlon(self): '''Get this georef's (center) lat- and longitude (L{LatLon2Tuple}). ''' self._decode()
def precision(self): '''Get this georef's precision (C{int}). ''' if self._precision is None: self._decode() return self._precision
def radius(self): '''Get this georef's radius in C{meter} or C{None} if missing. ''' if self._radius is _MISSING: self._decode() return self._radius
'''Return (the center of) this georef cell as an instance of the supplied C{LatLon} class.
@param LatLon: Class to use (C{LatLon}). @keyword height: Optional height ({meter}). @keyword kwds: Optional keyword arguments for B{C{LatLon}}.
@return: This georef location (B{C{LatLon}}).
@raise WGRSError: Invalid B{C{LatLon}}. ''' raise WGRSError('%s invalid: %r' % ('LatLon', LatLon))
'''Decode a C{georef} to lat-, longitude and precision.
@param georef: To be decoded (L{Georef} or C{str}). @keyword center: If C{True} the center, otherwise the south-west, lower-left corner (C{bool}).
@return: A L{LatLonPrec3Tuple}C{(lat, lon, precision)}.
@raise WGRSError: Invalid B{C{georef}}, INValid, non-alphanumeric or odd length B{C{georef}}. ''' raise _Error(i)
return WGRSError('%s invalid: %r[%s]' % ('georef', georef, i))
raise _Error(i)
'''Decode a C{georef} to lat-, longitude, precision, height and radius.
@param georef: To be decoded (L{Georef} or C{str}). @keyword center: If C{True} the center, otherwise the south-west, lower-left corner (C{bool}).
@return: A L{LatLonPrec5Tuple}C{(lat, lon, precision, height, radius)} where C{height} and/or C{radius} are C{None} if missing.
@raise WGRSError: Invalid B{C{georef}}, INValid, non-alphanumeric or odd length B{C{georef}}. '''
except (IndexError, ValueError): raise WGRSError('%s invalid: %r' % (name, georef))
except (TypeError, ValueError): raise WGRSError('%s invalid: %r' % ('georef', georef))
'''Encode a lat-/longitude as a C{georef} of the given precision.
@param lat: Latitude (C{degrees}). @param lon: Longitude (C{degrees}). @keyword precision: Optional, the desired C{georef} resolution and length (C{int} 0..11). @keyword height: Optional, height in C{meter}, see U{Designation of area <https://WikiPedia.org/wiki/World_Geographic_Reference_System>}. @keyword radius: Optional, radius in C{meter}, see U{Designation of area <https://WikiPedia.org/wiki/World_Geographic_Reference_System>}.
@return: The C{georef} (C{str}).
@raise RangeError: Invalid B{C{lat}} or B{C{lon}}.
@raise WGRSError: Invalid B{C{precision}}, B{C{height}} or B{C{radius}}.
@note: The B{C{precision}} value differs from U{Georef<https:// GeographicLib.SourceForge.io/html/classGeographicLib_1_1Georef.html>}. The C{georef} length is M{2 * (precision + 1)} and the C{georef} resolution is I{15°} for B{C{precision}} 0, I{1°} for 1, I{1′} for 2, I{0.1′} for 3, I{0.01′} for 4, ... M{10**(2 - precision)}. ''' raise ValueError except (TypeError, ValueError): raise WGRSError('%s invalid: %r' % (name, m))
raise ValueError except (TypeError, ValueError): raise WGRSError('%s invalid: %r' % ('precision', precision))
lat *= EPS1_2
'''Determine the L{Georef} precision to meet a required (geographic) resolution.
@param res: The required resolution (C{degrees}).
@return: The L{Georef} precision (C{int} 0..11).
@see: Function L{wgrs.encode} for more C{precision} details. '''
'''Determine the (geographic) resolution of a given L{Georef} precision.
@param prec: The given precision (C{int}).
@return: The (geographic) resolution (C{degrees}).
@see: Function L{wgrs.encode} for more C{precision} details. ''' else:
# **) MIT License # # Copyright (C) 2016-2020 -- 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. |