[R-sig-Geo] sub-selecting spatial data by distance from a different point(set)

Roger Bivand Roger.Bivand at nhh.no
Sat Nov 14 16:31:10 CET 2009


On Sat, 14 Nov 2009, Paul Hiemstra wrote:

> Hi Sean,
>
> Take a look at the spDistsN1 function to calculate the distances from a 
> single point to all other points. You can then make the subset based on the 
> distance. What could also be an options is to build a polygon that has the 
> radius around the point you are taking the distance from, and then use 
> overlay to find which points are in the radius polygon. This might be faster 
> when using a large dataset. I'm not sure how nto make the polygon, but it 
> should be possible. But you could first take a look at spDistsN1.

In fact spDistsN1() in a loop over the coordinates of B runs (for me) 
faster than building and searching a quadtree in A, and (to machine 
precision) gets the same results. So allocate an empty list, and step 
through:

dn <- vector(mode="list", length=nrow(B))
for (i in 1:nrow(B)) {
   dn[[i]] <- which(spDists(A, B[i,]) <= threshold)
}

where A is a 2M by 2 matrix and B is a 500 by 2 matrix. dn will be a list 
of 500 vectors of point IDs in A. For me on a laptop (with A 2M points 
from runif() and B 500 points from runif()), this search took under 5 
minutes, with # neighbours between 7000 and 16000.

Roger

>
> cheers,
> Paul
>
> Sean O'Riordain wrote:
>> Good afternoon,
>> 
>> Apologies if this seems a basic question but I'm quite new to spatial
>> analysis (1 week now!) and I'm reading and learning as fast as I can.
>> I've read the Spatial Data task view, "Analysing spatial point
>> patterns in R" by Bradley and I've ordered "Applied Spatial Data
>> Analysis with R" - but it hasn't arrived yet...  I've looked and
>> searched - but I'm guessing that I'm not using the right buzzwords...
>> 
>> I have a data.frame (call it A) of about 2 million X,Y points each
>> with info with various characteristics, numeric and factor.  I have a
>> separate much smaller data.frame (call it B) of say 500 rows of X,Y
>> and I'd like to be able to pull a subset of A, so that for each B[i,]
>> I will get all rows from A that are within a given straight line
>> distance (i.e. within a short 'as the crow flies' radius).
>> 
>> Could somebody please give me some starting hints please?
>> 
>> Thanks in advance,
>> Sean O'Riordain
>> 
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> 
>
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list