I would like to snap one set of points (SpatialPoints* objects) to another in R using Euclidean distance. I'm hoping for a function like maptools::snapPointsToLines.
The attributes of the points don't need to be transferred.
Any ideas?
rsp
I would like to snap one set of points (SpatialPoints* objects) to another in R using Euclidean distance. I'm hoping for a function like maptools::snapPointsToLines.
The attributes of the points don't need to be transferred.
Any ideas?
Best Answer
Here's a reproducible answer and a function that I think solves the problem. It all relies on nncross from the spatstat package.
Step 1: Load the packages we'll be using
Step 2: Create two small sets of points, give one attribute data:
Step 3: Create a function to allocate coordinates of x those of the nearest y:
Step 4: Test the output:
The output is shown below - all the red 'x' points have snapped to only 3 green 'y' points and their attribute data is maintained. Please test on your (perhaps larger and more complex) dataset and let me know if it works.
There may be a pre-existing function to do this and almost certainly a more efficient implementation.