Openlayers基于地图编写和保存KML文件的方法

8

能否从OpenLayers编写和保存KML文件?有没有导出的示例?


2
这里有一个类似的解决方案:或许可以帮到你?http://gis.stackexchange.com/questions/17031/openlayers-format-kml-write-style - user1040259
3个回答

10

你只能将矢量要素导出到KML。

function GetKMLFromFeatures(features) {
    var format = new OpenLayers.Format.KML({
        'maxDepth':10,
        'extractStyles':true,
        'internalProjection': map.baseLayer.projection,
        'externalProjection': new OpenLayers.Projection("EPSG:4326")
    });

    return format.write(features);
}

更新

为了让浏览器将KML字符串作为KML文件下载,您需要将该字符串发送回服务器端,以便返回给浏览器作为要下载的文件。

您没有说明服务器端使用什么语言/平台等信息,但这是我在C#中所做的。

我创建了一个处理程序,从查询字符串中获取文件名,并从文本区域表单中获取KML。

KMLDownload.ashx:

<%@ WebHandler Language="C#" Class="KMLDownload" %>

using System;
using System.Web;

public class KMLDownload : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {


        HttpResponse response = context.Response;

        string kml = context.Request["kml"];
        string filename = context.Request.QueryString["filename"];

        if (String.IsNullOrEmpty(kml))
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("{\"error\":\"No files recevied\"}");
        }
        else
        {

            if (String.IsNullOrEmpty(filename)){
                filename = "Features_KML.kml";
            }

            // force a download of the kml file.
            response.Clear();
            response.ContentType = "application/kml";
            response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
            response.AddHeader("content-legth", kml.Length.ToString());
            response.Write(kml.ToString());
            response.End();
        }

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

然后从我的 JavaScript 代码中,我只需要调用这段代码来启动下载:

var filename = "NameofKMLfileI_WANT.kml";

var url = "secure/KMLDownload.ashx";
if (filename) {
    url += "?filename=" + filename;
}

var input = '<TEXTAREA name="kml">' + kml + '</TEXTAREA>';

//send request
jQuery('<form action="' + url + '" method="post">' + input + '</form>').appendTo('body').submit().remove();

不错。你可能会如何编写或保存KML? - user1040259
感谢您的帮助。强制浏览器启动 KML 的下载。 - user1040259
1
一些代码可在某些浏览器中实现无需服务器交互的保存功能:http://hackworthy.blogspot.co.nz/2012/05/savedownload-data-generated-in.html - ZiglioUK
2
https://dev59.com/wHA65IYBdhLWcg3wuhIR - Christophe Roussy
@ChristopheRoussy 非常有趣...我需要测试一下。 - capdragon
显示剩余6条评论

3
这里是一些JQuery代码以进行保存操作:
$('#saveKML').click(function() {
 var kmlFormat = new OpenLayers.Format.KML();
 var newWindow = window.open('', 
  'KML Export ' + (new Date()).getTime(), "width=300,height=300");
   newWindow.document.write('<textarea id="kml" style="width: 100%; height: 100%">' + 
   kmlFormat.write(features) + '</textarea>');
});

0
如果您正在使用Openlayers 3或4,您会发现之前(2012年)的答案语法不再适用。
这个可以:
        function GetKMLFromFeatures(features) {
            var format = new ol.format.KML();
            var kml = format.writeFeatures(features, {featureProjection: 'EPSG:3857'});
            return kml;
        }
        function GetGeoJSONFromFeatures(features) {
            var format = new ol.format.GeoJSON();
            var geoJSON = format.writeFeatures(features, {featureProjection: 'EPSG:3857'});
            return geoJSON;
        }
        function GetFeaturesFromLayer(layer) {
            var source = layer.getSource();
            var features = source.getFeatures();
            return features;
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接