在 'drawend' 上替换绘制的几何图形

Replacing the drawn geometry on 'drawend'

我想做的是在绘制要素后替换或更改要素的几何形状。例如:我画了一条线,画完后我用 Turf.js 修改几何图形,在绘制的线周围创建一个缓冲区并显示缓冲线(多边形)而不是线。

var draw = new ol.interaction.Draw({
    source: vectorSource,
    type: 'LineString'
});

draw.on('drawend', function(e) {

    //Sending the LineString to a Turf function with a buffer value and getting a Polygon in return
    var bfc = bufferedFeatureCollection(100, e.feature);

    //Replacing the LineString geometry with a Polygon geometry
    e.feature.setGeometry(bfc[0].getGeometry());

    //Removes and terminates the draw action.... 
    map.removeInteraction(this);
});

现在从 console.log() 开始,我可以看到 feature.geometry 已从 ol.geom.linestring 更改为 ol.geom.polygon。但是在地图上我仍然看到一条线正在显示。

我做错了什么?

将功能添加到 ol.source.Vector 后进行这些修改,因此:

vectorSource.on('addfeature', function(evt){
    //Sending the LineString to a Turf function with a buffer value and getting a Polygon in return
    var bfc = bufferedFeatureCollection(100, evt.feature);

    //Replacing the LineString geometry with a Polygon geometry
    evt.feature.setGeometry(bfc[0].getGeometry());

    //Removes and terminates the draw action.... 
    map.removeInteraction(draw);
});

我犯的错误是缓冲区值太低而不能被视为缓冲区因此我只看到了一条线(即使它是多边形)我不得不将缓冲区值从 100 to 10000000 ...

因此在draw.on('drawend')vectorSource.on('addfeature')中都可以替换已绘制的几何图形。

编辑:

为了回答的完整性。我必须将缓冲区从 100 to 10000000 更改的原因是我忘记将几何图形转换为 EPSG:4326,然后再将其传递给 Turf 函数。 Turf 仅适用于 EPSG:4326.

var draw = new ol.interaction.Draw({
    source: vectorSource,
    type: 'LineString'
});

draw.on('drawend', function(e) {

    e.feature.getGeometry().transform('EPSG:3857', 'EPSG:4326');

    var bfc = bufferedFeatureCollection(radius, e.feature);
    bfc[0].getGeometry().transform('EPSG:4326', 'EPSG:3857');

    e.feature.setGeometry(bfc[0].getGeometry());

    //Removes and terminates the draw action....
    map.removeInteraction(this); 
});