【问题标题】:Adding GeoJSON Layer in OpenLayers 3 with TurfJS "merge"-function使用 TurfJS“合并”功能在 OpenLayers 3 中添加 GeoJSON 层
【发布时间】:2015-10-17 11:59:43
【问题描述】:

我正在使用 Openlayers 3 并想添加一个层,其中 TurfJS 函数“合并”的答案应该是源。 直接在 OpenLayers 3 中添加 GeoJSON-Layer 没有问题并且工作正常。 但是当我在变量中加载 GeoJSON 文件并使用 turf.merge(source) 时,不能再将其添加到图层中。 我已经尝试将 turf.merge 的答案转换为 FeatureCollection 并将其添加为图层源,但这也不起作用

//define source (contains Polygons)
        var source = new ol.source.Vector({
      url: 'geb03_f_source.geojson',
      format: new ol.format.GeoJSON({
          })
    });

//Merge source
var merged = turf.merge(source);

//define layer
var vectorLayer = new ol.layer.Vector({
  source: merged,
  projection: 'EPSG:3857'
});

//add layer to map
map.addLayer(vectorLayer);

我看到的问题是,在加载页面时,GeoJSON-File 没有加载,尽管它应该加载。

但只是加载和显示文件有效:

var source = new ol.source.Vector({
  url: 'geb03_f_source.geojson',
  format: new ol.format.GeoJSON({
      })
});

var vectorLayer = new ol.layer.Vector({
  source: source,
  projection: 'EPSG:3857'
});

map.addLayer(vectorLayer);

使用草皮合并时,GeoJSON-Fomat 可能有问题? 我很高兴得到每一次帮助!

【问题讨论】:

  • 控制台显示“TypeError: t.features is undefined”。加载的 GeoJSON-Feature 的格式似乎不对...
  • 别忘了提供反馈。你得到了答案。

标签: json openlayers-3 geojson turfjs


【解决方案1】:

Turf JS 使用 GeoJSON 作为输入和输出,您提供一个 OpenLayers 矢量源对象作为输入。所以你需要改变它。

一种选择是使用ol.source.Vectorloader 选项而不是urlformat,在将GeoJSON 多边形添加到源之前直接合并它。

另一种选择是将 ol 源重新转换为 GeoJSON,例如:

var geoJSONFormat = new ol.format.GeoJSON({});

var source = new ol.source.Vector({
  url: 'geb03_f_source.geojson',
  format: geoJSONFormat
});

var mergedSource = new ol.source.Vector({});

source.on('change', function(){
  var sourceJSON = geoJSONFormat.writeFeaturesObject(source.getFeatures());
  var merged = turf.merge(sourceJSON);
  var mergedOLFeature = geoJSONFormat.readFeature(merged);
  mergedSource.clear();
  mergedSource.addFeature(mergedOLFeature);
});

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多