Exemplo n.º 1
0
 def _height(self, lats, lons, Error=HeightError):
     if isscalar(lats) and isscalar(lons):
         llis = LatLon_(lats, lons)
     else:
         n, lats = len2(lats)
         m, lons = len2(lons)
         if n != m:
             raise Error('non-matching %s: %s vs %s' % ('len', n, m))
         llis = [LatLon_(*ll) for ll in zip(lats, lons)]
     return self(llis)  # __call__(lli) or __call__(llis)
Exemplo n.º 2
0
    def __init__(self, points, tolerance, radius, shortest,
                                          indices, **options):
        '''New state.
        '''
        n, self.pts = len2(points)
        if n > 0:
            self.n = n
            self.r = {0: True, n-1: True}  # dict to avoid duplicates

        if indices:
            self.indices = True

        if radius:
            self.radius = float(radius)
        if self.radius < self.eps:
            raise ValueError('%s too small: %.6e' % ('radius', radius))

        if options:
            self.options = options

        # tolerance converted to degrees squared
        self.s2 = degrees(tolerance / self.radius)**2
        if min(self.s2, tolerance) < self.eps:
            raise ValueError('%s too small: %.6e' % ('tolerance', tolerance))
        self.s2e = self.s2 + 1  # sentinel

        # compute either the shortest or perpendicular distance
        self.d2i = self.d2iS if shortest else self.d2iP  # PYCHOK false
Exemplo n.º 3
0
def polygon(points, closed=True, base=None):
    '''Check a polygon given as an array, list, sequence, set or
       tuple of points.

       @param points: The points of the polygon (I{LatLon}[])
       @keyword closed: Optionally, treat polygon as closed and remove
                        any duplicate or closing final I{points} (bool).
       @keyword base: Optional I{points} base class (None).

       @return: 2-Tuple (number, sequence) of points (int, sequence).

       @raise TypeError: Some I{points} are not I{LatLon}.

       @raise ValueError: Insufficient number of I{points}.
    '''
    n, points = len2(points)

    if closed:
        # remove duplicate or closing final points
        while n > 1 and (points[n - 1] == points[0]
                         or points[n - 1] == points[n - 2]):
            n -= 1
        # XXX following line is unneeded if points
        # are always indexed as ... i in range(n)
        points = points[:n]  # XXX numpy.array slice is a view!

    if n < (3 if closed else 1):
        raise ValueError('too few points: %s' % (n, ))

    if base and not (isNumpy2(points) or isTuple2(points)):
        for i in range(n):
            base.others(points[i], name='points[%s]' % (i, ))

    return n, points
Exemplo n.º 4
0
    def clip(self, points, inull, closed):  # MCCABE 19, clip points
        np, self._points = len2(points)
        if np > 0:  # initial points, opened
            p = self._points[0]
            while np > 1 and _eq(self._points[np - 1], p):
                np -= 1
        if np < 3:
            raise ValueError('too few %s: %s' % ('points', np))

        no = ni = True  # all out- or inside?
        for e in self.clips():
            # clip the points, closed
            d2, p2 = self.dot2(np - 1)
            for i in range(np):
                d1, p1 = d2, p2
                d2, p2 = self.dot2(i)
                if d1 < 0:  # p1 inside, ...
                    # self.append(p1, False, e)
                    if d2 < 0:  # ... p2 inside
                        self.append(p2, inull)
                    else:  # ... p2 outside
                        p = self.intersect(p1, p2, e)
                        self.append(p, inull)
                        if d2 > 0:
                            no = False
                elif d2 < 0:  # p1 out-, p2 inside
                    p = self.intersect(p1, p2, e)
                    self.append(p, inull)
                    self.append(p2, inull)
                    if d1 > 0:
                        no = ni = False
                elif d1 > 0:
                    ni = False
            if self._clipped:  # replace points
                self._points = self._clipped
                self._clipped = []
                np = len(self._points)

        # no is True iff all points are on or at one
        # side (left or right) of each clip edge,
        # ni is True iff all points are on or on the
        # right side (i.e. inside) of all clip edges
        if no and not ni:
            self._points = ()
            np = 0
        elif np > 1:
            p = self._points[0]
            if closed:  # close clipped polygon
                if _neq(self._points[np - 1], p):
                    self._points.append(p)
                    np += 1
            elif not inull:  # open clipped polygon
                while np > 0 and _eq(self._points[np - 1], p):
                    np -= 1
        return np
