[GIS] How to simplify borders between polygons in shapely

geoprocessingpythonshapely

I have some fairly complex, and not completely accurate shapes stored as shapely Polygon objects. I'm trying to find the common borders, which should be as easy as finding the LineString returned from poly1.intersection(poly2).

However, due the the slight inaccuracies I'm finding that intersection is returning Polygon and Point objects, or sometimes a GeometryCollection object. Never a simple LineString except in my simple test code where the Polygon shapes are accurate.

Is there any way of simplifying the borders, like a "snap-to" method, or a tolerance parameter that might ease my pain?

Best Answer

Since version 1.5, shapely provides a snap function that can be used in that way.

From the shapely documentation

from shapely.ops import snap
square = Polygon([(1,1), (2, 1), (2, 2), (1, 2), (1, 1)])
line = LineString([(0,0), (0.8, 0.8), (1.8, 0.95), (2.6, 0.5)])
result = snap(line, square, 0.5)