gpt4 book ai didi

javascript - 使用不同的投影将 OpenLayers 多边形转换为 GeoJSON

转载 作者:行者123 更新时间:2023-12-03 00:20:58 27 4
gpt4 key购买 nike

与本问题中发现的问题类似

How to convert OpenLayers polygon coordinates into latitude and longitude?

我已经设置了一个基本 map 来捕获用户定义的多边形并将其转换为 GeoJSON,同时我可以使用其 native 投影 (ESPG:3857) 来执行此操作,我想获取捕获的 GeoJSON 并将其转换为EPSG:4326 - 然后我会将其保存起来。然而,使用上述捕获绘制事件上的特征并执行变换的方法会删除多边形,因为新坐标不再在 map 的投影中表示。我无法弄清楚如何在不删除现有多边形的情况下以我需要的格式保存 GeoJSON

我尝试通过在多边形的矢量源上使用 getFeatures 来执行此操作,然后执行从我正在使用的投影到我想要的投影的转换,但这仍然返回相同的坐标,我也有(就像链接的文章)尝试使用 writeFeatureObject 但它仍然保存不正确

https://jsfiddle.net/20gxo3nt/


dragBox.on('drawend', function(evt){

/* geom = evt.feature.getGeometry().transform('EPSG:3857', 'EPSG:4326');
console.log(geom.getCoordinates()); */

});

$( "#save" ).click(function() {
var geom=vectorSource.getFeatures();
console.log(geom);
var writer=new ol.format.GeoJSON();
var geoJsonStr = writer.writeFeatures(geom);
console.log (geom.proj.transform('EPSG:3857', 'EPSG:4326'));
/* console.log(geoJsonStr) */
});

取消注释drawend事件上的代码将正确地console.log坐标,并演示多边形消失

最佳答案

获取新的 geojson

  var geom = [];
vectorSource.forEachFeature( function(feature) { geom.push(new ol.Feature(feature.getGeometry().clone().transform('EPSG:3857', 'EPSG:4326'))); } );
var writer = new ol.format.GeoJSON();
var geoJsonStr = writer.writeFeatures(geom);
console.log(geoJsonStr);

关于javascript - 使用不同的投影将 OpenLayers 多边形转换为 GeoJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54313855/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com