Python pyclipper.Pyclipper() Examples

The following are 11 code examples of pyclipper.Pyclipper(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyclipper , or try the search function .
Example #1
Source File: metrics.py    From P2PaLA with GNU General Public License v3.0 6 votes vote down vote up
def poly_intersect(subj, clip):
    """
    """
    pc = pyclipper.Pyclipper()
    pc.AddPath(clip, pyclipper.PT_CLIP, True)
    pc.AddPath(subj, pyclipper.PT_SUBJECT, True)
    solution = pc.Execute(
        pyclipper.CT_INTERSECTION, pyclipper.PFT_EVENODD, pyclipper.PFT_EVENODD
    )
    return np.array(solution) 
Example #2
Source File: clipping.py    From glmtools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def join_polys(polys, scale=True):
    """ Given a list of polygons, merge them (union) and return a list
        of merged polygons
    """
    pc = pyclipper.Pyclipper()

    if scale:
        polys = scale_to_clipper(polys)

    results=[]
    pc.AddPaths(polys, pyclipper.PT_SUBJECT, True)
    clip_polys = pc.Execute(pyclipper.CT_UNION, pyclipper.PFT_NONZERO,
        pyclipper.PFT_NONZERO)
    if scale:
        clip_polys = scale_from_clipper(clip_polys)
    results.extend([cp for cp in clip_polys])
    pc.Clear()
    return results 
Example #3
Source File: clippers.py    From geomeppy with MIT License 5 votes vote down vote up
def _prepare_clipper(self, poly):
        """Prepare 2D polygons for clipping operations.

        :param poly: The clip polygon.
        :returns: A Pyclipper object.

        """
        s1 = pc.scale_to_clipper(self.vertices_list)
        s2 = pc.scale_to_clipper(poly.vertices_list)
        clipper = pc.Pyclipper()
        clipper.AddPath(s1, poly_type=pc.PT_SUBJECT, closed=True)
        clipper.AddPath(s2, poly_type=pc.PT_CLIP, closed=True)
        return clipper 
Example #4
Source File: clippers.py    From geomeppy with MIT License 5 votes vote down vote up
def _prepare_clipper(self, poly):
        """Prepare 3D polygons for clipping operations.

        :param poly: The clip polygon.
        :returns: A Pyclipper object.

        """
        if not self.is_coplanar(poly):
            return False
        poly1 = self.project_to_2D()
        poly2 = poly.project_to_2D()

        s1 = pc.scale_to_clipper(poly1.vertices_list)
        s2 = pc.scale_to_clipper(poly2.vertices_list)
        clipper = pc.Pyclipper()
        clipper.AddPath(s1, poly_type=pc.PT_SUBJECT, closed=True)
        clipper.AddPath(s2, poly_type=pc.PT_CLIP, closed=True)

        return clipper 
Example #5
Source File: polygon.py    From mapbox-vector-tile with MIT License 5 votes vote down vote up
def make_valid_pyclipper(shape):
    """
    Use the pyclipper library to "union" a polygon on its own. This operation
    uses the even-odd rule to determine which points are in the interior of
    the polygon, and can reconstruct the orientation of the polygon from that.
    The pyclipper library is robust, and uses integer coordinates, so should
    not produce any additional degeneracies.

    Before cleaning the polygon, we remove all degenerate inners. This is
    useful to remove inners which have collapsed to points or lines, which can
    interfere with the cleaning process.
    """

    # drop all degenerate inners
    clean_shape = _drop_degenerate_inners(shape)

    pc = pyclipper.Pyclipper()

    try:
        pc.AddPaths(_coords(clean_shape), pyclipper.PT_SUBJECT, True)

        # note: Execute2 returns the polygon tree, not the list of paths
        result = pc.Execute2(pyclipper.CT_UNION, pyclipper.PFT_EVENODD)

    except pyclipper.ClipperException:
        return MultiPolygon([])

    return _polytree_to_shapely(result) 
Example #6
Source File: evaluation.py    From keras-ocr with MIT License 5 votes vote down vote up
def iou_score(box1, box2):
    """Returns the Intersection-over-Union score, defined as the area of
    the intersection divided by the intersection over the union of
    the two bounding boxes. This measure is symmetric.

    Args:
        box1: The coordinates for box 1 as a list of (x, y) coordinates
        box2: The coordinates for box 2 in same format as box1.
    """
    if len(box1) == 2:
        x1, y1 = box1[0]
        x2, y2 = box1[1]
        box1 = np.array([[x1, y1], [x2, y1], [x2, y2], [x1, y2]])
    if len(box2) == 2:
        x1, y1 = box2[0]
        x2, y2 = box2[1]
        box2 = np.array([[x1, y1], [x2, y1], [x2, y2], [x1, y2]])
    if any(cv2.contourArea(np.int32(box)[:, np.newaxis, :]) == 0 for box in [box1, box2]):
        warnings.warn('A box with zero area was detected.')
        return 0
    pc = pyclipper.Pyclipper()
    pc.AddPath(np.int32(box1), pyclipper.PT_SUBJECT, closed=True)
    pc.AddPath(np.int32(box2), pyclipper.PT_CLIP, closed=True)
    intersection_solutions = pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_EVENODD,
                                        pyclipper.PFT_EVENODD)
    union_solutions = pc.Execute(pyclipper.CT_UNION, pyclipper.PFT_EVENODD, pyclipper.PFT_EVENODD)
    union = sum(cv2.contourArea(np.int32(points)[:, np.newaxis, :]) for points in union_solutions)
    intersection = sum(
        cv2.contourArea(np.int32(points)[:, np.newaxis, :]) for points in intersection_solutions)
    return intersection / union 
