簡體   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