Fast minimum distance between two sets of polygons

In PostGIS, use ST_Distance.

--If the units of the spatial reference is meters then units would be meters
SELECT DISTINCT ON (df.gid) df.gid, df.name, df_subset.name
FROM df
    LEFT JOIN df_subset ON ST_Distance(df.geometry, df_subset.geometry)
ORDER BY df.name, ST_Distance(df.geometry, df_subset.geometry);

ST_DWithin would be faster because it uses indexes but you must supply a distance parameter also.


You can do this in Python with the help of Rtree and Shapely. The procedure would look similar to this answer.

The R-tree index would identify a list of possible nearest polygons, all based on bounding boxes. Then from this shortlist list, you'd go through them and use Shapely to determine which polygon is closest, based on the minimum distance between the geometries. Consider that you might have more than one geometry that is closest (e.g., if there are multiple polygons that intersect, they will all be 0 distance, thus there is no metric for "closest". You could additionally evaluate the minimum distance of centroids from polygons with 0 distance.)

This is a pretty good blog post showing how to use GeoPanda's sindex (spatial index), which is based on Rtree (which is an optional dependency for GeoPandas).