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')
geometries = geopandas.read_file('collection.shp')
big_geometry = geometries.geometry.unary_union
points['distance_to_geometries'] = points.geometry.distance(big_geometry)

For context, the points are the coordinates of buildings and the polygons and lines are various natural hazard things (rivers, flood zones, landslide zones, and the like), all within Norway.

  • 3
    How many polygons? Can you add the code you are using now
    – BERA
    Sep 15 '21 at 18:06
  • If you can store all your geometries into a PostGIS database, then you would have access to SD_distance or ST_3ddistance. This can be another solution. Sep 15 '21 at 18:07
  • 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.
    – arnsholt
    Sep 15 '21 at 18:17
  • If I leave the polygon/line collections as is, maybe I could use some kind of spatial indexing technique to narrow down the list of candidates to measure against and speed it up that way?
    – arnsholt
    Sep 15 '21 at 19:32
  • It would really help to provide some code which creates randomized points, polygons, and lines which is similar to your problem.
    – Shawn
    Oct 7 '21 at 1:31

Your Answer

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

Browse other questions tagged or ask your own question.