Skip to content

For each point, distance to nearest point in second dataset

7 messages · Nick Eubank, Christopher W. Ryan, Michael Sumner

#
Hi All,

Trying to an equivalent to the "nearest" tool in ArcGIS for two
SpatialPointDataFrames. I can do the naive implementation by calculating
all pairwise distances using gDistance and taking the min, but I have some
huge datasets and was looking for something more efficient. (For example,
here's a trick with knearneigh for points in same dataset (
http://gis.stackexchange.com/questions/132384/distance-to-nearest-point-for-every-point-same-spatialpointsdataframe-in-r
)).

Any suggestions?

Thanks!

Nick
#
Use nabor package, it takes a little to learn but is crazy fast.

Happy to show if you make a relevant example.
Mike
On Fri, May 20, 2016, 06:42 Nick Eubank <nickeubank at gmail.com> wrote:

            
#
Thanks Michael!

Super easy context: two SpatialPointsDataFrames ("voters",
"polling.places"). Each has coordinate columns "x" and "y".

Is it just:

library(nabor)
knn(voters, query = polling.places, k=1)

?
On Thu, May 19, 2016 at 2:59 PM Michael Sumner <mdsumner at gmail.com> wrote:

            

  
  
#
For other's reference, that was almost right:

To get distance from each voter to closest polling place, the syntax was:

knn.results = knn(data=coordinates(polling.places),
query=coordinates(voters), k=1)
On Thu, May 19, 2016 at 3:03 PM Nick Eubank <nickeubank at gmail.com> wrote:

            

  
  
#
Last note for future searchers: also suggested was the SearchTree library:
http://stackoverflow.com/questions/37333747/for-each-point-distance-to-nearest-point-in-second-dataset-in-r
On Thu, May 19, 2016 at 3:38 PM Nick Eubank <nickeubank at gmail.com> wrote:

            

  
  
#
See also the spatstat package, and its nncross function.

--Chris Ryan
On Thu, May 19, 2016 at 6:48 PM, Nick Eubank <nickeubank at gmail.com> wrote:
#
On Fri, 20 May 2016 at 08:38 Nick Eubank <nickeubank at gmail.com> wrote:

            
Great!  Be sure to use the WKNNF approach to cache the kdtree of the data -
it only needs to be done once, but calls to knn obviously will redo that
part.

 Also, you'll probably want to do it with coordinates in a map projection
where nearest-neighbour distance makes sense for your domain.

Cheers, Mike.
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia