简体   繁体   English

使用ggmap缓存地图

[英]Cache maps using ggmap

I'm plotting maps using the ggmap package. 我正在使用ggmap包绘制地图。 To download a map over the internet, I can use this code: 要通过互联网下载地图,我可以使用以下代码:

library(ggmap)
get_map(location = c(-1.81, 55.655), zoom = 12, maptype = "hybrid")

Is there a way to avoid downloading maps over the internet, and instead import a .png file from a local folder? 有没有办法避免通过互联网下载地图,而是从本地文件夹导入.png文件? Or in other words, download maps once, cache the .png and thereafter import the .png from a local folder? 或者换句话说,下载地图一次,缓存.png,然后从本地文件夹导入.png? My connection is rather slow, I constantly redownloading the same base map is wasting valuable time. 我的连接速度很慢,我不断重新加载相同的基本地图浪费了宝贵的时间。

As get_map returns an R object, you can save that to disk and reuse later if you wish: get_map返回一个R对象时,您可以将其保存到磁盘并在以后重复使用,如果您愿意:

> x <- get_map(location = c(-1.81, 55.655), zoom = 12, maptype = "hybrid")
Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=55.655,-1.81&zoom=12&size=%20640x640&scale=%202&maptype=hybrid&sensor=false
Google Maps API Terms of Service : http://developers.google.com/maps/terms
> str(x)
 chr [1:1280, 1:1280] "#122C38" "#122C38" "#122C38" "#122C38" ...
 - attr(*, "class")= chr [1:2] "ggmap" "raster"
 - attr(*, "bb")='data.frame':  1 obs. of  4 variables:
  ..$ ll.lat: num 55.6
  ..$ ll.lon: num -1.92
  ..$ ur.lat: num 55.7
  ..$ ur.lon: num -1.7

So simply write x to your disk with saveRDS , and load that via readRDS even from another R session. 因此,只需使用saveRDSx写入磁盘,然后通过readRDS加载,甚至从另一个R会话加载。 POC demo: POC演示:

> t <- tempfile()
> saveRDS(x, file = t)
> x <- readRDS(t)
> ggmap(x)

In the 2+ years that have passed since the question was asked, new releases of ggmap have come out. 在提出问题后的2年多时间里, ggmap新版本已经问世。 The package now directly supports caching of downloads. 该软件包现在直接支持下载缓存。 However, when caching Google Maps, there are some conditions one should be aware of; 但是,在缓存Google地图时,应该注意一些条件; a link to the terms of service appears when ggmap is attached. 附加ggmap会显示ggmap服务条款的链接。

The general get_map function doesn't have an option for saving the downloaded Google Map tiles, but it can be done with the dedicated get_googlemap function, by setting archiving = TRUE . 常规get_map函数没有用于保存下载的Google Map图块的选项,但可以通过设置archiving = TRUE使用专用的get_googlemap函数来完成。 The manual has a note about the user having to accept the Google Maps terms of service (ToS) in this case (too). 手册中有关于用户必须接受Google地图服务条款(ToS)的说明(也是)。 In my view, the most obvious limitation in the ToS (September 21, 2015) is that the map content must not be stored for more than 30 days. 在我看来,ToS(2015年9月21日)中最明显的限制是地图内容不得存储超过30天。 If you accept that, the following code should work. 如果您接受,则以下代码应该有效。

library(ggmap)
## get_map() as in the question
foo1 <- get_map(location = c(-1.81, 55.655), zoom = 12, maptype = "hybrid")

## This will store map tiles locally, used by any repeated calls
foo2 <- get_googlemap(center = c(-1.81, 55.655), zoom = 12,
                      maptype = "hybrid", archiving = TRUE, force = FALSE)

identical(foo1, foo2) # TRUE

Other map sources may have more permissive terms or licenses. 其他地图来源可能有更多的许可条款或许可。 For example, to use Stamen Maps : 例如,要使用Stamen Maps

## Get roughly the same area as in the Google map
bbox <- c(left=-1.88, bottom=55.625, right=-1.74, top=55.685)
## Broken in ggmap 2.6.1, try GitHub version (which may have other problems)
foo3 <- get_map(location = bbox, zoom = 13, crop = FALSE,
                source = "stamen", maptype = "terrain", force = FALSE)

## Compare the two map sources / types
dev.new()
print(ggmap(foo2))
dev.new()
print(ggmap(foo3))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM