From 748fec4e5757d083caf12ac5bedb67c47ab3cf01 Mon Sep 17 00:00:00 2001 From: gregleleu Date: Sun, 6 Oct 2019 14:34:39 -0400 Subject: [PATCH 1/4] Adding function to change bounding box to webmercator to be compatible with sf (could be an option of get_map...) --- DESCRIPTION | 3 ++- NAMESPACE | 3 +++ R/ggmap_to_webmercator.R | 28 ++++++++++++++++++++++++++++ man/ggmap_to_webmercator.Rd | 14 ++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 R/ggmap_to_webmercator.R create mode 100644 man/ggmap_to_webmercator.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 6df63a48..24aad796 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,7 +31,8 @@ Imports: magrittr, tibble, tidyr, - rlang + rlang, + sf Suggests: MASS, hexbin, diff --git a/NAMESPACE b/NAMESPACE index 9c782ac5..2dff3b52 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) @@ -64,6 +65,7 @@ export(theme_nothing) export(trek) export(write_geocode_cache) import(ggplot2) +import(sf) importFrom(RgoogleMaps,XY2LatLon) importFrom(bitops,bitAnd) importFrom(bitops,bitOr) @@ -120,6 +122,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 00000000..730f21fb --- /dev/null +++ b/R/ggmap_to_webmercator.R @@ -0,0 +1,28 @@ +#' \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 +#' @import sf +#' @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) + 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 00000000..fce22c50 --- /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) +} From d65e19f3f68729b9594338311b87c1b5e910de90 Mon Sep 17 00:00:00 2001 From: gregleleu Date: Fri, 22 Oct 2021 10:55:28 -0400 Subject: [PATCH 2/4] Remove import of sf --- NAMESPACE | 1 - R/ggmap_to_webmercator.R | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 2dff3b52..ff5638ab 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -65,7 +65,6 @@ export(theme_nothing) export(trek) export(write_geocode_cache) import(ggplot2) -import(sf) importFrom(RgoogleMaps,XY2LatLon) importFrom(bitops,bitAnd) importFrom(bitops,bitOr) diff --git a/R/ggmap_to_webmercator.R b/R/ggmap_to_webmercator.R index 730f21fb..d814e59b 100644 --- a/R/ggmap_to_webmercator.R +++ b/R/ggmap_to_webmercator.R @@ -4,7 +4,6 @@ #' @export #' @rdname ggmap_to_webmercator -#' @import sf #' @importFrom stats setNames #' ggmap_to_webmercator <- function(map) { @@ -16,6 +15,9 @@ ggmap_to_webmercator <- function(map) { # 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 From d43631701a565d3e9a3a5560ec41cbe685234293 Mon Sep 17 00:00:00 2001 From: gregleleu Date: Fri, 22 Oct 2021 10:55:55 -0400 Subject: [PATCH 3/4] Update roxygen --- DESCRIPTION | 2 +- man/reexports.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 24aad796..1e10c7bd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -39,6 +39,6 @@ Suggests: testthat License: GPL-2 LazyData: true -RoxygenNote: 7.1.0 +RoxygenNote: 7.1.2 Roxygen: list(markdown = TRUE) Encoding: UTF-8 diff --git a/man/reexports.Rd b/man/reexports.Rd index f4533bc8..64749216 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]{\%>\%}}} }} From e0817e7b29097e18a914e866a7244cf23c7d0f83 Mon Sep 17 00:00:00 2001 From: gregleleu Date: Fri, 22 Oct 2021 11:02:15 -0400 Subject: [PATCH 4/4] fix description for sf package suggestion --- DESCRIPTION | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1e10c7bd..23065b2d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,12 +31,12 @@ Imports: magrittr, tibble, tidyr, - rlang, - sf + rlang Suggests: MASS, hexbin, - testthat + testthat, + sf License: GPL-2 LazyData: true RoxygenNote: 7.1.2