繁体   English   中英

将 OpenLayers 多边形转换为具有不同投影的 GeoJSON

[英]Converting OpenLayers Polygon to GeoJSON with different projection

类似于这个问题中发现的问题

如何将 OpenLayers 多边形坐标转换为经纬度?

我已经设置了一个基本地图来捕获用户定义的多边形并将其转换为 GeoJSON,而我可以使用其原生投影 (ESPG:3857) 来做到这一点我想获取捕获的 GeoJSON 并将其转换为 EPSG:4326 - 然后我会保存它。 使用上述方法在 drawend 事件上捕获特征并执行变换会删除多边形,因为新坐标不再显示在地图的投影中。 我无法弄清楚如何在不删除现有多边形的情况下以我需要的格式保存 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);

Openlayers 6 ,无需循环功能

var format = new ol.format["GeoJSON"](); 
var geoJsonStr = format.writeFeatures(vectorSource.getFeatures(), { dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857'});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM