Openlayers 3 如何将在openlayers3地图(投影EPSG:3857)上绘制的要素覆盖导出到投影到EPSG:4326的KML文件
我正在连接到openlayers3地图(投影EPSG:3857)的featureOverlay上绘制多边形,如下所示:Openlayers 3 如何将在openlayers3地图(投影EPSG:3857)上绘制的要素覆盖导出到投影到EPSG:4326的KML文件,openlayers-3,Openlayers 3,我正在连接到openlayers3地图(投影EPSG:3857)的featureOverlay上绘制多边形,如下所示: // create openlayers3 map var map = new ol.Map({ layers: [ new ol.layer.Tile({ source: new ol.source.MapQuest({layer: 'sat'}) }) ], target: 'map',
// create openlayers3 map
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'})
})
],
target: 'map',
view: new ol.View({
center: [-11000000, 4600000],
zoom: 4
})
});
// create the featureOverlay to receive polygons
var featureOverlay = new ol.FeatureOverlay();
featureOverlay.setMap(map);
// create the tool to draw polygons
draw = new ol.interaction.Draw({
features: featureOverlay.getFeatures(),
type: 'Polygon'
});
map.addInteraction(draw);
在这里之前,一切都很好!这个想法来自政府。下一步是使用投影EPSG:4326将地图上绘制的要素导出到KML文件。我已尝试通过以下代码完成此任务:
$('#download-button').click(function() {
// get the features drawn on the map
var features = featureOverlay.getFeatures().getArray();
// create an object to write features on a output KML file
var format = new ol.format.KML();
// write features to KML format using projection EPSG:4326
var kml = format.writeFeatures(features, 'EPSG:4326', 'EPSG:3857');
// Save KML node as KML file using FileSaver.js script
var str = (new XMLSerializer).serializeToString(kml);
var blob = new Blob([str], {type: "text/plain;charset=utf-8;"});
saveAs(blob, "NovaCamada.kml");
});
主要问题是KML文件结果始终包含以度量单位(我需要度单位!)表示顶点的功能,即使在EPSG:4326上显示KML文件也是如此。
最糟糕的(也是我问题的目的)是,即使我将将将功能写入kml格式的代码更改为:
var kml = format.writeFeatures(features);
或:
结果是一样的,没有任何变化
我希望ol.format.KML类将特性从EPSG:3857转换为EPSG:4326。我走对了吗?
在格式化功能之前,是否需要对其应用任何重投影?有人能帮我完成这个任务吗
我说清楚了吗
注意:
writeFeatures
作为第二个参数,使用具有两个属性的对象文字:数据投影
和功能投影
KML格式有一个默认的数据投影
,EPSG:4326。这意味着您只需在您的案例中指定功能投影
:
var result=kmlFormat.writeFeatures(features,{featureProjection:'EPSG:3857'});
不相关的注意:我们正在更改OpenLayers 3.1.0中writeFeatures
的返回类型<对于任何格式,code>writeFeatures将始终返回字符串。这意味着您不需要自己使用XMLSerializer
对writeFeatures
的结果进行序列化
var kml = format.writeFeatures(features, 'EPSG:3857', 'EPSG:4326');