Search code examples
rggmap

What's going on with get_map?


I started off today with about 230 map requests to googlemaps with get_map before I got an error (lost the original error). My first assumption was just that I used it beyond the limits of the api but I've tried using the open street maps api as a source an the error message I get still looks like get_map is using googlemaps. What's going on? I restarted my computer and tried to recreate the error. When I go to the url listed in the source I don't get my map, I get an small image that looks like I went over my api usage. But I've set my source as osm.

> library(ggmap)
Loading required package: ggplot2
Warning message:
package ‘ggmap’ was built under R version 3.1.1 
Loading required package: sp
> get_map(source = "osm")
Error in download.file(url, destfile = destfile, quiet = !messaging, mode = "wb") : 
  cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=29.763284,-95.363271&zoom=10&size=%20640x640&maptype=terrain&sensor=false'
In addition: Warning messages:
1: package ‘sp’ was built under R version 3.1.1 
2: In download.file(url, destfile = destfile, quiet = !messaging, mode = "wb") :
  cannot open: HTTP status was '403 Forbidden'
> detach(sp)
Error in detach(sp) : invalid 'name' argument
> detach("sp")
Error in detach("sp") : invalid 'name' argument
> get_map(source = "osm")
Error in download.file(url, destfile = destfile, quiet = !messaging, mode = "wb") : 
  cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=29.763284,-95.363271&zoom=10&size=%20640x640&maptype=terrain&sensor=false'
In addition: Warning message:
In download.file(url, destfile = destfile, quiet = !messaging, mode = "wb") :
  cannot open: HTTP status was '403 Forbidden'

This is the original loop I set up to get my map images.

for(i in seq(levels(named_trails$TRAIL_NAME))) {
one_trail <- named_trails[which(named_trails$TRAIL_NAME == levels(named_trails$TRAIL_NAME)[i]),]
map_center <- c(min(one_trail$long) - 0.02, min(one_trail$lat) - 0.02, max(one_trail$long) + 0.02, max(one_trail$lat) + 0.02)
anc_map <- get_map("anchorage, AK", source = "stamen")
p <- ggmap(anc_map) +
    geom_line(data = one_trail, aes(x = long, y = lat, color = TRAIL_NAME)) +
    ggtitle(levels(named_trails$TRAIL_NAME)[i]) +
    theme(legend.position="none")
ggsave(paste(i, ".png", sep = ""))
Sys.sleep(4)
}

windows 7 x86 R 3.1.0 Rstudio Version 0.98.932


Solution

  • The problem appears to be that when you specify source="osm" but the location type isn't a bounding box, then it makes a call to get_googlemap to get the lat/lon location before pulling the map from openstreetmap. So you need to make sure you pass in a vector of length 4 with lat/lon coordinates when using source="osm" to avoid any calls to the Google maps API. It seems OSM can't translate things like "anchorage, AK" into longitude and latitude values on its own.