[R-sig-Geo] Distance from coast

Michael Sumner mdsumner at gmail.com
Fri Sep 23 06:29:07 CEST 2011


Here's a really simple example using the default low res longlat
coastline in the maps package. It should work for any coastal dataset
that can extract the raw coordinates from, though there's no attempts
at efficiency. spDistsN1 is really fast and works well for this
though, be careful to use longlat argument correctly.

library(sp)

library(maps)

## single point for a simple test
pts <- matrix(c(-88, 36), ncol = 2)

## simple map data
mp <- map("usa", plot = FALSE)

## convert coords to matrix and dump NA
xy.coast <- cbind(mp$x, mp$y)[!is.na(mp$x), ]


## container for all the nearest points matching the input
closest.points <- matrix(0, ncol = 2, nrow = nrow(pts))

for (i in 1:nrow(pts)) {
   closest.points[i, 1:2] <- xy.coast[which.min(spDistsN1(xy.coast,
pts, longlat = TRUE)), ]
}

map(mp)
points(pts)
points(closest.points)

Cheers, Mike.
On Fri, Sep 23, 2011 at 1:11 PM, Sandeep Patil
<sandeep.coepcivil at gmail.com> wrote:
> Hello does anyone know of any R -script or function which will give the
> value of nearest distance from coast of a point
> in USA.
>
> I have a number of latitude and longitude values spread evenly across USA
> and i want to find the nearest distance from coast for
>
> each of these points. If not a function can anyone suggest me a strong
> library for geographical data manipulation.
>
> Thanks a lot
>
> Sandeep
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
Michael Sumner
Institute for Marine and Antarctic Studies, University of Tasmania
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-sig-Geo mailing list