Exemplo n.º 5
0
    def __init__(self, knots, weight=None):
        '''New L{HeightLSQBiSpline} interpolator.

           @param knots: The points with known height (C{LatLon}s).
           @keyword weight: Optional weight or weights for each I{knot}
                            (C{scalar} or C{scalar}s).

           @raise HeightError: Insufficient number of I{knots} or
                               I{weight}s or invalid I{knot} or I{weight}.

           @raise ImportError: Package C{numpy} or C{scipy} not found
                               or not installed.

           @raise SciPyError: A C{LSQSphereBivariateSpline} issue.

           @raise SciPyWarning: A C{LSQSphereBivariateSpline} warning
                                as exception..
        '''
        np, spi = self._NumSciPy()

        xs, ys, hs = self._xyhs3(knots)
        m = len(hs)

        if not weight:
            w = None  # default
        elif isscalar(weight):
            w = float(weight)
            if w <= 0:
                raise HeightError('invalid %s: %.6f' % ('weight', w))
        else:
            n, w = len2(weight)
            if n != m:
                raise HeightError('invalid %s: %s, not %s' %
                                  ('number of weights', n, m))
            w = np.array(map(float, w))
            for i in range(m):
                if w[i] <= 0:
                    raise HeightError('invalid %s[%s]: %.6f' %
                                      ('weight', i, w[i]))

        T = 1.0e-4  # like SciPy example
        ps = np.array(_ordedup(xs, T, PI2 - T))
        ts = np.array(_ordedup(ys, T, PI - T))

        try:
            self._ev = spi.LSQSphereBivariateSpline(ys,
                                                    xs,
                                                    hs,
                                                    ts,
                                                    ps,
                                                    eps=EPS,
                                                    w=w).ev
        except Exception as x:
            raise _SciPyIssue(x)
Exemplo n.º 6
0
 def __init__(self, corners):
     n = ''
     try:
         n, cs = len2(corners)
         if n == 2:  # make a box
             b, l, t, r = boundsOf(cs, wrap=False)
             cs = LL_(b, l), LL_(t, l), LL_(t, r), LL_(b, r)
         n, cs = points2(cs, closed=True)
         self._corners = cs = cs[:n]
         self._nc = n
         self._cw = 1 if isclockwise(cs, adjust=False, wrap=False) else -1
         if self._cw != isconvex_(cs, adjust=False, wrap=False):
             raise ValueError
     except ValueError:
         raise ValueError('invalid %s[%s]: %r' % ('corners', n, corners))
     self._clipped = self._points = []
Exemplo n.º 7
0
def sumOf(vectors, Vector=Vector3d, **kwds):
    '''Compute the vectorial sum of several vectors.

       @param vectors: Vectors to be added (L{Vector3d}[]).
       @keyword Vector: Optional class for the vectorial sum (L{Vector3d}).
       @keyword kwds: Optional, additional I{Vector} keyword arguments.

       @return: Vectorial sum (I{Vector}).

       @raise ValueError: No I{vectors}.
    '''
    n, vectors = len2(vectors)
    if n < 1:
        raise ValueError('no vectors: %r' & (n, ))
    return Vector(fsum(v.x for v in vectors), fsum(v.y for v in vectors),
                  fsum(v.z for v in vectors), **kwds)
Exemplo n.º 8
0
def sumOf(nvectors, Vector=Nvector, h=None, **kwds):
    '''Return the vectorial sum of two or more n-vectors.

       @param nvectors: Vectors to be added (L{Nvector}[]).
       @keyword Vector: Optional class for the vectorial sum (L{Nvector}).
       @keyword kwds: Optional, additional B{C{Vector}} keyword arguments.
       @keyword h: Optional height, overriding the mean height (C{meter}).

       @return: Vectorial sum (B{C{Vector}}).

       @raise ValueError: No B{C{nvectors}}.
    '''
    n, nvectors = len2(nvectors)
    if n < 1:
        raise ValueError('no nvectors: %r' & (n, ))

    if h is None:
        h = fsum(v.h for v in nvectors) / float(n)
    return _sumOf(nvectors, Vector=Vector, h=h, **kwds)
Exemplo n.º 9
0
def _allis2(llis, m=1, Error=HeightError):  # imported by .geoids
    # dtermine return type and convert lli C{LatLon}s to list
    if not isinstance(llis, tuple):  # llis are *args
        raise AssertionError('type(%s): %r' % ('*llis', llis))

    n = len(llis)
    if n == 1:  # convert single lli to 1-item list
        llis = llis[0]
        try:
            n, llis = len2(llis)
            _as = _alist  # return list of interpolated heights
        except TypeError:  # single lli
            n, llis = 1, [llis]
            _as = _ascalar  # return single interpolated heights
    else:  # of 0, 2 or more llis
        _as = _atuple  # return tuple of interpolated heights

    if n < m:
        raise Error('insufficient %s: %s, need %s' % ('llis', n, m))
    return _as, llis
Exemplo n.º 10
0
def sumOf(nvectors, Vector=Nvector, h=None, **kwds):
    '''Return the vectorial sum of any number of n-vectors.

       @param nvectors: Vectors to be added (L{Nvector}[]).
       @keyword Vector: Optional class for the vectorial sum (L{Nvector}).
       @keyword kwds: Optional, additional I{Vector} keyword argments.
       @keyword h: Optional height, overriding the mean height (meter).

       @return: Vectorial sum (I{Vector}).

       @raise ValueError: No I{nvectors}.
    '''
    n, nvectors = len2(nvectors)
    if n < 1:
        raise ValueError('no nvectors: %r' & (n, ))
    if h is None:
        m = fsum(v.h for v in nvectors) / float(n)
    else:
        m = h
    return _sumOf(nvectors, Vector=Vector, h=m, **kwds)
Exemplo n.º 11
0
    def __init__(self, AB, x, y):
        '''(INTERNAL) New Alpha or Beta Krüger series

           @param AB: Krüger Alpha or Beta series coefficients (4-,
                      6- or 8-tuple).
           @param x: Eta angle (C{radians}).
           @param y: Ksi angle (C{radians}).
        '''
        n, j2 = len2(range(2, len(AB) * 2 + 1, 2))

        self._ab = AB
        self._pq = map2(mul, j2, self._ab)
        #       assert len(self._ab) == len(self._pq) == n

        x2 = map2(mul, j2, (x, ) * n)
        self._chx = map2(cosh, x2)
        self._shx = map2(sinh, x2)
        #       assert len(x2) == len(self._chx) == len(self._shx) == n

        y2 = map2(mul, j2, (y, ) * n)
        self._cy = map2(cos, y2)
        self._sy = map2(sin, y2)
Exemplo n.º 12
0
    def __init__(self, AB, x, y):
        '''(INTERNAL) New Alpha or Beta Krüger series

           @param AB: 6th-order Krüger Alpha or Beta series
                      coefficients (7-tuple, 1-origin).
           @param x: Eta angle (radians).
           @param y: Ksi angle (radians).
        '''
        n, j2 = len2(range(2, len(AB) * 2, 2))

        self._ab = AB[1:]  # 0-origin
        self._pq = map2(mul, j2, self._ab)
        #       assert len(self._ab) == len(self._pq) == n

        x2 = map2(mul, j2, (x, ) * n)
        self._chx = map2(cosh, x2)
        self._shx = map2(sinh, x2)
        #       assert len(x2) == len(self._chx) == len(self._shx) == n

        y2 = map2(mul, j2, (y, ) * n)
        self._cy = map2(cos, y2)
        self._sy = map2(sin, y2)
Exemplo n.º 13
0
def points2(points, closed=True, base=None):
    '''Check a polygon represented by points.

       @param points: The polygon points (C{LatLon}[])
       @keyword closed: Optionally, consider the polygon closed,
                        ignoring any duplicate or closing final
                        I{points} (C{bool}).
       @keyword base: Optionally, check the I{points} against this
                      base class C{None}.

       @return: 2-Tuple (n, points) with the number (C{int}) of points
                and the points C{list} or C{tuple}.

       @raise TypeError: Some I{points} are not C{LatLon}.

       @raise ValueError: Insufficient number of I{points}.
    '''
    n, points = len2(points)

    if closed:
        # remove duplicate or closing final points
        while n > 1 and (points[n - 1] == points[0]
                         or points[n - 1] == points[n - 2]):
            n -= 1
        # XXX following line is unneeded if points
        # are always indexed as ... i in range(n)
        points = points[:n]  # XXX numpy.array slice is a view!

    if n < (3 if closed else 1):
        raise ValueError('too few %s: %s' % ('points', n))

    if base and not (isNumpy2(points) or isTuple2(points)):
        for i in range(n):
            base.others(points[i], name='%s[%s]' % ('points', i))

    return n, points