解决方案:
Chuckle - 有一种方法可以在不 修改库的情况下使用 rosm 做到这一点。
- 使用该
rosm:: source_from_url_format()
功能。这就是它的设计目的,它记录在 rosm 包中。[诚然有些简短]
注意 - 为了完整起见,我在单独的后续项目符号下介绍了在使用共享代码时如何隐藏您的公共 API 密钥。
我希望这会有所帮助,它至少应该让那些想要在不需要修改库的情况下执行您的建议的人更容易。
保重T。
示例用法:source_from_url_format()
注意:替换<insert key>
为您在 Thunderforest 网站上的私钥。
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
输出图: