dkahle / dkahle/ggmap

How to use ggmap programmatically without guessing zoom level?

Open
#352 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
R
Stars
778
Forks
234
PR merge metrics
No merged PRs in 30d

Description

`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](https://github.com/dkahle/ggmap/assets/11711566/94c0c07c-f6c4-46a6-ab54-70811ba0ebd8)

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](https://github.com/dkahle/ggmap/assets/11711566/f9d9471a-ded0-420d-902c-e9570932d0af)

**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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.