[R-sig-Geo] closest points between 2 polygons

Edzer Pebesma edzer.pebesma at uni-muenster.de
Wed Apr 17 23:44:34 CEST 2013


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:
> I have two polygons and I would like to know the coordinates of the closest
> points, where one is found on each polygon. I know how to get the shortest
> distance between two polygons, with gDistance from the rgeos package, but I
> guess I would like to know the coordinates that this distance is calculated
> from.
> 
> Any ideas?
> 
> thanks!
> -Pascal
> 
> 

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list