Someone in this server suggested me to use this code too. A while ago. I
just can't find the email
This code finds the closer between two set of points.

coords=coordinates(housing)

housing$x1=coords[,2]

housing$y1=coords[,1]

summary(housing)

DBout1810<-NULL

date()

for(i in 1:length(housing))

{

dists <- as.data.frame(cbind(Longitude=tempPoints[,2], Latitude=tempPoints[,
1], distance=spDistsN1(as.matrix(tempPoints), coords[i,], longlat=T)))
#calculate
the distances of the point of interest from the sample points on the segment

dists <- dists[order(dists$distance),]  #sort by distance

P1 <- dists[1,"distance"] # get the closest point on the segment to the
watch site

distroad=P1*0.621371

distroad

DBout1810<-rbind(DBout1810,distroad)

}

hope this helps

Juan


On Thu, Apr 18, 2013 at 1:28 PM, Robert J. Hijmans <r.hijmans@gmail.com>wrote:

> Pascal,
>
> For polygons with a longitude/latitude CRS only, I think the function below
> should work (based on Edzer's example and Bastien's suggestion to use
> dist2Line (which, by the way, also works for polygons)).
>
>
>
> dfun <- function(p1, p2) {
> p1 <- makePoly(p1)
> p2 <- makePoly(p2)
> asPts = function(x) as(as(x, "SpatialLines"), "SpatialPoints")
> d = spDists(asPts(pol1), asPts(pol2))
> w = which(d == min(d), arr.ind = TRUE)
> xy1 <- coordinates(asPts(pol1)[w[1,1],])
> d1 <- dist2Line(xy1, p2)
> d2 <- dist2Line(d1[, 2:3,drop=FALSE], p1)
> d1 <- dist2Line(d2[, 2:3,drop=FALSE], p2)
> rbind(d1, d2)[,-1]
> }
>
>
> library(maptools)
> library(geosphere)
> xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1])
> proj4string(xx) = CRS("+proj=longlat +ellps=clrk66")
> pol1 = xx[1,]
> pol2 = xx[10,]
>
> xy <- dfun(pol1, pol2)
>
> plot(xx, xlim=c(-82, -81))
> plot(pol1, add=T, col='red')
> plot(pol2, add=T, col='red')
> points(xy, col='blue', pch=20)
>
>
>
> On Thu, Apr 18, 2013 at 5:39 AM, <Bastien.Ferland-Raymond@mrn.gouv.qc.ca
> >wrote:
>
> > Hello,
> >
> > I don't have a clear solution for your problem, but I'll try to give you
> > ideas to where to look that may help you.  If I understand well your
> > problem, the difficulty comes from the fact that you have two POLYGONS
> and
> > that you are looking for the COORDINATES.
> >
> > You could transform your polygons in polylines with something like:
> >
> > ####
> > polygon2polyline <- function(poly.shape) {
> >                          # On donne un object de classe
> > SpatialPolygonsDataFrame
> > coo <- lapply((slot(poly.shape,"polygons")),function(xx)
> lapply(xx@Polygons,
> > slot, "coords"))       # Sort les coordonnées des polygones
> > IDs <- sapply(slot(poly.shape, "polygons"), slot, "ID")
> >                           # Sort les IDs des polygons
> >
> > shape.line <- vector(mode="list",length=length(coo))
> >                          # boucle qui crée la liste de Lines.  Pourrait
> > être changé par un Lapply si on trouve le moyen de gérer les IDs
> > for (i in 1:length(coo)){
> > shape.line[[i]] <- Lines(lapply(coo[[i]], Line),ID=IDs[i])
> > }
> > shape <- SpatialLinesDataFrame(SpatialLines(shape.line,proj4string =
> > CRS(proj4string(poly.shape))), data=poly.shape@data)  # Crée la nouvelle
> > shape de lignes
> > shape
> >                           # sort un object de classe
> SpatialLinesDataFrame
> > }
> > ####
> >
> > Than, using polylines may help with some functions like:  dist2line()
> from
> > the geosphere package.  There is also some homemade function that do a
> > similar task available at:
> >
> http://geotux.tuxfamily.org/index.php/en/geo-blogs/item/296-snapping-points-to-lines-in-r
> >
> > However, both of those functions require a line and a point...   You have
> > two lines.  Maybe you can modify them?
> >
> > Another idea, which would not give you the real answer but close and
> > requires tons of computing power so it may not help you much is to use
> the
> > costDistance() function of gdistance.
> >
> > 1) you transform both your polygons to lines,
> > 2) you select one of the polygon,
> > 3) you rasterize it, finer is better,
> > 4) you calculate the costDistance from this raster,
> > 5) you superpose the second polygons on the costDistance raster,
> > 6) you identify every cell from the raster that touches the line of the
> > second polygon,
> > 7) you identify the cell with the minimum distance.  The coordinates from
> > the second polygon that is the closest to the first polygon is there.
> > 8) inverse the process or use the functions proposed before to identify
> > the closest point from the first polygon.
> >
> > As I said, it's very complicated for what should be a simple task,
> however
> > it's the best I can think of right now.
> >
> > Good luck,
> >
> > Bastien Ferland-Raymond
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo@r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
>
>         [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>


-- 
Juan Tomás Sayago Gómez
Graduate Research Assistant
West Virginia University - RRI
886 Chestnut Ridge Road, Room 520
P.O. Box 6825
Morgantown, WV 26506-6825

	[[alternative HTML version deleted]]

