繁体   English   中英

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

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

我正在使用leafletR包( http://cran.r-project.org/web/packages/leafletR/index.html )制作一个传单Web地图小程序,但是在将2组要素加载到同一地图上时遇到了麻烦。

据我了解,leaflet()函数将仅接受一种几何类型的GeoJSON文件。

因此,我有2个单独的GeoJSON文件,一个包含MultiPolygons,另一个包含Point。

我可以使用以下代码将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)

我还可以获取要渲染的点:

#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)

但是,当我尝试在同一张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)

我怀疑最后两行代码有问题。 但是我不知道是什么。

我相信LeafletR仅在每层只有一个样式时才能处理多层。

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)

这可以按预期工作,但是如果您将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)

它提供了与您描述的相同的黑屏(尽管如果peak是唯一的层,则可以正常工作)。 我不确定地图的渐变样式有多重要,但是如果使用styleSingle() ,则两个图层都应该出现。

我在想什么(猜测,实际上我一点都不熟悉R)是,如果这两个GeoJSON文件实际上是FeatureCollections,那么将它们结合起来是行不通的。 FeatureCollection看起来像这样:

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

现在,如果将其中两个结合在一起,最终将得到如下结果:

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

Leaflet的L.GeoJSON层不会接受这一点。 您要做的是合并嵌入的要素数组,然后将其嵌入到新的FeatureCollection对象中,或者仅将两个要素数组合并为一个新数组并使用它。 L.GeoJSON可以使用FeatureCollection或一系列功能。 但这并不需要一系列FeatureCollections,这就是我认为您现在正在做的事情。

在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);

另一种方法是使用addData的方法L.GeoJSON但我不知道这是否使用LeafletR时提供给您,但在JS,我们可以这样做:

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

var layer = new L.GeoJSON();

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

希望能有所帮助。

暂无
暂无

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

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