R:使用addResourcePath渲染本地传单图块 [英] R: Using addResourcePath for rendering local leaflet tiles
问题描述
我想为传单添加本地图块,以使其在闪亮的应用程序中脱机. 尽管在SO上有解决方案,例如这里,最后仍然是没有瓷砖的灰色地图.看到一些可重现的示例确实对我有帮助. 谢谢.
I would like to add local tiles for leaflet to render them offline in a shiny application. Although there are solutions to this on SO for example here and here , I am still ending up with grey map with no tiles. It would really help me to see some reproducible example. Thanks.
我的示例代码:
library(shiny)
library(dplyr)
library(RgoogleMaps)
#downloads tiles for a given regions, saves it to C:/Users/.../mapTiles/OSM
for (zoom in 0:16)
GetMapTiles(center = c(lat = 52.431635, lon = 13.194773),
zoom = zoom,
nTiles = round(c(20,20)/(17-zoom)))
#shiny ui
ui = fluidPage(leafletOutput("map"))
#create basic map, load tiles from directory and set view to centre of downloaded tiles
server = function(input, output, server){
addResourcePath(prefix = "OSM", "C:/Users/.../mapTiles")
output$map = renderLeaflet({
leaflet() %>%
addTiles( urlTemplate = "/OSM/{z}_{x}_{y}.png") %>%
setView(52.431635, 13.194773 , zoom = 10) %>% #set to the location with tiles
addMarkers(52.431635, 13.194773 )
}
)
}
shinyApp(ui, server)
推荐答案
在我的情况下,我通过gdal2tiles
创建了自己的图块,该图将获取您的数据并自动创建{z}/{x}/{y} .png文件夹结构.请参阅此链接,以获得一个不错的教程以及我对文件结构的理解;
In my case, I create my own tiles via gdal2tiles
, which takes your data and automatically creates a {z}/{x}/{y}.png folder structure. Please see this link for a nice tutorial and what i mean about the file structure;
+---14
| +---8185
| +---5460.png
| +---5461.png
| +---etc.png
| \---8186
# I use the following server (see how my addTiles has a folder structure)
server <- function(input, output,session) {
addResourcePath("mytiles", "C:/.../tiles")
output$tilemap <- renderLeaflet({
leaflet() %>%
setView(lng = -4.4, lat = 52, zoom = 12) %>%
addTiles(urlTemplate = "mytiles/{z}/{x}/{y}.png")
})
}
现在,当您将图块从Google Maps下载到硬盘驱动器时,您会需要一种略有不同的方法,因为文件是以{z}_{x}_{y}.png
格式下载的,而不是像gdal create那样生成的文件结构;
Now, as you are downloading tiles from Google Maps to your hard drive, you'll want a slightly different approach as the files are downloaded in a {z}_{x}_{y}.png
format, and not produced into a file structure like gdal creates;
+ --- 11_1098_671.png 等
+---11_1098_671.png etc.
,因此您需要使用下划线(例如Google文件名)来调整addTiles
代码以反映这一点;
so you need to adjust your addTiles
code to reflect this, using underscores, like the Google filenames;
server <- function(input, output,session) {
addResourcePath("mytiles", "C:/.../OSM")
output$tilemap <- renderLeaflet({
leaflet() %>%
setView(lng = 13.194773, lat = 52.431635, zoom = 11) %>%
addTiles(urlTemplate = "mytiles/{z}_{x}_{y}.png")
})
}
最后,我的setView参数与您的顺序不同,但是我不确定这是否有所不同.
Lastly, my setView arguments are in a different order to yours but i'm not sure whether that makes a difference or not.
这篇关于R:使用addResourcePath渲染本地传单图块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!