1

So right now I have pretty large datasets, about 200,000 (lat,long) locations and a Large SpatialPolygonsDataFrame with a polygons list of length 428386 spread in a single city of around 450 square kms.

I want to calculate the nearest Spatial Polygon for every point in the dataset. I have tried the following procedures:

  1. Using the gDistance function from rgeos package. First I converted both the points and Polygons to same CRS and to planar projection and applied the function. Currently, around 75 points take 1.25 minutes which comes down to around 55 hours for my dataset.
  2. Using dist2Line function from geosphere package. This allows me to use my (lat, long) points as it is, i.e. without modifying the data but it takes a huge amount of time. A single point took around 15 minutes so it is clearly not feasible.

Can anyone suggest me a better way of calculating these distances, if possible in R?

However I'm also comfortable in using python if it has a better approach.

closed as off-topic by PolyGeo Jul 6 at 23:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Welcome to GIS SE! We're a little different from other sites. We're a Q&A site, not a discussion forum. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely. Please check out our short tour for more about how the site works. Thanks. – PolyGeo Jul 6 at 23:19
  • Since you have working code, and are just seeking to improve its performance, you may want to consider the [code-review.se] Stack Exchange for your question. – PolyGeo Jul 6 at 23:20
  • If points are inside polygons do you treat that as zero distance? Doing that first will reduce the need to compare the remaining distances. – mdsumner Jul 7 at 21:19
  • You asked this "yesterday", which means maybe 24 hours ago. If your 55 hour estimate is correct, this will be done in about 31 hours. Is it worth us answering after then? – Spacedman Jul 7 at 23:03