def decode3(garef, center=True): '''Decode a C{garef} to lat-, longitude and precision. @param garef: To be decoded (L{Garef} 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 GARSError: Invalid B{C{garef}}, INValid, non-alphanumeric or bad length B{C{garef}}. ''' def _Error(i): return GARSError('%s invalid: %r[%s]' % ('garef', garef, i)) def _ll(chars, g, i, j, lo, hi): ll, b = 0, len(chars) for i in range(i, j): d = chars.find(g[i]) if d < 0: raise _Error(i) ll = ll * b + d if ll < lo or ll > hi: raise _Error(j) return ll def _ll2(lon, lat, g, i, m): d = _Digits.find(g[i]) if d < 1 or d > m * m: raise _Error(i) d, r = divmod(d - 1, m) lon = lon * m + r lat = lat * m + (m - 1 - d) return lon, lat g, precision = _2garstr2(garef) lon = _ll(_Digits, g, 0, _LonLen, 1, 720) + _LonOrig_M1_1 lat = _ll(_Letters, g, _LonLen, _MinLen, 0, 359) + _LatOrig_M1 if precision > 0: lon, lat = _ll2(lon, lat, g, _MinLen, _M2) if precision > 1: lon, lat = _ll2(lon, lat, g, _MinLen + 1, _M3) r = _Resolutions[precision] # == 1.0 / unit if center: lon = lon * 2 + 1 lat = lat * 2 + 1 r *= 0.5 lon *= r lat *= r return _xnamed(LatLonPrec3Tuple(lat, lon, precision), nameof(garef))
def decode3(georef, center=True): '''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}}. ''' def _digit(ll, g, i, m): d = _Digits.find(g[i]) if d < 0 or d >= m: raise _Error(i) return ll * m + d def _Error(i): return WGRSError('%s invalid: %r[%s]' % ('georef', georef, i)) def _index(chars, g, i): k = chars.find(g[i]) if k < 0: raise _Error(i) return k g, precision = _2geostr2(georef) lon = _index(_LonTile, g, 0) + _LonOrig_Tile lat = _index(_LatTile, g, 1) + _LatOrig_Tile u, p = 1.0, precision - 1 if p >= 0: lon = lon * _Tile + _index(_Degrees, g, 2) lat = lat * _Tile + _index(_Degrees, g, 3) if p > 0: m = 6 for i in range(p): lon = _digit(lon, g, _BaseLen + i, m) lat = _digit(lat, g, _BaseLen + i + p, m) u *= m m = _Base u *= _Tile if center: lon = lon * 2 + 1 lat = lat * 2 + 1 u *= 2 u = _Tile / u lon *= u lat *= u return LatLonPrec3Tuple(lat, lon, precision)
def decode3(georef, center=True): '''Decode a C{georef} to lat-, longitude and precision. @arg georef: To be decoded (L{Georef} or C{str}). @kwarg 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}}. ''' def _digit(ll, g, i, m): d = _Digits.find(g[i]) if d < 0 or d >= m: raise _Error(i) return ll * m + d def _Error(i): return WGRSError(_item_sq(georef=i), georef) def _index(chars, g, i): k = chars.find(g[i]) if k < 0: raise _Error(i) return k g, precision = _2geostr2(georef) lon = _index(_LonTile, g, 0) + _LonOrig_Tile lat = _index(_LatTile, g, 1) + _LatOrig_Tile u = 1.0 if precision > 0: lon = lon * _Tile + _index(_DegChar, g, 2) lat = lat * _Tile + _index(_DegChar, g, 3) m, p = 6, precision - 1 for i in range(_BaseLen, _BaseLen + p): lon = _digit(lon, g, i, m) lat = _digit(lat, g, i + p, m) u *= m m = _Base u *= _Tile if center: lon = lon * 2 + 1 lat = lat * 2 + 1 u *= 2 u = _Tile / u r = LatLonPrec3Tuple(Lat(lat * u, Error=WGRSError), Lon(lon * u, Error=WGRSError), precision) return _xnamed(r, nameof(georef))