Skip to content
Prev 18042 / 29559 Next

closest points between 2 polygons

This may not be the most elegant solution. We seek the nearest points in
the first and tenth polygon of the nc sids data set:

library(maptools)
xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1])
proj4string(xx) = CRS("+proj=longlat +ellps=clrk66")
asPts = function(x) as(as(x, "SpatialLines"), "SpatialPoints")
pol1 = xx[1,]; pol2 = xx[10,]
d = spDists(asPts(pol1), asPts(pol2))
w = which(d == min(d), arr.ind = TRUE)
asPts(pol1)[w[1,1],]
asPts(pol2)[w[1,2],]

# or

coordinates(asPts(pol1)[w[1,1],])
coordinates(asPts(pol2)[w[1,2],])
On 04/17/2013 10:15 PM, Pascal Title wrote: