如下面发布的代码所示,我使用
turf.js
在 modifyend
事件的 Modify
的侦听器回调中缓冲几何图形。
我尝试通过以下方式在地图上设置缓冲功能asOLFeature
:this.#vectorSource.addFeature(asOLFeature)
为了确保缓冲的几何图形放置在地图上,我将
asOLFeature.getGeometry()
转换为 geojson 格式,以便我可以将其可视化。
我用来将 OL Geometry
转换为 GeoJSON
的代码如下:
var writer = new GeoJSON();
var geoJsonStr = writer.writeGeometry(asOLFeature.getGeometry());
数字化几何体如下图 1 所示,缓冲几何体如下图 2 所示。
显然,当使用
turf
缓冲图像 1 中的数字化几何图形时,如图像 2 所示,它以某种方式转换为两个几何图形并移位。
我相信关于坐标变换和投影问题,但我不知道如何解决它。
代码
this.#modifyEvtNullifierFeature = evt.feature;
const modifyEvtNullifierAsGeom = this.#modifyEvtNullifierFeature.getGeometry();
const geojsonFormat = new GeoJSON();
const geosonGeometry = geojsonFormat.writeGeometryObject(modifyEvtNullifierAsGeom, {
featureProjection: 'EPSG:3857',
dataProjection:'EPSG:4326'
});
let geomInMercator = turf.toMercator(geosonGeometry);
const buffered = turf.buffer(geomInMercator, 60, {units: 'kilometers'});
let bufferedInWgs84 = turf.toWgs84(buffered);
const asOLFeature = geojsonFormat.readFeature(bufferedInWgs84, {
dataProjection:'EPSG:4326',
featureProjection: 'EPSG:3857'
});
this.#modifyEvtNullifierFeature.set(ModifyEvtNullifierFeaturesConstants.CONST_IS_MODIFY_EVT_NULLIFIER_FEATURE.description, true);
this.#vectorSource.addFeature(asOLFeature);
turf.js 需要 WGS84 坐标。如果您使用 Openlayers 在投影之间进行转换,则无需使用 toMercator/toWgs84
来反转它const geosonGeometry = geojsonFormat.writeGeometryObject(modifyEvtNullifierAsGeom, {
featureProjection: 'EPSG:3857',
dataProjection:'EPSG:4326'
});
const buffered = turf.buffer(geosonGeometry, 60, {units: 'kilometers'});
const asOLFeature = geojsonFormat.readFeature(buffered, {
dataProjection:'EPSG:4326',
featureProjection: 'EPSG:3857'
});