Exemple #1
0
 def _json(ngs):
     '''(INTERNAL) Convert an NGS response in JSON to a C{dict}.
     '''
     # b'{"geoidModel": "GEOID12A",
     #    "station": "UserStation",
     #    "lat": 37.8816,
     #    "latDms": "N375253.76000",
     #    "lon": -121.9142,
     #    "lonDms": "W1215451.12000",
     #    "geoidHeight": -31.703,
     #    "error": 0.064
     #   }'
     #
     # or in case of errors:
     #
     # b'{"error": "No suitable Geoid model found for model 15"
     #   }'
     d = {}
     for t in ngs.strip().lstrip('{').rstrip('}').split(_COMMA_):
         t = t.strip()
         j = t.strip(_QUOTE2_).split('": ')
         if len(j) != 2:
             raise ParseError(json=t)
         k, v = j
         try:
             v = float(v)
         except (TypeError, ValueError):
             v = Str(ub2str(v.lstrip().lstrip(_QUOTE2_)), name=k)
         d[k] = v
     return d
Exemple #2
0
    def __new__(cls, cll, precision=3, name=NN):
        '''New L{Georef} from an other L{Georef} instance or georef
           C{str} or from a C{LatLon} instance or lat-/longitude C{str}.

           @arg cll: Cell or location (L{Georef} or C{str}, C{LatLon}
                     or C{str}).
           @kwarg precision: Optional, the desired georef resolution
                             and length (C{int} 0..11), see function
                             L{wgrs.encode} for more details.
           @kwarg 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}}.
        '''
        h = None

        if isinstance(cll, Georef):
            g, p = _2geostr2(str(cll))
            self = Str.__new__(cls, g)
            self._latlon = LatLon2Tuple(*cll._latlon)
            self._precision = p  # cll._precision
            if cll._name:
                self._name = cll._name

        elif isstr(cll):
            if ',' in cll:
                lat, lon, h = _2fllh(*parse3llh(cll, height=None))
                g = encode(lat, lon, precision=precision,
                           height=h)  # PYCHOK false
                self = Str.__new__(cls, g)
                self._latlon = LatLon2Tuple(lat, lon)
            else:
                self = Str.__new__(cls, cll.upper())
                self._decode()

        else:  # assume LatLon
            try:
                lat, lon, h = _2fllh(cll.lat, cll.lon)
                h = getattr(cll, _height_, h)
            except AttributeError:
                raise _xStrError(Georef, cll=cll)  # Error=WGRSError
            g = encode(lat, lon, precision=precision, height=h)  # PYCHOK false
            self = Str.__new__(cls, g)
            self._latlon = LatLon2Tuple(lat, lon)

        if h not in (None, MISSING):
            self._height = Height(h)
        if self._precision is None:
            self._precision = _2Precision(precision)

        if name:
            self.name = name
        return self
Exemple #3
0
    def __new__(cls, cll, precision=1, name=NN):
        '''New L{Garef} from an other L{Garef} instance or garef
           C{str} or from a C{LatLon} instance or lat-/longitude C{str}.

           @arg cll: Cell or location (L{Garef} or C{str}, C{LatLon}
                     or C{str}).
           @kwarg precision: Optional, the desired garef resolution
                             and length (C{int} 0..2), see function
                             L{gars.encode} for more details.
           @kwarg name: Optional name (C{str}).

           @return: New L{Garef}.

           @raise RangeError: Invalid B{C{cll}} lat- or longitude.

           @raise TypeError: Invalid B{C{cll}}.

           @raise GARSError: INValid or non-alphanumeric B{C{cll}}.
        '''
        if isinstance(cll, Garef):
            g, p = _2garstr2(str(cll))
            self = Str.__new__(cls, g)
            self._latlon = LatLon2Tuple(*cll._latlon)
            self._name = cll._name
            self._precision = p  # cll._precision

        elif isstr(cll):
            if ',' in cll:
                lat, lon = _2fll(*parse3llh(cll))
                cll = encode(lat, lon, precision=precision)  # PYCHOK false
                self = Str.__new__(cls, cll)
                self._latlon = LatLon2Tuple(lat, lon)
            else:
                self = Str.__new__(cls, cll.upper())
                self._decode()

        else:  # assume LatLon
            try:
                lat, lon = _2fll(cll.lat, cll.lon)
            except AttributeError:
                raise _xStrError(Garef, cll=cll)  # Error=GARSError
            cll = encode(lat, lon, precision=precision)  # PYCHOK false
            self = Str.__new__(cls, cll)
            self._latlon = LatLon2Tuple(lat, lon)

        if self._precision is None:
            self._precision = _2Precision(precision)

        if name:
            self.name = name
        return self
