I Del Toro
I Del Toro

Reputation: 943

Calculating distance to nearest shore from multiple GPS coordinates

I have tried using the response to this question to solve this problem but I cannot apply it in my case since I have many coordinates distributed at a global scale.

Does anyone have a way to calculate the minimum distance in km from a series of points to the nearest shore using a loop? This is a subset of the points I am using (DATA HERE)

#setwd and load directories----
setwd("your_wd")
  require (ggplot2)
  require (ggmap)

#build a map to plot distribution of sample sites ----
sites<-read.csv("sites.csv", header=T)

#Using GGPLOT, plot the Base World Map
mp <- NULL
mapWorld <- borders("world", colour="gray50", fill="gray50") # create a layer of borders
mp <- ggplot() +   mapWorld
#Now Layer the sites on top
Lon<-sites$x
Lat<-sites$y
mp <- mp+ geom_point(aes(x=Lon, y=Lat),color="blue", size=3) 
mp

Distribution of sites

Upvotes: 1

Views: 566

Answers (1)

Thierry
Thierry

Reputation: 18487

Have a look at the rgeos package

library(rgeos)
gDistance(spPoints, spPolygon, byid = TRUE)

spPoints will be a SpatialPoints object holding the coordinates. spPolygon will be a SpatialPolygons objects with landmasses. See the sp package. Make sure that both object have the same projection and have a sensible projection.

Upvotes: 2

Related Questions