[R-sig-Geo] Filtering a set of points by minimum distance between them [SUMMARY]
Arnald Marcer
arnald.marcer at uab.cat
Thu Mar 3 14:33:23 CET 2011
Hi,
I finally figured out how to select a subset of coordinates so that
any given one is at a minimum distance of any other one. Here
is the function I wrote. Roger, pointed me how to do it with the spdep
package but I have not been able to do it. I figured out another way,
surely less general, but which works for projected coordinates only.
Arnald
CREAF
Here it is my solution:
#*************************************************************************
sampleByMinDistance <- function(min.dist=0, coords){
# shuffle the coordinates to factor in randomness in the selection
of points
coords <- coords[sample(1:length(coords[,1]), length(coords[,1])),]
# initialize vector of indices at index 1 of coordinates
v <- c(1)
is.far <- TRUE
for(i in 1:length(coords[,1])){
for(j in 1:length(v)){
dist <- distance(coords[i,], coords[v[j],])
if(dist < min.dist){
is.far <- FALSE
}
}
if(is.far){
v <- append(v, i)
}
is.far <- TRUE
}
return(coords[v,)
}
#**************************************************************************
distance <- function(p1, p2){
dx <- abs(p1[1,1] - p2[1,1])
dy <- abs(p1[1,2] - p2[1,2])
dist <- sqrt(dx^2 + dy^2)
return(dist)
}
#**************************************************************************
On 03/03/2011 10:42 AM, Roger Bivand wrote:
> On Thu, 3 Mar 2011, Arnald Marcer wrote:
>
>> Roger,
>>
>> Thank you for your very rapid response.
>>
>> I tried
>>
>> knn <- knearneigh(coords, k=1, longlat=NULL)
>>
>> but then do not know how to proceed to extract points
>> separated by minimum distance ...
>
> df <- data.frame(from=1:knn$np, fromX=knn$x[,1], fromY=knn$x[,2],
> to=knn$nn[,1], toX=knn$x[knn$nn[,1],1], toY=knn$x[knn$nn[,1],2])
>
> makes a 6-column data frame, with from and to point sequence numbers,
> and from and to coordinates.
>
> plot(coords)
> segments(df$fromX, df$fromY, df$toX, df$toY)
>
> shows the links - some of which have from and to as mutual nearest
> neighbours. If you need more detail on extra options to use outside
> knearneigh(), look at the help page for nn() in RANN.
>
> Please summarise to the list when your prolem is resolved.
>
> Roger
>
>>
>> Arnald
>>
>> On 03/03/2011 10:12 AM, Roger Bivand wrote:
>>> On Thu, 3 Mar 2011, Arnald Marcer wrote:
>>>
>>>> Hi,
>>>>
>>>> I have a set of coordinates in utm and need to get a subset of
>>>> them in which any coordinate is at a minimum distance from
>>>> any other one, that is to say, I need to filter them according
>>>> to a minimum distance between them.
>>>>
>>>> Is there a function to do that or should I try to code it myself ?
>>>> Is there also a way of introducing randomness in the sense that
>>>> any run will give me a different set ?
>>>
>>> The knearneigh() function in the spdep package calls functions in
>>> the RANN package to do this for planar coordinates - set the number
>>> of neighbours required to 1. You could look at the RANN package for
>>> non-deterministic solutions.
>>>
>>> Roger
>>>
>>>>
>>>> Any help would be much appreciated
>>>>
>>>> Thanks
>>>>
>>>> Arnald Marcer
>>>> CREAF (Centre for Ecological Research and Forestry Applications)
>>>>
>>>> _______________________________________________
>>>> R-sig-Geo mailing list
>>>> R-sig-Geo at r-project.org
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>
>>>
>>
>>
>>
>
--
Arnald Marcer
Investigador
Centre de Recerca Ecològica i Aplicacions Forestals (CREAF)
Edifici C
Universitat Autònoma de Barcelona
08193 Barcelona
Tel: 93 581 46 67
Fax: 93 581 41 51
More information about the R-sig-Geo
mailing list