无法通过R-Shiny显示Leaflet HTML(404错误).如何将KML文件与rMaps或Leaflet-Shiny集成? [英] Can't display Leaflet HTML through R-Shiny (404 error). How to integrate KML file with rMaps or leaflet-shiny?

查看:158
本文介绍了无法通过R-Shiny显示Leaflet HTML(404错误).如何将KML文件与rMaps或Leaflet-Shiny集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示包含KML文件的R-Shiny中的交互式地图时遇到一些问题.我安装了 leaflet-plugins ,并且能够创建一个HTML文件,该文件可以在浏览器,但不在Shiny中.尝试此操作的示例是此处-如果您查看源代码,则可以使用该代码.

I'm having some issues displaying an interactive map in R-Shiny that incorporates a KML file. I installed leaflet-plugins and was able to create an HTML file that displays properly by itself in the browser but not within Shiny. This attempt followed an example here - the code is available if you view source.

因为此初始版本不需要更改HTML本身,所以我尝试遵循以下示例此处将原始HTML包含在我的页面中,但是当这些内容以及我尝试将其作为iframe包含在R-Shiny中时(在

Because this initial version does not require the HTML itself to change, I attempted to follow the samples here to include the raw HTML in my page but I receive a 404 error with these as well as when I attempted to include it as an iframe within R-Shiny (following this discussion). I then set up an external facing server for both the KML file and the HTML file and got the same result.

我可以使用 leaflet-shiny 在没有KML文件的情况下使用地图.坦白地说,我不确定如何添加KML文件,并且在文档中看不到这一点.

I was able to get a map working without the KML file with leaflet-shiny but I'm frankly not sure how to add the KML file and don't see this in the documentation.

最后,我尝试了 rMaps ,它确实具有"addKML"方法,但是我不能使它可以处理服务器上文件的各个位置(

Finally, I tried rMaps which does have a "addKML" method, but I can't get it working with various locations of files on my server (

map1 = Leaflet$new()
map1$setView(c(45.5236, -122.675), 13)
map1$tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
map1$addKML('http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml')
map1

它可以在没有$ addKML行的情况下工作.可能值得注意的是,示例1 此处也导致地图空白.

It works without the $addKML line. It might be worth noting that the tilelayer line on example 1 here also resulted in a blank map.

我实际上有一些可能类似的问题,托管派生的标题层仍为

I actually have some possibly similar issues hosting derived title layers that are still unresolved which is one reason I opted for uses KML layers for this demo version of the application, which is why I tagged networking on here as well. I'm using Digital Ocean.

感谢您的任何想法或建议.

Thank you for any thoughts or pointers you may have.

推荐答案

我认为rMaps库中可能存在一个小问题.如果您检查config.yml文件 https://github.com/ramnathv/rCharts/blob/master/inst/libraries/leaflet/config.yml ,您将看到 内容传输网络(cdn)有参考 " http://harrywood.co.uk /maps/examples/leaflet/leaflet-plugins/layer/vector/KML.js ".此KML阅读器是 https://github.com/shramov/leaflet-plugins/blob/master/layer/vector/KML.js .当内容在本地交付时:

I think there may be a small issue in the rMaps library. If you inspect the config.yml file https://github.com/ramnathv/rCharts/blob/master/inst/libraries/leaflet/config.yml you will see that for content delevery network (cdn) there is reference to "http://harrywood.co.uk/maps/examples/leaflet/leaflet-plugins/layer/vector/KML.js". This KML reader is a plugin for leaflet from https://github.com/shramov/leaflet-plugins/blob/master/layer/vector/KML.js. When content is delivered locally:

  css: [external/leaflet.css, external/leaflet-rCharts.css, external/legend.css]
  jshead:
    - external/leaflet.js
    - external/leaflet-providers.js
    - external/Control.FullScreen.js

没有对此JavaScript文件的引用.我们可以解决此问题:

there is no reference to this javascript file. We can fix this:

require(yaml)
leafletLib <- file.path(find.package("rMaps"), "libraries", "leaflet")
rMapsConfig <- yaml.load_file(file.path(leafletLib, "config.yml"))
# add a kml library
kmlLib <- readLines("http://harrywood.co.uk/maps/examples/leaflet/leaflet-plugins/layer/vector/KML.js")
write(kmlLib, file.path(leafletLib, "external", "leaflet-kml.js"))
# add the library to config.yml
rMapsConfig$leaflet$jshead <- union(rMapsConfig$leaflet$jshead , "external/leaflet-kml.js")
write(as.yaml(rMapsConfig), file.path(leafletLib, "config.yml"))

现在config.yml将包含指向KML阅读器的必要链接,并且现在在external/leaflet-kml.js中存储了本地副本.但是,我们的示例仍然无法正常工作,因为我们会得到Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml.,可以通过将资源移至同一域或启用CORS来解决此问题.

Now the config.yml will contain the necessary link to the KML reader and there is a local copy stored now in external/leaflet-kml.js. Our example still wont work however as we will get a Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml. This can be fixed by moving the resource to the same domain or enabling CORS.

我们将需要在本地提供此文件.我们可以将其作为临时措施放置在rMaps包的传单文件夹中.创建地图后,此文件夹将被复制到一个临时文件:

We will need to have this file served locally. We can place it as a temporary measure in the leaflet folder in the rMaps package. When a map is created this folder gets copied to a temporary file:

require(rMaps)
map1 = Leaflet$new()
map1$setView(c(45.5236, -122.675), 13)
map1$tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
map1$addKML('leaflet/placemark.kml')
# temp copy http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml
# to rMaps
sampleKml <- readLines('http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml')
write(sampleKml, file.path(leafletLib, 'placemark.kml'))

# finally try the map
map1

# remove the temp file 
file.remove(file.path(leafletLib, 'placemark.kml'))

更新: rCharts中有一个addAssets方法,可用于添加.js文件.这使我们可以简化事情,并且不需要我们编写js文件的副本或编辑config.yml文件.

UPDATE: There is an addAssets method in rCharts which allows you to add .js files. This allows us to simpilfy things and doesnt require us to write a copy of the js file nor edit the config.yml file.

require(rMaps)
map1 = Leaflet$new()
map1$setView(c(45.5236, -122.675), 13)
map1$tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
map1$addAssets(css = NULL, jshead = 'http://harrywood.co.uk/maps/examples/leaflet/leaflet-plugins/layer/vector/KML.js')
map1$addKML('leaflet/placemark.kml')
leafletLib <- file.path(find.package("rMaps"), "libraries", "leaflet")
sampleKml <- readLines('http://kml-samples.googlecode.com/svn/trunk/kml/Placemark/placemark.kml')
write(sampleKml, file.path(leafletLib, 'placemark.kml'))
# finally try the map
map1

# remove the temp file 
file.remove(file.path(leafletLib, 'placemark.kml'))

这篇关于无法通过R-Shiny显示Leaflet HTML(404错误).如何将KML文件与rMaps或Leaflet-Shiny集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