# Efficiently computing distance to large number of geometries

I have a largeish (slightly less than 500000) points and need to compute the distance to the nearest object of a collection of geometries (usually a bunch of polygons, but in one case a bunch of lines). My current approach is to compute the union of the target geometries into a big multipolygon/multiline, put the points in a GeoPandas frame and use the `df.geometry.distance()` object to compute the distance for each point. It works, but it's ridiculously slow, so I suspect I'm going about this the wrong way.

Given my problem, what's the correct way of computing the distance between each point and the collection of geometries? An efficient solution in Python would be great, but dropping down to raw GDAL in C/C++ isn't a problem either if that's what it takes.

The largest of the polygon collections has about 80000 polygons in it, while the line collections has about 750000 lines in it. In full code, my current approach is:

``````points = geopandas.read_file('points.shp')
• The polygon collections are in the range 500-80000 polygons, the line data set is bigger at about 750000 lines. The computation now is basically `big = collection_df.geometry.unary_union; dists = points.geometry.distance(big)`, because it was the dead easy way to write it. Sep 15 '21 at 18:17