I'm working with Shapely polygons and I need a way to delete all smaller polygons contained within a bigger polygon. I tried using the .contains()
method which Shapely provides, but the method doesn't return True
if a given smaller polygon isn't completely inside of the bigger, "parent" polygon.
Basically, I want a method like .contains()
but that it returns True
in case the inner polygon shares boundaries with the outer polygon like on the example image below.
.
Here are the polygons from the picture presented in wkt format:
The green one:
POLYGON Z ((14.4265764858233823 45.3396418051734784 0.0000000000000000, 14.4267228266679606 45.3395430970275015 0.0000000000000000, 14.4266753563381904 45.3394727193694536 0.0000000000000000, 14.4265290154936121 45.3395714275154376 0.0000000000000000, 14.4265764858233823 45.3396418051734784 0.0000000000000000))`
The red one:
POLYGON Z ((14.4265450394689161 45.3395951840357725 0.0000000000000000, 14.4265695507109317 45.3395786509942837 0.0000000000000000, 14.4265802185605700 45.3395944667317679 0.0000000000000000, 14.4265982245953417 45.3395823215079616 0.0000000000000000, 14.4265715327703994 45.3395427492501426 0.0000000000000000, 14.4265290154936121 45.3395714275154376 0.0000000000000000, 14.4265450394689161 45.3395951840357725 0.0000000000000000))
I also tried using the .intersects()
method but it returns True
for polygons outside of a given polygon which have some common boundaries, which I do not want.
I hope you understand what I need and I'm thankful if someone knows a solution to this.
contains
should normally work. Create, for example, two simple square polygons one inside another that share a side to see that it works. In your case, there is probably a problem with precision. Could you provide a definition of both polygons, so that we could reproduce the issue? – Xylographypolygon.wkt
, right?). Because if I load them and usegreen.contains(red)
then it will showTrue
. To get all the digits, could you please usewkt.dumps
withtrim=False
argument and add them in the question? So, it would be:from shapely import wkt; wkt.dumps(polygon, trim=False)
– Xylography