如何在Leaflet for R中渲染使用gdal2tiles创建的自定义地图图块? [英] How to render custom map tiles created with gdal2tiles in Leaflet for R?
问题描述
我正在使用 ESA的地面覆盖栅格图层,最终希望在"Leaflet Shiny"应用程序中显示全球范围内的数据.渲染如此大的文件是不可能的,因此我决定创建地图图块以显示数据.
I'm working with the ESA's landcover raster layer and ultimately want to display that data for the globe in a Leaflet Shiny app. Rendering such a massive file is impossible, so I've decided to create map tiles to display the data.
创建图块很简单-我在QGIS中使用了gdal2tiles
工具.快速浏览一下输出,该输出位于计算机的本地目录中:
Creating the tiles was simple--I used the gdal2tiles
tool in QGIS. Here's a quick look of the output, which is in a local directory on my computer:
当我单击leaflet.html file
时,将在浏览器中渲染图块,如下所示:
When I click the leaflet.html file
, the tiles are rendered in my browser, like so:
显然,这些瓷砖处于工作状态. 问题是我不知道如何在Leaflet for R中渲染这些图块.我尝试遵循此StackOverflow问题中探索了答案,但所有答案似乎已经过时了几年.
Obviously the tiles are in working order. The problem is that I don't know how to render these tiles in Leaflet for R. I tried following this tutorial, but nothing is rendered when I altered my code to fit the example. I also explored answers from this StackOverflow question, but all of the answers seem several years out of date.
这是我用来尝试以任何方式渲染图块的R代码:
Here's the R code that I'm using to try to get the tiles to render in any way:
library(leaflet)
leaflet() %>%
setView(0, 0, zoom = 1) %>%
addTiles(urlTemplate = "http://my-username.github.io/tiles/{z}/{x}/{y}.png",
options = tileOptions(minZoom = 1, maxZoom = 2, tms = TRUE)) %>%
addCircles(lat = 0, lng = 0, radius = 100) #just to see if anything is rendering
此代码呈现了我绘制的圆,但没有其他内容.
This code renders the circle I've drawn, but nothing else.
是否可以直接从本地计算机渲染这些图块?如果没有,如何托管这些图块,以便可以在Leaflet for R中渲染它们?看来这应该很简单,但我无法弄清楚!
Is there a way to render these tiles directly from my local machine? If not, how do I host these tiles so that they can be rendered in Leaflet for R? It seems like this should be pretty straightforward, but I can't figure it out!
推荐答案
弄清楚了.您必须在Shiny目录中使用"www"文件夹.因此,在这个问题中,我只有文件夹"Tiles"以及其中列出的所有平铺文件夹(0-7).而是将Tiles文件夹移动到www目录中(在我的示例中,它们将进一步移动到名为"map"的文件夹中).
Figured it out. You have to use a "www" folder inside your Shiny directory. So in the question, I just had the folder "Tiles" and all of the tiled folders listed inside of it (0 - 7). Instead, move the Tiles folder inside a www directory (in my example, they are further moved into a folder called "map").
因此,代替上面的结构Tiles > x
,它必须是www > map > Tiles > x
So instead of the above structure Tiles > x
, it needs to be www > map > Tiles > x
leaflet() %>%
addTiles(urlTemplate = "map/Tiles/{z}/{x}/{y}.png",
option = tileOptions(tms = T, minZoom = 5, maxZoom = 9))
这篇关于如何在Leaflet for R中渲染使用gdal2tiles创建的自定义地图图块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!