[R-sig-Geo] Distances from points to coast do not make sense.

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Sep 18 11:51:36 CEST 2014

On Wed, Sep 17, 2014 at 10:13 PM, Agus Camacho <agus.camacho at gmail.com> wrote:
> Thanks again Robert,
> In case it is of help for any body, after trying several combinations, the
> fastest solution I got to calculate minimum distances to coast was:
> require(PBSmapping)
> wcoastline <- importShapefile("GSHHS_c_L1.shp")
> w=as.matrix(wcoastline[,c('X','Y')])
> # You will need x1, an object with: 1) long 2)lat coordinates
> require(sp)
>   distance=rep(NA,dim(x1)[1])
>   for(n in 1:dim(x1)[1])
>     {
>     distance[n]=min(spDistsN1(w,cbind(x[n,1], x1[n,2]),longlat=TRUE))
>   }
>   meandist=mean(distance)#
> }

 If I read that correctly, it doesn't do what you want it to do. It's
computing the distance to the nearest *vertex* of the coastline. A
long straight or simplified coastline could have vertices a long way
apart, and you'll get the wrong answer.

Here X is very close to the coastline but quite far from the vertices.
You'll get the vertex distance. Y is the same distance to the
coastline, but gets the correct small distance from the nearest

                   X                Y
 *-----------------------------------*--------------* (proportional font needed)

I think someone has already given you a solution that computes
distance to lines, it may be slower but it has the property of being


More information about the R-sig-Geo mailing list