Coverage for pygeodesy/clipy.py : 94%

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 -*-
against a rectangular box or clip region.
@newfield example: Example, Examples '''
NN, _not_convex_, _start_, _too_few_, _0_0 LatLon_ as LL_
'''Clip box or clip region issue. ''' '''New L{ClipError}.
@arg name_n_corners: Either just a name (C{str}) or name, number, corners (C{str}, C{int}, C{tuple}). @kwarg txt: Optional explanation of the error (C{str}). '''
'''(INTERNAL) Check for near-equal points. '''
'''(INTERNAL) Check for not near-equal points. ''' abs(p1.lon - p2.lon) > EPS
'''(INTERNAL) Get the points to clip. ''' n, pts = len2(points) # only remove the final, closing point if n > 1 and _eq(pts[n-1], pts[0]): n -= 1 pts = pts[:n] if n < 3: raise PointsError(points=n, txt=_too_few_) else:
'''(INTERNAL) Cohen-Sutherland line clipping. ''' # single-bit clip codes
raise ValueError except (AttributeError, TypeError, ValueError) as x: raise ClipError(name, 2, (lowerleft, upperright), txt=str(x))
# def clip4(self, p, c): # clip point p for code c # if c & _CS._YMIN: # return self.lon4(p, self._ymin) # elif c & _CS._YMAX: # return self.lon4(p, self._ymax) # elif c & _CS._XMIN: # return self.lat4(p, self._xmin) # elif c & _CS._XMAX: # return self.lat4(p, self._xmax) # # should never get here # raise _AssertionError(_dot_(self._name, self.clip4.__name__))
else:
return _CS._YMAX, self.lon4, self._ymax, p else: # inside
return _CS._XMIN, self.lat4, self._xmin, p else: # inside
def nop4(self, b, p): # PYCHOK no cover if p: # should never get here raise _AssertionError(_dot_(self.name, self.nop4.__name__)) return _CS._IN, self.nop4, b, p
'''3-Tuple C{(start, end, index)} for each edge of a I{clipped} path with the C{start} and C{end} points (C{LatLon}) of the portion of the edge inside or on the clip box and the C{index} (C{int}) of the edge in the original path. '''
'''Clip a path against a rectangular clip box using the U{Cohen-Sutherland <https://WikiPedia.org/wiki/Cohen-Sutherland_algorithm>} algorithm.
@arg points: The points (C{LatLon}[]). @arg lowerleft: Bottom-left corner of the clip box (C{LatLon}). @arg upperright: Top-right corner of the clip box (C{LatLon}). @kwarg closed: Optionally, close the path (C{bool}). @kwarg inull: Optionally, retain null edges if inside (C{bool}).
@return: Yield a L{ClipCS3Tuple}C{(start, end, index)} for each edge of the clipped path.
@raise ClipError: The B{C{lowerleft}} and B{C{upperright}} corners specify an invalid clip box.
@raise PointsError: Insufficient number of B{C{points}}. '''
continue
if inull: # null edge if not c1: yield ClipCS3Tuple(p1, p1, i) elif not c2: yield ClipCS3Tuple(p2, p2, i) continue
else: # inside else: # PYCHOK no cover raise _AssertionError(_dot_(cs.name, 'for_else'))
'''(INTERNAL) List of clipped points. '''
'''(INTERNAL) LatLon_ for _SH intersections. '''
'''(INTERNAL) Sutherland-Hodgman polyon clipping. '''
raise ValueError(_not_convex_) raise ValueError('near-zero area') raise ClipError(name, n, cs, txt=str(x))
# clip points, closed always # pcs.append(p1) else: # ... p2 outside
raise ClipError(self.name, ne, self._cs, txt=_too_few_)
# ni is True iff all points are on or on the # right side (i.e. inside) of all clip edges, # no is True iff all points are on or at one # side (left or right) of each clip edge: if # none are inside (ni is False) and if all # are on the same side (no is True), then all # must be outside
# assert len(pts) == np
# and set self._x1, ._y1, ._dx, ._dy and # ._xy for each non-null clip edge
else: # original point
# clip corner c1 and clip edge c1 to c2, indicating whether # points[i] is located to the right, to the left or on top # of the (extended) clip edge from c1 to c2 self._dy * float(p.lon - self._x1) # clockwise corners, +1 means points[i] is to the right # of, -1 means on the left of, 0 means on edge c1 to c2
# of polygon edge p1 to p2 and the current clip edge, # where p1 and p2 are known to NOT be located on the # same side or on top of the current clip edge # <https://StackOverflow.com/questions/563198/ # how-do-you-detect-where-two-line-segments-intersect> if abs(fp) < EPS: # PYCHOK no cover raise _AssertionError(_dot_(self.name, self.intersect.__name__))
'''3-Tuple C{(start, end, original)} for each edge of a I{clipped} polygon, the C{start} and C{end} points (C{LatLon}) of the portion of the edge inside or on the clip region and C{original} indicates whether the edge is part of the original polygon or part of the clip region (C{bool}). '''
'''Clip a polygon against a clip region or box using the U{Sutherland-Hodgman <https://WikiPedia.org/wiki/Sutherland_Hodgman_algorithm>} algorithm.
@arg points: The polygon points (C{LatLon}[]). @arg corners: Three or more points defining a convex clip region (C{LatLon}[]) or two points to specify a rectangular clip box. @kwarg closed: Close the clipped points (C{bool}). @kwarg inull: Optionally, include null edges (C{bool}).
@return: Yield the clipped points (C{LatLon}[]).
@raise ClipError: The B{C{corners}} specify a polar, zero-area, non-convex or otherwise invalid clip box or region.
@raise PointsError: Insufficient number of B{C{points}}. '''
'''Clip a polygon against a clip region or box using the U{Sutherland-Hodgman <https://WikiPedia.org/wiki/Sutherland_Hodgman_algorithm>} algorithm.
@arg points: The polygon points (C{LatLon}[]). @arg corners: Three or more points defining a convex clip region (C{LatLon}[]) or two points to specify a rectangular clip box. @kwarg closed: Close the clipped points (C{bool}). @kwarg inull: Optionally, include null edges (C{bool}).
@return: Yield a L{ClipSH3Tuple}C{(start, end, original)} for each edge of the clipped polygon.
@raise ClipError: The B{C{corners}} specify a polar, zero-area, non-convex or otherwise invalid clip box or region.
@raise PointsError: Insufficient number of B{C{points}}. '''
# **) MIT License # # Copyright (C) 2018-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. |