securityvisionnr/R/geocoordinates.R

43 lines
1.1 KiB
R
Raw Normal View History

2023-05-08 15:58:56 +00:00
#' DMS to Decimal
#'
#' Convert geocoordinates froom DMS format to decimal
#'
#' @param input
#'
#' @return
#' @export
#'
#' @examples
dms_to_decimal <- function(input) {
if(is.na(input)) {
return(NA)
}
else {
return(sp::char2dms(input, chd="d", chm="m", chs="s") |>
as.numeric())
}
}
#' Convert Semantic Wiki style coordinates to DMS
#'
#' @param dataset
#' @param geocoordinates_variable
#'
#' @return
#' @export
#'
#' @examples
get_decimal_coordinates <- function(dataset, geocoordinates_variable) {
dataset |>
mutate(
{{geocoordinates_variable}} := toupper({{geocoordinates_variable}}),
{{geocoordinates_variable}} := str_replace_all({{geocoordinates_variable}}, '°', "d"),
{{geocoordinates_variable}} := str_replace_all({{geocoordinates_variable}}, "'", "m"),
{{geocoordinates_variable}} := str_replace_all({{geocoordinates_variable}}, '"', "s")) |>
mutate(geo_split = stringr::str_split({{geocoordinates_variable}}, ",")) |>
rowwise() |>
mutate(latitude = dms_to_decimal(geo_split[1]),
longitude = dms_to_decimal(geo_split[2])) |>
select(-geo_split, -{{geocoordinates_variable}})
}