Ggmap, ggimage ... - Saving and loading maps in R

In my current R project, I am working with a package ggmapfor loading and displaying maps. My concern is that you need internet access to download maps from Google, etc. I would like to download the map from the Internet and save it so that I can work offline. An ideal solution is to minimize the amount of code between get_map()(or the team associated with it) and use the saved map inside ggmap()(or the corresponding command).

I already know that it get_map()has an attribute filenamefor saving maps as an image, and I also know the method ggimage()(I never worked with it.)

Is there an easy way to save such a map, or the two best tools available - and if so, how can I use them effectively?

+4
source share
1 answer

I came across the same issue, expecting the same cards to load dozens of times per launch.

You can save any object in R, but it took me a little time to figure out the nuances of saving the map inside the function. Here is what I came up with - I would like to see if there are any improvements that can be made.

Locale ( -, ), . , , . , Google .

, , , .GlobalEnv , R .

load.map <- function(Locale, Lon, Lat, MapZoom){
  MapName <- paste("Map", gsub(" ", "", Locale), MapZoom, sep = "")

  FileName <- paste(MapName,".RData", sep = "")
  if (file.exists(FileName) & ReloadMaps == 0)
  {
    load(FileName, envir = .GlobalEnv)
  } else 
  {
    Map <- get_googlemap(center=c(lon = Lon, lat = Lat), zoom=MapZoom, scale = 2,
        size = c(640, 640), maptype = "roadmap", color = "color", format = "png8")
    assign(MapName, Map,  envir = .GlobalEnv)
    save(list = MapName, file = FileName, envir = .GlobalEnv)
  }  
} 
+3

All Articles