我正在使用rosm和ggspatial R包创建地图。ggosm函数易于用于基于所提供的空间对象提取基本地图层。下面是一个例子。
library(ggplot2)
library(sp)
library(rosm)
library(ggspatial)
ggosm() +
geom_spatial(longlake_waterdf, fill = NA, color = "black")

这个很好用。我可以将基本映射层更改为其他类型(可用类型请参见osm.types() )。下面是一个使用cartolight作为基本映射层的示例。
ggosm(type = "cartolight") +
geom_spatial(longlake_waterdf, fill = NA, color = "black")

这也很管用。现在我的问题是如何传递Thunderforest API密钥?如果我将该类型用作thunderforestoutdoors,则得到以下输出。
ggosm(type = "thunderforestoutdoors") +
geom_spatial(longlake_waterdf, fill = NA, color = "black")

显然,我需要雷霆森林API密钥,所以我已经从https://www.thunderforest.com/注册了一个API密钥。此页面(https://www.thunderforest.com/docs/apikeys/)演示如何使用API键。rosm的文档还显示,用户可以通过提供URL来定义地图块(参见?as.tile_source)。尽管如此,URL的一般结构似乎是:https://{s}.tile.thunderforest.com/outdoors/{z}/{x}/{y}.png?apikey=<insert-your-apikey-here>。我需要知道z、x和y (缩放级别和平铺号)来指定瓷砖。这是不可能的,因为我有很多空间对象要绘制,我需要ggosm来确定正确的缩放水平和瓷砖为我。如果有人能对此有所了解,那就太好了。
发布于 2017-10-24 04:26:45
解决方案:
笑--有一种方法可以用rosm 实现这一点,而不需要修改库。
rosm:: source_from_url_format()函数。这就是它设计的目的,它被记录在rosm包中。不可否认,略显短暂注意:为了完整性,我将介绍如何在使用共享代码时隐藏公共API密钥。
我希望这能有所帮助,至少应该让那些不需要修改库就想做你提议的事情的人更容易。
小心T。
示例用法: source_from_url_format()
备注:将<insert key>替换为来自雷霆森林网站的私钥。
if (!require(ggplot2)) install.packages("ggplot2")
if (!require(rosm)) install.packages("rosm")
if (!require(ggspatial)) install.packages("ggspatial")
if (!require(sp)) install.packages("sp")
thunderforest = source_from_url_format(
url_format = c('http://a.tile.thunderforest.com/landscape/${z}/${x}/${y}.png?apikey=<insert key>',
'http://b.tile.thunderforest.com/landscape/${z}/${x}/${y}.png?apikey=<insert key>',
'http://c.tile.thunderforest.com/landscape/${z}/${x}/${y}.png?apikey=<insert key>'),
attribution = "More on Thunderforest at http://www.thunderforest.com/"
)
ggosm(type = thunderforest) +
geom_spatial(longlake_waterdf, fill = NA, color = "black")示例运行时输出:
> if (!require(ggplot2)) install.packages("ggplot2")
> if (!require(rosm)) install.packages("rosm")
> if (!require(ggspatial)) install.packages("ggspatial")
> if (!require(sp)) install.packages("sp")
> thunderforest = source_from_url_format(
+ url_format = c('http://a.tile.thunderforest.com/landscape/${z}/${x}/${y}.png?apikey=<secret>',
+ 'http://b.tile.thunderforest.com/landscape/${z}/${x}/${y}.png?apikey=<secret>',
+ 'http://c.tile.thunderforest.com/landscape/${z}/${x}/${y}.png?apikey=<secret>'),
+ attribution = "More on Thunderforest at http://www.thunderforest.com/"
+ )
> ggosm(type = thunderforest) +
+ geom_spatial(longlake_waterdf, fill = NA, color = "black")
Converting coordinates to lat/lon (epsg:4326)
Zoom: 15
Fetching 4 missing tiles
|===================================================================================================================================| 100%
...complete!输出地块:

从公共代码中隐藏您的Thunderforest API密钥
另一个相关的问题是如何将API密钥隐藏在公共代码中。推荐的方法是将API键添加到~/.Renviron文件中。
THUNDERFOREST_API_KEY="<insert api key"`我们还通过调用检索环境变量:
Sys.getenv("THUNDERFOREST_API_KEY")现在,我们可以在R程序中调用它,如下所示:
if (!require(ggplot2)) install.packages("ggplot2")
if (!require(rosm)) install.packages("rosm")
if (!require(ggspatial)) install.packages("ggspatial")
if (!require(sp)) install.packages("sp")
thunderforest = source_from_url_format(
url_format = c(paste0('http://a.tile.thunderforest.com/landscape/${z}/${x}/${y}.png?apikey=',Sys.getenv("THUNDERFOREST_API_KEY")),
paste0('http://b.tile.thunderforest.com/landscape/${z}/${x}/${y}.png?apikey=',Sys.getenv("THUNDERFOREST_API_KEY")),
paste0('http://c.tile.thunderforest.com/landscape/${z}/${x}/${y}.png?apikey=',Sys.getenv("THUNDERFOREST_API_KEY"))),
attribution = "More on Thunderforest at http://www.thunderforest.com/"
)
ggosm(type = thunderforest) +
geom_spatial(longlake_waterdf, fill = NA, color = "black")执行代码
使用此示例,共享代码要容易得多。不需要在已发布的代码中包含密钥;-)
> if (!require(ggplot2)) install.packages("ggplot2")
> if (!require(rosm)) install.packages("rosm")
> if (!require(ggspatial)) install.packages("ggspatial")
> if (!require(sp)) install.packages("sp")
> thunderforest = source_from_url_format(
+ url_format = c(paste0('http://a.tile.thunderforest.com/landscape/${z}/${x}/${y}.png?apikey=',Sys.getenv("THUNDERFOREST_API_KEY")),
+ paste0('http://b.tile.thunderforest.com/landscape/${z}/${x}/${y}.png?apikey=',Sys.getenv("THUNDERFOREST_API_KEY")),
+ paste0('http://c.tile.thunderforest.com/landscape/${z}/${x}/${y}.png?apikey=',Sys.getenv("THUNDERFOREST_API_KEY"))),
+ attribution = "More on Thunderforest at http://www.thunderforest.com/"
+ )
> ggosm(type = thunderforest) +
+ geom_spatial(longlake_waterdf, fill = NA, color = "black")
Converting coordinates to lat/lon (epsg:4326)
Zoom: 15输出地块:

发布于 2017-10-21 14:29:11
目前似乎还没有一种方法可以用ggspatial或rosm来实现这一点。所以,我把罗曼并修改了其中一个函数,如果在您的环境中找到它,它将包含一个api键。
短期解
你可以直接用叉子回购。
# Packages
devtools::install_github("jonathande4/rosm")
library(rosm)
library(ggspatial)
# Set environment.
Sys.setenv("THUNDERFOREST_KEY" = "YOUR_API_KEY")
# Plot
ggosm(type = "thunderforestoutdoors") +
geom_spatial(longlake_waterdf, fill = NA, color = "black")输出这张地图,没有水印。

长期解
我想试着为这个改变提交一份拉力请求。如果实现中有任何偏离最初解决方案的更改,我将发布一个更新。
希望这能有所帮助。
https://stackoverflow.com/questions/46793225
复制相似问题