我使用leafletR包(http://cran.r-project.org/web/packages/leafletR/index.html)制作传单webmap applet,但无法将2组功能加载到同一个地图上。
据我所知,leaflet()函数只接受一种几何类型的GeoJSON文件。
因此,我有2个单独的GeoJSON文件,一个使用MultiPolygons,另一个使用Points。
我可以使用此代码将MultiPolygons渲染为等值线:
#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)
我怀疑最后几行代码存在问题。但我无法弄清楚是什么。
答案 0 :(得分:1)
我相信LeafletR只能在每个图层有一个样式时处理多个图层。
来自leaflet()
documentation page:
# 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()
,则应显示两个图层。
答案 1 :(得分:0)
我在想(实际上猜测,我并不熟悉R),如果他们实际上是FeatureCollections,那么组合这两个GeoJSON文件将无法工作。 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
的{{1}}方法,但我不知道在使用LeafletR时您是否可以使用,但在JS中我们可以这样做:< / p>
L.GeoJSON
希望有所帮助。