From ecb00b8d43387ade0adbf346caa4edf4354bfe6f Mon Sep 17 00:00:00 2001 From: "vilda.novak@gmail.com" Date: Fri, 25 Apr 2014 07:19:20 +0000 Subject: [PATCH] checks for shapely conversion, was throwing bugs with some Polygon polygons --- scripts/addons/cam/polygon_utils_cam.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/scripts/addons/cam/polygon_utils_cam.py b/scripts/addons/cam/polygon_utils_cam.py index 0e75180e..3929bc0a 100644 --- a/scripts/addons/cam/polygon_utils_cam.py +++ b/scripts/addons/cam/polygon_utils_cam.py @@ -6,6 +6,7 @@ import curve_simplify import Polygon try: + import shapely from shapely.geometry import polygon as spolygon from shapely import ops from shapely import geometry @@ -17,24 +18,26 @@ except: def Polygon2Shapely(p): conts=[] sp=geometry.Polygon() - #for ci,c in enumerate(p): - # l=geometry.Polygon(c) - # conts.append(l) holes=[] contours=[] #print(len(p)) #print(p) for ci,c in enumerate(p): - if p.isHole(ci): - #print('ishole') - holes.append(c) - else: - contours.append(c) + #print(ci) + shapely_p=spolygon.Polygon(c) + if p.nPoints(ci)>2 and shapely_p.is_valid: + + if p.isHole(ci): + #print('ishole') + holes.append(shapely_p) + else: + contours.append(shapely_p) - for c in contours: - sp=sp.union(spolygon.Polygon(c)) + #for c in contours: + #sp=sp.union(spolygon.Polygon(c) + sp=shapely.ops.unary_union(contours) for h in holes: - sp=sp.difference(spolygon.Polygon(h)) + sp=sp.difference(h) #sp=geometry.asMultiPolygon(conts) #sp=ops.cascaded_union(sp)