R/utils.R In spacey: Easily Obtain Spatial Data and Make Better Maps

```#' Convert decimal degrees to radians
#'
#' @param deg A vector of values, in decimal degrees, to convert to radians
#'
#' @return A vector of the same length in radians
#'
#' @examples
#' @export
stopifnot(is.numeric(deg))
deg * base::pi / 180
}

#'
#' @param rad A vector of values, in radians, to convert to decimal degrees
#'
#' @return A vector of the same length in decimal degrees
#'
#' @examples
#' @export
}

#' Extract latitude and longitude from a provided object
#'
#' This is an internal utility function to convert bounding boxes into
#' coordinate pairs.
#'
#' @param coords An object to try and extract coordinates from
#'
#' @keywords internal
#'
#' @return A vector of length 2 containing object latitude and longitude
#'
#' @examples
#' extract_coords(get_centroid_bounding_box(c(
#'   "lat" = 44.121268,
#'   "lng" = -73.903734
#' ),
#' distance = 10
#' )[[1]])
#' @export
extract_coords <- function(coords) {
if (all(names(coords %in% c(
"lat",
"lng",
"long",
"latitude",
"longitude"
)))) {
if (sum(names(coords) %in% c("lat", "latitude")) != 1) {
stop("Couldn't infer latitude variable -- try passing a value to lat.")
} else if (sum(names(coords) %in% c("lng", "long", "longitude")) != 1) {
stop("Couldn't infer longitude variable -- try passing a value to lng.")
} else {
output <- c(
coords[names(coords) %in% c("lat", "latitude")],
coords[names(coords) %in% c("lng", "long", "longitude")]
)
names(output) <- c("lat", "lng")
return(output)
}
} else {
stop("Couldn't determine lat/lng values.")
}
}
```

Try the spacey package in your browser

Any scripts or data that you put into this service are public.

spacey documentation built on March 15, 2020, 1:07 a.m.