calculating shortest distance from a set of SpatialPoints to a polygon
On Wed, 13 Jun 2012, Malcolm Fairbrother wrote:
Dear list, I have a large set of locations scattered over a land area covering about 200,000 km?. That area borders the sea, and I'd like to find the shortest distance from each point location to the sea/coast. Point locations are in lat/long. I've turned the locations into a SpatialPoints object, with a proj4string. And I have a SpatialPolygons object consisting of a single polygon, defining the sea areas (same proj4string as for the SpatialPoints object). My problem is I'm not clear on the next step. I tried using "gDistance" from the "rgeos" package: dists <- gDistance(locations, sea) but got two warnings: Warning messages: 1: In RGEOSDistanceFunc(spgeom1, spgeom2, byid, "rgeos_distance") : Spatial object 1 is not projected; GEOS expects planar coordinates 2: In RGEOSDistanceFunc(spgeom1, spgeom2, byid, "rgeos_distance") : Spatial object 2 is not projected; GEOS expects planar coordinates
Wait a moment! The warnings are there for a purpose (and would also affect the use of functions in spatstat, which also assumes planar coordinates). Never delete important data (in this case the metadata of your points, saying which spatial reference system they possess). If your data are in geographical coordinates, you need to state that specifically, and look for suitable functions. One such is perhaps dist2Line() in geosphere. The alternative (and I advise doing both) is to project your points and coastine, and use gDistance(locations, sea, byid=TRUE), which will return distances in the metric of the projection (probably metres).
So should I set the proj4string to NA? And/or is gDistance even the right tool for this job? Maybe it's designed for instances where distances are small, not in the hundreds of kilometres?
Never set proj4string to NA if it is known (NA means not available, it doesn't mean ignore). The function works on planar geometries, and projection distortions for well-chosed projections at that scale are not large. Usually, wrongly defined datum metadata are more of a problem. Roger
Any advice on how to do this appropriately would be much appreciated. - Malcolm
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand Department of Economics, NHH Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no