2

I'm using shapely and python to divide polygons into smaller pieces: trapezoids, parallelograms with sides parallel to x-axis and triangles with side parallel to x-axis. Initial data come from .gds file and is presented like list of tuples x,y coords. For my purpose I used difference method like described here. But I get stack when polygon has holes, for example:

from shapely.geometry import Polygon
points = [(0.0, -1.0), (0.0, 2.0), (3.0, 2.0), (3.0, 1.0), (1.0, 1.0), (1.0, 0.0), (2.0, 0.0), (2.0, 1.0), (3.0, 1.0), (3.0,-1.0)]
poly = Polygon(points)
print(poly.is_valid)
#Self-intersection at or near point 2 1
#False

So the question is what is the simpliest way how to convert this list of points into hull and holes for correct creation of polygon?

Thank you!

Community
  • 1
  • 1
Anton
  • 67
  • 4

1 Answers1

0

The standard trick for this is to use poly.buffer(0) (see the shapely manual).

polyb = poly.buffer(0)
print(shapely.geometry.mapping(polyb))
# {'type': 'Polygon', 'coordinates': (((0.0, -1.0), (0.0, 2.0), (3.0, 2.0), (3.0, 1.0), (3.0, -1.0), (0.0, -1.0)), ((2.0, 1.0), (1.0, 1.0), (1.0, 0.0), (2.0, 0.0), (2.0, 1.0)))}
print(polyb.is_valid)
# True
jdmcbr
  • 5,964
  • 6
  • 28
  • 38
  • Thanks for fast reply! I had read that before I wrote here, but I was confused with my usual way checking of polygon `x,y = shape.exterior.coords.xy` and of course there were no interior points. So you cleared my mind. Thanks! – Anton Feb 03 '17 at 20:24