Exemple #4
0
def decode5(georef, center=True):
    '''Decode a C{georef} to lat-, longitude, precision, height and radius.

       @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{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}}.
    '''
    def _h2m(kft, name):
        return Height(ft2m(kft * _1000_0), name=name, Error=WGRSError)

    def _r2m(NM, name):
        return Radius(NM / m2NM(1), name=name, Error=WGRSError)

    def _split2(g, name, _2m):
        i = max(g.find(name[0]), g.rfind(name[0]))
        if i > _BaseLen:
            return g[:i], _2m(int(g[i + 1:]), _SPACE_(georef, name))
        else:
            return g, None

    g = Str(georef, Error=WGRSError)

    g, h = _split2(g, _Height_, _h2m)  # H is last
    g, r = _split2(g, _Radius_, _r2m)  # R before H

    return decode3(g, center=center).to5Tuple(h, r)
Exemple #5
0
    def __new__(cls, cll, precision=None, name=NN):
        '''New L{Geohash} from an other L{Geohash} instance or C{str}
           or from a C{LatLon} instance or C{str}.

           @arg cll: Cell or location (L{Geohash} or C{str}, C{LatLon}
                     or C{str}).
           @kwarg precision: Optional, the desired geohash length (C{int}
                             1..12), see function L{geohash.encode} for
                             some examples.
           @kwarg name: Optional name (C{str}).

           @return: New L{Geohash}.

           @raise TypeError: Invalid B{C{cll}}.

           @raise GeohashError: INValid or non-alphanumeric B{C{cll}}.
        '''
        if isinstance(cll, Geohash):
            gh = _2geostr(str(cll))
            self = Str.__new__(cls, gh)

        elif isstr(cll):
            if ',' in cll:
                lat, lon = _2fll(*parse3llh(cll))
                gh = encode(lat, lon, precision=precision)
                self = Str.__new__(cls, gh)
                self._latlon = lat, lon
            else:
                gh = _2geostr(cll)
                self = Str.__new__(cls, gh)

        else:  # assume LatLon
            try:
                lat, lon = _2fll(cll.lat, cll.lon)
            except AttributeError:
                raise _xStrError(Geohash, cll=cll)  # Error=GeohashError
            gh = encode(lat, lon, precision=precision)
            self = Str.__new__(cls, gh)
            self._latlon = lat, lon

        if name:
            self.name = name
        return self
Exemple #6
0
def _xml(tag, xml):
    '''(INTERNAL) Get a <tag>value</tag> from XML.
    '''
    # b'<?xml version="1.0" encoding="utf-8" ?>
    #   <USGS_Elevation_Point_Query_Service>
    #    <Elevation_Query x="-121.914200" y="37.881600">
    #     <Data_Source>3DEP 1/3 arc-second</Data_Source>
    #     <Elevation>3851.03</Elevation>
    #     <Units>Feet</Units>
    #    </Elevation_Query>
    #   </USGS_Elevation_Point_Query_Service>'
    i = xml.find(Fmt.TAG(tag))
    if i > 0:
        i += len(tag) + 2
        j = xml.find(Fmt.TAGEND(tag), i)
        if j > i:
            return Str(xml[i:j].strip(), name=tag)
    return _no_(_XML_, Fmt.TAG(tag))