minDistanceToCoast: Get the minumim distance to coast

View source: R/minDistanceToCoast.R

minDistanceToCoastR Documentation

Get the minumim distance to coast

Description

Get the minumim distance to coast

Usage

minDistanceToCoast(
  data,
  colLon = "lon",
  colLat = "lat",
  countryFilter = "peru",
  unit = "nm",
  multicore = FALSE,
  ncores = 1,
  out = c("value", "position")
)

Arguments

data

data.frame with coords that will be used to calculate min distance to coast line.

colLon

Name or position of column for longitude. As default, it will be lon.

colLat

Name or position of column for latitude. As default, it will be lat.

unit

Define the unit for outputs: nm (nautical miles), kilometers (km) or m (meters).

multicore

logical indicating whether to use multiple cores for calculate distances. See Details.

ncores

If multicore = TRUE, how many cores are you going to use?

out

character indicating what products are going to be returned: value or position. By default, it will return both as a list.

country

character. Define the country (or countries) used to perform the distance searching. See Details.

Details

This function uses internaly distance[terra] function, with argument longlat = TRUE.

If country is not defined (NULL), the internal algorithm will perform a search along the ENTIRE west coast of America: from Alaska to Chile. This might be (very) inefficient especially if the calculation is performed over many (thousands of) points, so it is suggested to define the country or countries with which you want to make the comparison.

If data is a matrix, colLon and colLat must be ONLY numeric values.

It is recommended to use multicore mode only when the database exceeds 2000 rows, less than this, single core mode have proved to be faster.

out argument allows to users to decide having between: the values (the values of minimum distance to coast), the positions (where this minimum distance where achieved in coast) or both.

Value

It returns a list with 2 levels: value, The minimum distance to coast line and position, the point where this minimum distance is reached.

Examples


n <- 100

allData <- data.frame(lon = runif(n, -80, -70), lat = runif(n, -18, -2),
                      stringsAsFactors = FALSE)

allData <- allData[!is.na(isopArea.assigner(allData)),]

minValues <- minDistanceToCoast(allData)

xlim <- c(-85, -70)
ylim <- c(-20, -2)

dev.new()
par(mar = c(2, 3, 1, 1), xaxs = "i", yaxs = "i")
plot(1, 1, pch = NA, axes = FALSE, xlab = NA, ylab = NA, xlim = xlim, ylim = ylim)

points(allData$lon, allData$lat, pch = 16, cex = 0.5, xlim = c(-85, -70), ylim = c(-20, -2))
lines(coastline$lon, coastline$lat)


for(i in 1:nrow(minValues$position)){
  lines(c(allData$lon[i], minValues$position$lon[i]), c(allData$lat[i], minValues$position$lat[i]),
        lty = "dotted", col = "red")
}

axis(side = 1, at = seq(xlim[1], xlim[2], length.out = 4),
     labels = getCoordsAxes(seq(xlim[1], xlim[2], length.out = 4), "lon"))
axis(side = 2, at = seq(ylim[1], ylim[2], length.out = 10),
     labels = getCoordsAxes(seq(ylim[1], ylim[2], length.out = 10), "lat"), las = 2)
box()

LuisLauM/ruisu documentation built on March 26, 2024, 8:23 a.m.