首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Thunderforest API键传递给ggspatial包以创建地图

如何将Thunderforest API键传递给ggspatial包以创建地图
EN

Stack Overflow用户
提问于 2017-10-17 14:58:35
回答 2查看 1.3K关注 0票数 2

我正在使用rosmggspatial R包创建地图。ggosm函数易于用于基于所提供的空间对象提取基本地图层。下面是一个例子。

代码语言:javascript
复制
library(ggplot2)
library(sp)
library(rosm)
library(ggspatial)

ggosm() + 
  geom_spatial(longlake_waterdf, fill = NA, color = "black")

这个很好用。我可以将基本映射层更改为其他类型(可用类型请参见osm.types() )。下面是一个使用cartolight作为基本映射层的示例。

代码语言:javascript
复制
ggosm(type = "cartolight") + 
  geom_spatial(longlake_waterdf, fill = NA, color = "black")

这也很管用。现在我的问题是如何传递Thunderforest API密钥?如果我将该类型用作thunderforestoutdoors,则得到以下输出。

代码语言:javascript
复制
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>。我需要知道zxy (缩放级别和平铺号)来指定瓷砖。这是不可能的,因为我有很多空间对象要绘制,我需要ggosm来确定正确的缩放水平和瓷砖为我。如果有人能对此有所了解,那就太好了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-24 04:26:45

解决方案:

笑--有一种方法可以用rosm 实现这一点,而不需要修改库。

  • 使用rosm:: source_from_url_format()函数。这就是它设计的目的,它被记录在rosm包中。不可否认,略显短暂

注意:为了完整性,我将介绍如何在使用共享代码时隐藏公共API密钥。

我希望这能有所帮助,至少应该让那些不需要修改库就想做你提议的事情的人更容易。

小心T。

示例用法: source_from_url_format()

备注:将<insert key>替换为来自雷霆森林网站的私钥。

代码语言:javascript
复制
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")

示例运行时输出:

代码语言:javascript
复制
> 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文件中。

代码语言:javascript
复制
THUNDERFOREST_API_KEY="<insert api key"`

我们还通过调用检索环境变量:

代码语言:javascript
复制
Sys.getenv("THUNDERFOREST_API_KEY")

现在,我们可以在R程序中调用它,如下所示:

代码语言:javascript
复制
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")

执行代码

使用此示例,共享代码要容易得多。不需要在已发布的代码中包含密钥;-)

代码语言:javascript
复制
> 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

输出地块:

票数 2
EN

Stack Overflow用户

发布于 2017-10-21 14:29:11

目前似乎还没有一种方法可以用ggspatial或rosm来实现这一点。所以,我把罗曼并修改了其中一个函数,如果在您的环境中找到它,它将包含一个api键。

短期解

你可以直接用叉子回购。

代码语言:javascript
复制
# 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")

输出这张地图,没有水印。

长期解

我想试着为这个改变提交一份拉力请求。如果实现中有任何偏离最初解决方案的更改,我将发布一个更新。

希望这能有所帮助。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46793225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档