【问题标题】:Problems loading 2 GeoJSON files into a leaflet web map in R using the leafletR package使用 LeafletR 包将 2 个 GeoJSON 文件加载到 R 中的传单 web 地图中的问题
【发布时间】:2015-04-23 08:11:45
【问题描述】:

我正在使用 LeafletR 包 (http://cran.r-project.org/web/packages/leafletR/index.html) 来制作一个传单网络地图小程序,但在将 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)

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

【问题讨论】:

    标签: r leaflet


    【解决方案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(),则两个图层都应该出现。

    【讨论】:

      【解决方案2】:

      我在想什么(实际上是在猜测,我对 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);
      

      另一种方法是使用 L.GeoJSONaddData 方法,但我不知道在使用 LeafletR 时是否可以使用,但在 JS 中我们可以这样做:

      var collectionA = { //FeatureCollection };
      var collectionB = { //FeatureCollection };
      
      var layer = new L.GeoJSON();
      
      layer.addData(collectionA);
      layer.addData(collectionB);
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多