[R-sig-Geo] Calculate shortest distance between points belonging to different polygons
Ivan Palmegiani
pan.sapiens.it at gmail.com
Tue Feb 17 11:35:31 CET 2015
Dear members of the list,
I'm handling a SpatialPointsDataFrame with 100 ramdom points distributed
within 13 different polygons.
> randp
coordinates Point_ID Polygon_ ID
0 (690926.8, 7522595) 1_hs 13
1 (696727.1, 7576122) 2_hs 6
...
...
98 (728199.9, 7549810) 99_hs 12
99 (723428.1, 7545891) 100_hs 12
I need to calculate the shortest distance between points belonging to
different polygons. Basically I'd like to do what nndist {spatstat}
does. The difference is that the distance should be calculated between
groups of points instead of within a group of points.
I tried to use "aggregate" as suggested below but it didn't work out for me.
http://www.inside-r.org/packages/cran/spatstat/docs/nndist
Please find my try below:
> randp.df<-data.frame(randp)
> randp.hs.df
Point_ID coords.x1 coords.x2 Polygon_ ID
0 1_hs 690926.8 7522595 13
1 2_hs 696727.1 7576122 6
2 3_hs 723480.7 7546594 12
library(spatstat)
# Calculate nearest neighbors within a polygon
> nn.within.pol<-nndist(randp.df[,c(2,3)],by=marks(randp.df$Polygon_ID))
> nn.within.pol
[1] 2579.42199 1391.88915 59.85628 734.95108 734.95108
840.65125 957.47838 741.58160 955.26483 3307.59444 1361.64626
2682.70690
...
...
[97] 1349.88694 955.26483 3166.00894 705.25663
# Ok but these are not the distances I need
# Calculate nearest neighbors between polygons
nn.between.pol<-aggregate(nn.within.pol,
by=list(from=marks(randp.df$Polygon_ID)), min)
# Error in aggregate.data.frame(as.data.frame(x), ...) : arguments must
have same length
> nn.between.hs<-aggregate(randp.hs.df[,c(2,3)],
by=list(randp.df$Polygon_ID), nndist)
> nn.between.hs
The outcome is an asymmetric data frame (dim 13, 6) with a lot of empty
cells and values that look unlikely to be distances.
The result I'd like to get is a matrix (dim 100, 1) with the distances
between each random point and its nearest neighbor belonging to a
different polygon (i.e. its nearest neighbor having a different Polygon_ID).
Can someone kindly correct my script or suggest a function able to do
the job?
Cheers,
Ivan
More information about the R-sig-Geo
mailing list