使用TurpJS在OpenLayers 3中添加GeoJSON层;“合并”-功能

使用TurpJS在OpenLayers 3中添加GeoJSON层;“合并”-功能,json,openlayers-3,geojson,turfjs,Json,Openlayers 3,Geojson,Turfjs,我正在使用Openlayers 3,希望添加一个层,其中TurpJS函数“merge”的答案应该是源代码。 直接在OpenLayers 3中添加GeoJSON层并没有问题,效果很好。 但是当我在一个变量中加载GeoJSON文件并使用turp.merge(source)时,就不能再在层中添加它了。 我已经尝试在FeatureCollection中转换turf.merge的答案,并将其添加为图层源,但这也不起作用 //define source (contains Polygons)

我正在使用Openlayers 3,希望添加一个层,其中TurpJS函数“merge”的答案应该是源代码。 直接在OpenLayers 3中添加GeoJSON层并没有问题,效果很好。 但是当我在一个变量中加载GeoJSON文件并使用turp.merge(source)时,就不能再在层中添加它了。 我已经尝试在FeatureCollection中转换turf.merge的答案,并将其添加为图层源,但这也不起作用

//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文件没有加载,尽管它应该加载

但只需加载和显示文件即可:

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可能有问题?
我为每一个帮助感到高兴

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

一个选项是使用
ol.source.Vector
loader
选项,而不是
url
format
,直接合并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);
});

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

一个选项是使用
ol.source.Vector
loader
选项,而不是
url
format
,直接合并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);
});

控制台显示“TypeError:t.features未定义”。加载的GeoJSON特性的格式似乎不对……别忘了给出反馈。您得到了答案。控制台显示“TypeError:t.features未定义”。加载的GeoJSON特性的格式似乎不对……别忘了给出反馈。你得到你的答案了。