Example #7
Source File: polygon.py    From go2mapillary with GNU General Public License v3.0 5 votes vote down vote up
def make_valid_pyclipper(shape):
    """
    Use the pyclipper library to "union" a polygon on its own. This operation
    uses the even-odd rule to determine which points are in the interior of
    the polygon, and can reconstruct the orientation of the polygon from that.
    The pyclipper library is robust, and uses integer coordinates, so should
    not produce any additional degeneracies.

    Before cleaning the polygon, we remove all degenerate inners. This is
    useful to remove inners which have collapsed to points or lines, which can
    interfere with the cleaning process.
    """

    # drop all degenerate inners
    clean_shape = _drop_degenerate_inners(shape)

    pc = pyclipper.Pyclipper()

    try:
        pc.AddPaths(_coords(clean_shape), pyclipper.PT_SUBJECT, True)

        # note: Execute2 returns the polygon tree, not the list of paths
        result = pc.Execute2(pyclipper.CT_UNION, pyclipper.PFT_EVENODD)

    except pyclipper.ClipperException:
        return MultiPolygon([])

    return _polytree_to_shapely(result) 
Example #8
Source File: viafence.py    From RF-tools-KiCAD with GNU General Public License v3.0 5 votes vote down vote up
def clipPolygonWithPolygons(path, clipPathList):
    import pyclipper
    pc = pyclipper.Pyclipper()
    pc.AddPath(path, pyclipper.PT_SUBJECT, True)
    for clipPath in clipPathList: pc.AddPath(clipPath, pyclipper.PT_CLIP, True)
    return pc.Execute(pyclipper.CT_DIFFERENCE) 
Example #9
Source File: viafence.py    From RF-tools-KiCAD with GNU General Public License v3.0 5 votes vote down vote up
def unionPolygons(pathList):
    import pyclipper
    pc = pyclipper.Pyclipper()
    for path in pathList: pc.AddPath(path, pyclipper.PT_SUBJECT, True)
    return pc.Execute(pyclipper.CT_UNION, pyclipper.PFT_NONZERO) 
Example #10
Source File: clipping.py    From glmtools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def clip_poly_pair(pc, p, q):
    """"
    pc: an instance of pyclipper.Pyclipper.
    p:  the polygon by which to clip other polygon q.

    pc and p may be held fixed through use of functools.partial so that
    multiple q may be clipped by p.
    """
    pc.Clear()
    pc.AddPath(q, pyclipper.PT_SUBJECT, True)
    pc.AddPath(p, pyclipper.PT_CLIP, True)
    clip_polys = pc.Execute(clip_type=pyclipper.CT_INTERSECTION)
    return clip_polys 
Example #11
Source File: clipping.py    From glmtools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def clip_polys_by_one_poly(polys, p, scale=True):
    """ polys: a list of polygons
        p: the polygon with which to clip polys
        scale: convert floating point polygon coordinates to ints required
            by the underlying clipper library.

        Returns results, sub_poly_count
        results: all polygons that were part of p but were chopped by the
            other polys

        sub_poly_count: how many sub-polygons were found for each
            poly in polys. Can be used with np.repeat to replicate a
            list of values associated with each original poly in polys.
    """
    pc = pyclipper.Pyclipper()

    if scale:
        polys = scale_to_clipper(polys)
        p = scale_to_clipper(p)
        # Changing from the above to the below seems to move most of
        # time consumption to within the AddPath calls, i.e., it's not
        # the float->int that is expensive, but rather the type conversion
        # from Python.
        # scale_fact = 2 ** 31
        # polys = (polys * scale_fact).astype('int64')
        # p = (p*scale_fact).astype('int64')

    # Each individual sub-poly is stored here.
    # For any polygon p, zero, one or more than one polygon may be returned
    # for each poly in polys (depending on overlap and the complexity of p).
    # Keep track of how many sub-polygons were found for each poly in polys
    cpp = partial(clip_poly_pair, pc, p)
    all_clip_polys = map(cpp, polys)
    if scale:
        sfc = scale_from_clipper #partial(scale_from_clipper, scale=scale_fact)
        all_clip_polys = map(sfc, all_clip_polys)
    all_clip_polys = list(map(list, all_clip_polys))
    sub_polys_per_poly = list(map(len, all_clip_polys))
    results = list(itertools.chain.from_iterable(all_clip_polys))

    return results, sub_polys_per_poly