3

I have a piece of code which all of the sudden stopped working. As simple as that. I found some vague answers on SO, but nothing is helping in my case.

library(ggmap)
myLocation <- c(21.5, -18.5, 34, -8)

myMap <- get_map(location=myLocation,
                 source="google", maptype="terrain", crop=FALSE, color="bw")

I get the following error:

Warning: bounding box given to google - spatial extent only approximate.
converting bounding box to center/zoom specification. (experimental)
Error in download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") : 
  cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=-13.25,27.75&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false'
In addition: Warning message:
In download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") :
  cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=-13.25,27.75&zoom=6&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false': HTTP status was '403 Forbidden'

Tried this line, but didn't help:

options(download.file.method = "curl")

If I try osm - also problems:

osmmap <- get_openstreetmap(bbox = c(left = 21.5, bottom = -18.5, right =
                                   34, top = 8), scale = 7, color = c("bw"))
Error: map grabbing failed - see details in ?get_openstreetmap.
In addition: There were 16 warnings (use warnings() to see them)

Any idea what to do?

MIH
  • 1,083
  • 3
  • 14
  • 26
  • Google now requires users to register for and use an api key, see this [post](https://stackoverflow.com/questions/19827598/error-in-get-map-using-ggmap-in-r). The same function for OSM seems to be very [resource intensive](https://stackoverflow.com/questions/41526370/400-bad-request-with-ggmap), so it doesn't run – Carlos Xavier Bonilla Oct 04 '18 at 16:38
  • @CarlosBonilla Thanks for response! How to do this? How to register and use an api key? I do not see the directions in the post you pointed to. help is much appreciated. – MIH Oct 04 '18 at 21:33

2 Answers2

4

So there are two things here that are causing problems.

  1. ggmap is using Google Maps as its standard map source. The Google Maps API that ggmap is connecting to needs a registered Static Maps API key. You can get a (free) API key here, though note that you will have to register billing to be able to use one (if you don't Maps API requests will return an error). Once you have that set up, you will need to register it in every new session via register_google(key = "...")

enter image description here

  1. The Google Maps API that ggmap is connecting to needs lon/lat coordinates (e.g., location = c(-75.1636077,39.9524175)) to fully run.

So, the full code for you would be:

library(rjson)
library(digest)
library(glue)
library(devtools)
if(!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("dkahle/ggmap", ref = "tidyup")
library(ggmap)

register_google(key = "...",  # your Static Maps API key
                account_type = "standard")

map <- get_map(location = c(-75.1636077,39.9524175), zoom = 13)

ggmap(map)

enter image description here

1

I have similar issue when I'm downloading maps using get_map (403 Forbidden and OVER QUERY LIMIT) - I solved it by using dput to save the map on the computer and dget to load the map.

map <- get_map(location = myLocation, source="google", maptype="terrain", crop=FALSE, color="bw")
dput(map, file = "myMaps")
map <- dget(file = "myMaps")

Now if you have multiple maps to download and a high ratio of errors, you can use a loop with a tryCatch to perform the download.

myLocation <- c(21.5, -18.5, 34, -8)

getMap <- function(loc){
  map <- get_map(location = loc, source="google", maptype="terrain", crop=FALSE, color="bw")
  i <<- 0
  return(map)
}

i <- -1
c <- 0 # avoid infinite loop

while(i < 0 & c < 20){
  tryCatch(map <- getMap(myLocation), error = function(w){
    i <- -1
  })
  c <- c+1
}
gc_
  • 111
  • 5
  • sadly I am unable to create the `map` object in the first place, thus `dput()` won't work – MIH Oct 05 '18 at 11:07