简体   繁体   English

使用leafletR包将2个GeoJSON文件加载到R中的传单网络地图时出现问题

[英]Problems loading 2 GeoJSON files into a leaflet web map in R using the leafletR package

I'm using the leafletR package ( http://cran.r-project.org/web/packages/leafletR/index.html ) to make a leaflet webmap applet, but having trouble loading 2 sets of features onto the same map. 我正在使用leafletR包( http://cran.r-project.org/web/packages/leafletR/index.html )制作一个传单Web地图小程序,但是在将2组要素加载到同一地图上时遇到了麻烦。

As I understand it, the leaflet() function will only accept GeoJSON files of one geometry type. 据我了解,leaflet()函数将仅接受一种几何类型的GeoJSON文件。

I therefore have 2 separate GeoJSON files, one with MultiPolygons and another with Points. 因此,我有2个单独的GeoJSON文件,一个包含MultiPolygons,另一个包含Point。

I'm able to get the MultiPolygons to render as a choropleth with this code: 我可以使用以下代码将MultiPolygons渲染为choropleth:

#Load LeafletR
require(leafletR)

#Create quantiles
cuts <- round(quantile(UKpostcode_areas$data, probs = seq(0, 1, 0.20), na.rm = FALSE), 0)
cuts[1] <- 0 #We don't want any negative values, so let's make the first cut zero

#Fields to include in the popup
popup.1 <- c("name", "data")

#Graduated style based on an attribute
sty.1 <- styleGrad(prop = "data", breaks=cuts, right=FALSE, style.par="col", style.val=rev(heat.colors(6)), leg="Data", lwd=1)

#Create the map and load into browser
map <- leaflet(data = "map/UKpostcode_areas.geojson", dest = "map", style = sty.1, title = "UKpostcode_areas_choropleth", base.map= "osm", incl.data=TRUE,  popup = popup.1)

I'm also able to get the Points to render: 我还可以获取要渲染的点:

#Create new style and popup details for the 2nd layer
sty.2 <- styleSingle(col = "white", fill = "#2b83ba", fill.alpha = 1, rad = 3)
popup.2 <- c("name", "trust")

#Let's take a look at the map of hospitals
map2 <- leaflet(data="map/hospitals.geojson", dest = "map", style = sty.2, popup = popup.2, title = "hospitals", base.map = "osm", incl.data=TRUE, controls = "all")
browseURL(map2)

However, when I try to render both on the same Leaflet map, it just gives me a blank screen: 但是,当我尝试在同一张Leaflet地图上同时渲染它们时,它只是给了我一个空白屏幕:

#Now we can combine the 2 into 1 map, this is problematic, can't get it to work!
map3 <- leaflet(data = list("map/UKpostcode_areas.geojson", "map/hospitals.geojson"), style = list(sty.1, sty.2), dest = "map", title = "index", base.map= "osm", incl.data=TRUE, controls = "all")
browseURL(map)

I suspect there's something wrong with the last couple of lines of code. 我怀疑最后两行代码有问题。 But I can't figure out what. 但是我不知道是什么。

I believe LeafletR can only handle multiple layers when there is a single style for each layer. 我相信LeafletR仅在每层只有一个样式时才能处理多层。

Example from the leaflet() documentation page : leaflet() 文档页面中的示例:

# more than one data set
park <- toGeoJSON(data=system.file(package="leafletR", "files", 
  "park_sk.zip"), dest=tempdir())
peak <- toGeoJSON(system.file(package="leafletR", "files", "peak_sk.kml"), 
  dest=tempdir()) # httr package required
sty.1 <- styleSingle(col="green", fill="green")
sty.2 <- styleSingle(col="brown", fill="brown", rad=3)
map <- leaflet(data=list(park, peak), dest=tempdir(), 
  style=list(sty.1, sty.2))
browseURL(map)

This works as expected, but if you change sty.2 to a graduated style, eg: 这可以按预期工作,但是如果您将sty.2更改为渐变样式,例如:

sty.2 <- styleGrad(prop="Name", col="brown", fill="brown", style.par="rad",
     style.val=c(1,10), breaks=breaks, right=TRUE, out=1)

it gives the same blank screen you described (though it works fine if peak is the only layer). 它提供了与您描述的相同的黑屏(尽管如果peak是唯一的层,则可以正常工作)。 I'm not sure how crucial it is that your map has a graduated style, but if you use styleSingle() , both layers should appear. 我不确定地图的渐变样式有多重要,但是如果使用styleSingle() ,则两个图层都应该出现。

What i'm thinking (guessing actually, i'm by no means familiar with R) is that combining those two GeoJSON files won't work if they're actually FeatureCollections. 我在想什么(猜测,实际上我一点都不熟悉R)是,如果这两个GeoJSON文件实际上是FeatureCollections,那么将它们结合起来是行不通的。 A FeatureCollection would look something like this: FeatureCollection看起来像这样:

{
    "type": "FeatureCollection",
    "features": [{
        // Feature
    }, {
        // Another feature
    }]
}

Now if you combine two of those you'll end up with something like this: 现在,如果将其中两个结合在一起,最终将得到如下结果:

[{
    "type": "FeatureCollection",
    "features": [{
        // Feature
    }, {
        // Another feature
    }]
}, {
    "type": "FeatureCollection",
    "features": [{
        // Feature
    }, {
        // Another feature
    }]
}]

Leaflet's L.GeoJSON layer won't take that. Leaflet的L.GeoJSON层不会接受这一点。 What you want to do is merge the embedded feature arrays, and embed that into a new FeatureCollection object or just merge the two feature arrays into one new array and use that. 您要做的是合并嵌入的要素数组,然后将其嵌入到新的FeatureCollection对象中,或者仅将两个要素数组合并为一个新数组并使用它。 L.GeoJSON can use a FeatureCollection or an array of features. L.GeoJSON可以使用FeatureCollection或一系列功能。 But it won't take an array of FeatureCollections and thats what i think you are doing now. 但这并不需要一系列FeatureCollections,这就是我认为您现在正在做的事情。

In JS i would do something like this: 在JS中,我会执行以下操作:

var collectionA = { //FeatureCollection };
var collectionB = { //FeatureCollection };

var features = collectionA.features.concat(collectionB.features);

var collection = {
    "type": "FeatureCollection",
    "features": features
}

// Now you can use: 
new L.GeoJSON(features);
// Or you can use:
new L.GeoJSON(collection);

Another way is to use the addData method of L.GeoJSON but i wouldn't know if that's available to you when using LeafletR, but in JS we can do: 另一种方法是使用addData的方法L.GeoJSON但我不知道这是否使用LeafletR时提供给您,但在JS,我们可以这样做:

var collectionA = { //FeatureCollection };
var collectionB = { //FeatureCollection };

var layer = new L.GeoJSON();

layer.addData(collectionA);
layer.addData(collectionB);

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM