Skip to content

Distance to (nearest) polygon

4 messages · Roger Bivand, Karl Ove Hufthammer, Robert J. Hijmans

#
Dear list members

Is there an easy and preferably fast way to measure the distance from a  
of (large) number of points to the nearest polygon? spDistsN1 seems to 
only want to measure the distance between points.

For example, I need the distance between points at sea to the nearest 
land area (as defined by for example the 'world' dataset).

It's not very important which polygon, e.g., country, the given distance 
is to. It would be nice if the 'distance' from points that are inside 
the polygons were zero or negative, but it's not required -- distance to 
the polygon border, e.g., shoreline, would be OK.
#
On Tue, 26 Jan 2010, Karl Ove Hufthammer wrote:

            
There are methods for point to line segment distances in spatstat. 
Further, for projected (planar) coordinates, this could be added to 
R-Forge rgeos; for geographical coordinates Boost ggl would be needed.

Roger

  
    
#
On Tue, 26 Jan 2010 16:27:23 +0100 (CET) Roger Bivand
<Roger.Bivand at nhh.no> wrote:

            
Thanks. The 'nncross' function in 'spatstat' does essentially do what I 
need. Unfortunately, I work with geographical coordinates (spanning 
about 15 degrees of latitude), so the results are not perfect (using 
plain long/lat coordinates), but they will probably be an adequate 
approximation.

BTW, for my application, I'm not interested in the distance per se, only 
in the points (e.g., boats) within a certain distance from the polygon 
(land). So I have also thought about a possible solution of using an 
expanded polygon, expanded in a certain number of kilometers outwards, 
and then using point-in-polygon (i.e., 'overlay') to find the points 
that are inside this new polygon. But I couldn't find a function to 
'grow' a polygon in this way.
#
if you have a high precision coast line relative to the distances you
want to estimate, you could compute the distance of each point to the
nearest node of your coast line.
Here illustrated with a quick hack example based on what Roger just
send in response to Agus:

library(raster) # to get the data
fra = getData('GADM', country='FRA', level=0)
fra = as(fra, 'SpatialLines')
crd <- coordinates(fra)
crd <- sapply(crd, function(x) do.call("rbind", x))
# returns a vector, not sure why, but this fixes it
crd <- matrix(crd, ncol=2)
# plot(crd, cex=0.1)
min(pointDistance(c(-5,46), crd, type='GreatCircle'))

Robert
On Wed, Jan 27, 2010 at 1:17 AM, Karl Ove Hufthammer <karl at huftis.org> wrote: