解决方案:
咯咯笑-有一种方法可以做到这一点,用rosm做到这一点
没有
修改库
.
-
使用
rosm:: source_from_url_format()
作用这就是它的设计目的,它记录在rosm包中。[无可否认,有点简短]
注意-为了完整性,我介绍了在使用单独的后续项目符号下的共享代码时如何隐藏公共API密钥。
我希望这会有所帮助,它至少会让那些想做你提议的事情而不需要修改库的人变得更容易。
当心
T
用法示例:source\u from\u url\u 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密钥
~/.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
输出绘图: