首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用TurfJS "merge"-function在OpenLayers 3中添加GeoJSON层

使用TurfJS "merge"-function在OpenLayers 3中添加GeoJSON层
EN

Stack Overflow用户
提问于 2015-10-17 19:59:44
回答 1查看 1.3K关注 0票数 1

我正在使用Openlayers3,并且想要添加一个层,其中TurfJS函数"merge“的答案应该是源代码。在OpenLayers 3中直接添加一个GeoJSON-Layer是没有问题的,而且运行良好。但是当我将GeoJSON-File加载到变量中并使用turf.merge(源)时,就不能再将其添加到层中。我已经尝试在FeatureCollection中转换turf.merge的答案,并将其添加为图层源,但同样不起作用

代码语言:javascript
复制
//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没有加载,尽管它应该加载。

但是只需要加载和显示文件就可以了:

代码语言:javascript
复制
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);

也许在使用turfs merge时,GeoJSON-Fomat有什么问题?我为每一个帮助感到高兴!

EN

回答 1

Stack Overflow用户

发布于 2015-10-19 00:48:02

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

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

另一种选择是将ol源代码重新转换为GeoJSON,如下所示:

代码语言:javascript
复制
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);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33186368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档