diff --git a/DESCRIPTION b/DESCRIPTION index 6df63a4..23065b2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -35,9 +35,10 @@ Imports: Suggests: MASS, hexbin, - testthat + testthat, + sf License: GPL-2 LazyData: true -RoxygenNote: 7.1.0 +RoxygenNote: 7.1.2 Roxygen: list(markdown = TRUE) Encoding: UTF-8 diff --git a/NAMESPACE b/NAMESPACE index 9c782ac..ff5638a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -28,6 +28,7 @@ export(ggmap) export(ggmap_credentials) export(ggmap_hide_api_key) export(ggmap_show_api_key) +export(ggmap_to_webmercator) export(ggmapplot) export(google_account) export(google_client) @@ -120,6 +121,7 @@ importFrom(purrr,walk) importFrom(rlang,set_names) importFrom(scales,expand_range) importFrom(stats,asOneSidedFormula) +importFrom(stats,setNames) importFrom(stats,time) importFrom(stringr,str_c) importFrom(stringr,str_detect) diff --git a/R/ggmap_to_webmercator.R b/R/ggmap_to_webmercator.R new file mode 100644 index 0000000..d814e59 --- /dev/null +++ b/R/ggmap_to_webmercator.R @@ -0,0 +1,30 @@ +#' \code{ggmap_to_webmercator} change bounding box to match with sf objects (CRS needs to be 3857) +#' +#' @param map The map to update + +#' @export +#' @rdname ggmap_to_webmercator +#' @importFrom stats setNames +#' +ggmap_to_webmercator <- function(map) { + if (!inherits(map, "ggmap")) stop("map must be a ggmap object") + # Extract the bounding box (in lat/lon) from the ggmap to a numeric vector, + # and set the names to what sf::st_bbox expects: + map_bbox <- setNames(unlist(attr(map, "bb")), + c("ymin", "xmin", "ymax", "xmax")) + + # Coonvert the bbox to an sf polygon, transform it to 3857, + # and convert back to a bbox (convoluted, but it works) + if( !requireNamespace("sf") ) + stop("package sf is required to use this function") + + bbox_3857 <- sf::st_bbox(sf::st_transform(sf::st_as_sfc(sf::st_bbox(map_bbox, crs = 4326)), 3857)) + + # Overwrite the bbox of the ggmap object with the transformed coordinates + attr(map, "bb")$ll.lat <- bbox_3857["ymin"] + attr(map, "bb")$ll.lon <- bbox_3857["xmin"] + attr(map, "bb")$ur.lat <- bbox_3857["ymax"] + attr(map, "bb")$ur.lon <- bbox_3857["xmax"] + + map +} diff --git a/man/ggmap_to_webmercator.Rd b/man/ggmap_to_webmercator.Rd new file mode 100644 index 0000000..fce22c5 --- /dev/null +++ b/man/ggmap_to_webmercator.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ggmap_to_webmercator.R +\name{ggmap_to_webmercator} +\alias{ggmap_to_webmercator} +\title{\code{ggmap_to_webmercator} change bounding box to match with sf objects (CRS needs to be 3857)} +\usage{ +ggmap_to_webmercator(map) +} +\arguments{ +\item{map}{The map to update} +} +\description{ +\code{ggmap_to_webmercator} change bounding box to match with sf objects (CRS needs to be 3857) +} diff --git a/man/reexports.Rd b/man/reexports.Rd index f4533bc..6474921 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -11,6 +11,6 @@ These objects are imported from other packages. Follow the links below to see their documentation. \describe{ - \item{magrittr}{\code{\link[magrittr]{\%>\%}}} + \item{magrittr}{\code{\link[magrittr:pipe]{\%>\%}}} }}