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?


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)

Although not a complete answer, I think I have an algorithm that could work.

  1. For each point in poly1, if the Point is closer than epsilon to a LineString in poly2 then move the Point onto the LineString.

  2. For each pair of Point objects (one from poly1 and one from poly2) if the two are closer than epsilon then move both to the central point between them.

  3. For any sets of adjacent collinear Point objects on the same Polygon, remove all but the Point at each end.


If you are dealing with conterminous polygons, you probably want to use TopoJSON to represent your data, as it by definition stores shared boundaries in a common structure, making them identical. Data file size should be reduced, as well.

That said, as cool as it is, I don't think the format is widely used, and you still have the problem of how to get your data in to that format. If @francisopuga's "snap" answer works, then maybe use TopoJSON after that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.