-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Type: Package | ||
Package: MazamaLocationUtils | ||
Version: 0.3.4 | ||
Version: 0.3.6 | ||
Title: Manage Spatial Metadata for Known Locations | ||
Authors@R: c( | ||
person("Jonathan", "Callahan", email="[email protected]", role=c("aut","cre")), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#' @rdname table_getDistanceFromTarget | ||
#' @export | ||
#' @importFrom MazamaCoreUtils stopIfNull | ||
#' @importFrom rlang .data | ||
#' | ||
#' @title Return distances and directions from a target location to known locations | ||
#' | ||
#' @param locationTbl Tibble of known locations. | ||
#' @param longitude Target longitude in decimal degrees E. | ||
#' @param latitude Target latitude in decimal degrees N. | ||
#' @param measure One of "haversine" "vincenty", "geodesic", or "cheap" | ||
#' specifying desired method of geodesic distance calculation. | ||
#' | ||
#' @return Tibble of distances in meters and cardinal directions from a target location. | ||
#' | ||
#' @description Returns a tibble with the same number of rows as \code{locationTbl} | ||
#' containing the distance and direction from the target location specified by | ||
#' \code{longitude} and \code{latitude} to each known location found in | ||
#' \code{locationTbl}. | ||
#' | ||
#' @note Only a single target location is allowed. | ||
#' | ||
#' @examples | ||
#' library(MazamaLocationUtils) | ||
#' | ||
#' locationTbl <- get(data("wa_monitors_500")) | ||
#' | ||
#' locationTbl %>% | ||
#' table_getDistanceFromTarget( | ||
#' longitude = -117.3647, | ||
#' latitude = 47.6725 | ||
#' ) %>% | ||
#' dplyr::glimpse() | ||
#' | ||
|
||
|
||
table_getDistanceFromTarget <- function( | ||
locationTbl = NULL, | ||
longitude = NULL, | ||
latitude = NULL, | ||
measure = c("geodesic", "haversine", "vincenty", "cheap") | ||
) { | ||
|
||
# ----- Validate parameters -------------------------------------------------- | ||
|
||
MazamaLocationUtils::validateLocationTbl(locationTbl, locationOnly = TRUE) | ||
MazamaLocationUtils::validateLonLat(longitude, latitude) | ||
measure <- match.arg(measure) | ||
|
||
if ( length(longitude) > 1 || length(latitude) > 1 ) | ||
stop("Only a single target location is allowed.") | ||
|
||
# ----- Calculate distances -------------------------------------------------- | ||
|
||
distanceMatrix <- | ||
geodist::geodist( | ||
y = cbind( | ||
"x" = longitude, | ||
"y" = latitude | ||
), | ||
x = cbind( | ||
"x" = locationTbl$longitude, | ||
"y" = locationTbl$latitude | ||
), | ||
paired = FALSE, | ||
sequential = FALSE, | ||
pad = FALSE, | ||
measure = measure | ||
) | ||
|
||
# NOTE: distanceMatrix is nrow(locationTbl) X 1 | ||
|
||
returnTbl <- | ||
locationTbl %>% | ||
dplyr::select(c("locationID")) %>% | ||
dplyr::mutate( | ||
distanceFromTarget = round(distanceMatrix[,1]) | ||
) | ||
|
||
# ----- Add direction -------------------------------------------------------- | ||
|
||
# https://stackoverflow.com/questions/30794729/find-angle-from-two-points-formula | ||
|
||
x1 <- longitude * pi/180 | ||
x2 <- locationTbl$longitude * pi/180 | ||
y1 <- latitude * pi/180 | ||
y2 <- locationTbl$latitude * pi/180 | ||
|
||
angle = atan2(y2 - y1, x2 - x1) * 180/pi | ||
|
||
# NOTE: Mathematical angle is zero facing East and increases counter-clockwise | ||
# NOTE: Cover everything between -180:180 | ||
|
||
returnTbl$directionFromTarget <- | ||
cut( | ||
angle, | ||
breaks = c(-202.5, -157.5, -112.5, -67.5, -22.5, 22.5, 67.5, 112.5, 157.5, 202.5), | ||
labels = c("W", "SW", "S", "SE", "E", "NE", "N", "NW", "W") | ||
) %>% | ||
as.character() | ||
|
||
# ----- Return --------------------------------------------------------------- | ||
|
||
return(returnTbl) | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.