#' coord2dec
#'
#' Convert coordinates from various formats to decimal.
#'
#' Supported formats include W48d32m31.000, 48d32m31.000W,
#' 48 32 31.000, 48 32 31.000N, W48 32 31.000, N048d32m31.000
#'
#' @param x coordinate to convert
#'
#' @return dataframe containing identifier, lon, and lat
#' @export
#' @importFrom foreach foreach %do%
#' @importFrom measurements conv_unit
coord2dec <- function(x) {
crd <- gsub("(d|m)", " ", x)
crd <- gsub("(.+)((W|E))", "\\2\\1", crd)
crd <- gsub("(N|E)", "", crd)
crd <- gsub("(S|W)", "-", crd)
crd <- gsub("(\\d)-", "\\1 ", crd)
crd <- gsub("(\\s,-)0", "\\1 ", crd)
conv_unit(crd, from = 'deg_min_sec', to = 'dec_deg')
}
#' distNm
#'
#' Find distance between two points in nautical miles by
#' haversine formula.
#'
#'
#'
#' @param lat1 Latitude of the first point
#' @param lon1 Longitude of the first point
#' @param lat2 Latitude of the second point
#' @param lon2 Longitude of the second point
#'
#' @return Distance between points in nautical miles
#' @export
distNm <- function(lat1, lon1, lat2, lon2) {
r <- 6371 # Radius of the earth in km
dLat <- deg2rad(lat2-lat1) # deg2rad below
dLon <- deg2rad(lon2-lon1)
a <-
sin(dLat/2) * sin(dLat/2) +
cos(deg2rad(lat1)) * cos(deg2rad(lat2)) *
sin(dLon/2) * sin(dLon/2)
c <- 2 * atan2(sqrt(a), sqrt(1-a))
d <- r * c; # distance in km
d * 0.539957
}
# internal. convert degrees to radians
deg2rad <- function (deg) {
deg * (pi/180)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.