How can I merge polygons that are almost touching, using Python? For example, given these polygons and some distance threshold T: The algorithm would produce something like this: So basically I need to remove any gaps smaller than T between the two polygons. The original vertices of the polygons should not be unnecessarily modified. So for example simply buffering both polygons and then taking a union of them is not good. A library that already implements this functionality would also be fine. EDIT: As another example, ArcGIS polygon aggregation seems to do exactly this. However there isn't much hint about how it is implemented, and using ArcGIS is not an option. Continue reading...