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

How to use ggmap programmatically without guessing zoom level? #352

Open
kevinwolz opened this issue Aug 29, 2023 · 0 comments
Open

How to use ggmap programmatically without guessing zoom level? #352

kevinwolz opened this issue Aug 29, 2023 · 0 comments

Comments

@kevinwolz
Copy link

get_map() requires the selection of a zoom level in order to retrieve Google map tiles. The a priori selection of zoom level seems to effectively prevent the application of ggmap programmatically with areas of interest of unknown sizes.

For example, using a dataset in the examples provided in the get_map() documentation and zoom level 8, you get a map that includes a lot of area beyond the bounding box that was supplied to get_map().

library(ggplot2)
library(ggmap)

data(zips)
zips$area <- NULL # data has two columns named the same

zips.bbox <- c(left   = min(zips$lon), 
               bottom = min(zips$lat),
               right  = max(zips$lon), 
               top    = max(zips$lat))
class(zips.bbox) <- "bbox"

gg.map <- get_map(location = zips.bbox, 
                  maptype  = "satellite", 
                  zoom     = 8)

ggmap(gg.map) +
  geom_polygon(aes(x = lon, y = lat, group = plotOrder),
               data = zips, 
               colour = "white", 
               fill = "red", 
               alpha = .2)

image

But then if we do the same thing but with zoom level 10 to try to get a tighter fit, we overshoot and get_map() does not bring in sufficient tiles at that zoom level to cover the entire bounding box.
image

Is there a way to get the correct amount (i.e that exactly matches the supplied bounding box) of satellite image brought in on-the-fly without the user guessing the zoom level?

I can think of some ways to, based on the known size of Google tiles at various zoom levels, to come up with a way to auto-detect the highest zoom level that encompasses the whole area of interest. But even that is only going to get you so far, as that zoom level may include a ton of imagery outside of the area of interest. And that also forces you to use lower zoom levels (and therefor lower resolution image tiles) than you may want to. The ideal approach would (1) allow specification of whatever zoom level you wanted and (2) ensure that all of the necessary tiles were downloaded to cover the entire area of interest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant