Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webmercator fix #328

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions R/ggmap_to_webmercator.R
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions man/ggmap_to_webmercator.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/reexports.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.