Find nearest land for some (red) dots. Demonstrates which.min()
function, extraction of coastline data, and projected mapping.
library(oce)
## Loading required package: gsw
## Loading required package: testthat
data(coastlineWorld)
nearestLand <- function(lon, lat)
{
clon <- coastlineWorld[["longitude"]]
clat <- coastlineWorld[["latitude"]]
d <- geodDist(clon, clat, lon, lat)
min <- which.min(d)
list(lon=clon[min], lat=clat[min])
}
par(mar=rep(1,4))
mapPlot(coastlineWorld, fill='lightgray')
theta <- seq(0, 2*pi, pi/8)
lon0 <- -45
lat0 <- 30
L <- 10 # lat deg
LL <- L / cos(lat0*pi/180)
lons <- lon0 + LL * sin(theta)
lats <- lat0 + L * cos(theta)
for (i in seq_along(lons)) {
nl <- nearestLand(lons[i], lats[i])
mapLines(c(lons[i], nl$lon), c(lats[i], nl$lat), col='blue', lwd=1)
mapPoints(lons[i], lats[i], col='red', pch=20, cex=0.5)
}