Skip to content
Prev 22342 / 29559 Next

Calculate shortest distance between points belonging to different polygons

Dear Adrian,

Thanks for your explanation.

My goal is measuring the minimum distance from each data point to its 
nearest neighbor belonging to a different polygon (a pair of point then).
In fact, this distance is in the matrix resulting from
The polygons I'm considering are relatively small and distant each other 
so, in my case, the distance between data points belonging to the same 
polygon is certainly shorter that the distance to data points belonging 
to other polygons. Thus, the smallest value of each row is the distance 
from a data point to its nearest neighbor within the same polygon, while 
the second smallest value of each row is the distance from a data point 
and its nearest neighbor belonging to another polygon.

The latter value is the distance I want and I extracted it from the 
matrix using the following /for/ loop:
+ min.dist.temp<-min(nn.pol[i,] [nn.pol[i,] != min(nn.pol[i,])])

+ min.dist<-as.vector(c(min.dist,min.dist.temp))

+ }

Maybe not the most elegant way to do it...but it worked. The result is a 
vector whose length is equal to the number of data point.
[1] 26370.49 15070.99 27587.33 15722.28 15186.39 31147.84


If the polygons were large and tangential or overlapping, I could not 
know which value I should have extracted because the distance between 
two points belonging to the same polygon might be larger than the 
distance between two points belonging to different polygons.

Thus, my question: is there a way to calculate the distance from each 
data point and its nearest neighbor (only one) belonging to a different 
polygon?

Regards,

Ivan
On 18-Feb-15 13:02, Adrian Baddeley wrote: