Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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)
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
return paths2
if not paths2:
return paths1
pc = pyclipper.Pyclipper()
if paths1:
if paths1[0][0] in (int, float):
raise pyclipper.ClipperException()
paths1 = pyclipper.scale_to_clipper(paths1, SCALING_FACTOR)
pc.AddPaths(paths1, pyclipper.PT_SUBJECT, True)
if paths2:
if paths2[0][0] in (int, float):
raise pyclipper.ClipperException()
paths2 = pyclipper.scale_to_clipper(paths2, SCALING_FACTOR)
pc.AddPaths(paths2, pyclipper.PT_CLIP, True)
try:
outpaths = pc.Execute(pyclipper.CT_UNION, pyclipper.PFT_EVENODD, pyclipper.PFT_EVENODD)
except:
print("paths1={}".format(paths1))
print("paths2={}".format(paths2))
outpaths = pyclipper.scale_from_clipper(outpaths, SCALING_FACTOR)
return outpaths
InvalidSubjectContourError, InvalidClippingContourError, ExecutionError)
import pyclipper
"""
General Suggestions:
- Contours should only be sent here if they actually overlap.
This can be checked easily using contour bounds.
- Only perform operations on closed contours.
- contours must have an on curve point
- some kind of a log
"""
_operationMap = {
"union": pyclipper.CT_UNION,
"intersection": pyclipper.CT_INTERSECTION,
"difference": pyclipper.CT_DIFFERENCE,
"xor": pyclipper.CT_XOR,
}
_fillTypeMap = {
"evenOdd": pyclipper.PFT_EVENODD,
"nonZero": pyclipper.PFT_NONZERO,
# we keep the misspelling for compatibility with earlier versions
"noneZero": pyclipper.PFT_NONZERO,
}
def clipExecute(subjectContours, clipContours, operation, subjectFillType="nonZero",
clipFillType="nonZero"):
pc = pyclipper.Pyclipper()
def union(self, poly):
# type: (Polygon) -> List[Polygon]
"""Union with another polygon.
:param poly: The clip polygon.
:returns: A list of Polygons.
"""
clipper = self._prepare_clipper(poly)
if not clipper:
return []
unions = clipper.Execute(
pc.CT_UNION, pc.PFT_NONZERO, pc.PFT_NONZERO)
return self._process(unions)