我正在使用Openlayers3,并且想要添加一个层,其中TurfJS函数"merge“的答案应该是源代码。在OpenLayers 3中直接添加一个GeoJSON-Layer是没有问题的,而且运行良好。但是当我将GeoJSON-File加载到变量中并使用turf.merge(源)时,就不能再将其添加到层中。我已经尝试在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-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);也许在使用turfs merge时,GeoJSON-Fomat有什么问题?我为每一个帮助感到高兴!
发布于 2015-10-19 00:48:02
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);
});https://stackoverflow.com/questions/33186368
复制相似问题