python – Creating a Polygon with a Hole in Shapely

geometrymulti-polygonpolygonpythonshapely

So I have two polygons: (i) An outer polygon, and (ii) an inner Polygon representing the 'hole'. I'm trying to merge these two to give a single Polygon, with the hole.

For easy illustration, the outer Polygon looks like:

enter image description here

And the (ii) Hole looks like:

enter image description here

Both these Polygons are Shapely Polygons. To create a Polygon with the hole, I researched a lot through this exchange and although I found limited resources, I tried the following:

#x: Shapely Polygon Type - The outer Polygon
#y: Shapely Polygon Type - The 'hole'
w=geometry.Polygon(x, holes=[y])

I still seem to be getting the same issue, the hole is not represented. Any tips?

Best Answer

If starting with two Polygon objects then you can use the object.difference() method:

with_hole = outer.difference(inner)
Related Question