This question has been asked a lot by a lot of different people I am sure. I did some searching and found some blog posts and potential ways forward, but before I dove off into the deep end I thought I would come here to get the community consensus.
- Problem Statement
I have a series of points (approximately 1.5 million) which I need to test for inclusion in rectangular polygon. When I profile my code, this is the longest pole in the tent when it comes to execution time. I would like to see if there is something that I can do which would reduce the execution time.
- Minimal code example and Offending Code Line
library(sf) load('inputs.RData') points_to_include = unlist(sf::st_contains(profile_segments$buffer[], bathy_data))
(The above code and the input.RData file can be found at this GitHub Gist)
This code is currently taking ~ 6 seconds to run based on
microbenchmark. While that does not seem like a long time, doing this over and over really begins to add up.
Unit: seconds expr min lq mean median uq max neval points_to_include 5.494459 5.532229 5.985662 5.591614 6.060509 7.249499 5
- Potential Solutions
I looked at the blog post https://www.r-bloggers.com/speeding-up-spatial-analyses-by-integrating-sf-and-data-table-a-test-case-2/ for some guidance. There hinted that possibly I could use some mashup between data.table, parallel execution, and chunking the points. I would like to avoid the use of data.table if possible (however, the speed improvements identified on that site are fairly amazing).
One possibility I thought of was to go to a UTM style coordinate system and apply a transformation (rotation) which would place my polygon (always a rectangle, however arbitrarily sized and oriented) at the coordinate 0,0 and then just do a simple filter on xmin, xmax and ymin, ymax. However, while the search is very fast in that sense, the transformation would take some time.
Like I mentioned before, I wanted to get peoples opinion on which way I should go so as to not spin my wheels. Thanks for any info on what I can do with my specific use case and the current state of the tools.
R: 3.5.2 RStudio: 1.3.66 SF: 0.9-2