Sounds like a simple question, and the Shapely doc says all I need to do to create a simple polygon with a hole is to pass two lists of coordinate tuples:
exterior = [(2, 8), (7, 8), (7, 3), (2, 3), (2, 8)]
interior = [(4, 7), (4, 5), (6, 5), (6, 7), (4, 7)]
pgn = Polygon(exterior, holes=interior)
However, I get the following error:
AttributeError: 'tuple' object has no attribute '_ndim'
I also tried passing a Polygon as the interior ring.
interior=Polygon([(4, 7), (4, 5), (6, 5), (6, 7), (4, 7)])
pgn = Polygon(exterior, holes=interior)
>>> TypeError: object of type 'Polygon' has no len()
What is the correct way to use this constructor? Looks like the holes
parameter should not be a tuple or a Polygon
, so what should it be?
Best Answer
A polygon can have multiple holes in it, so the
holes
argument is a list of list of coordinate tuples:This fails:
because
interior
isn't a list of interior polygons, its just one. So make it a list with one member:Docs:
The "source code" links are great working examples of the shapely API, eg this:
in https://shapely.readthedocs.io/en/latest/code/polygon.py