简体   繁体   English

Openlayers根据您的地图编写并保存KML

[英]Openlayers write and save a KML based on your map

Is it possible to write and save a KML from OpenLayers? 是否可以从OpenLayers编写和保存KML? Anyone know of an example of exporting one? 有人知道出口一个例子吗?

You can export only the vector features to KML. 您只能将矢量要素导出到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);
}

UPDATE UPDATE

In order to force the browser to download the KML string as a KML file you need to send that string back to the server-side so it can be returned to the browser as a file to download. 为了强制浏览器将KML字符串作为KML文件下载,您需要将该字符串发送回服务器端,以便将其作为要下载的文件返回到浏览器。

You haven't specified what language/platform/etc you are using on the server-side But this is what i did in C#. 你还没有在服务器端指定你正在使用的语言/平台/等等。但这就是我在C#中所做的。

I created a handler which takes in a the filename from the querystring and the KML from a textarea form. 我创建了一个处理程序,它从查询字符串中获取文件名,从textarea表单中获取KML。

KMLDownload.ashx: 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;
        }
    }

}

Then from my javascript side i simply call this to initiate the download: 然后从我的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();

Here's some JQuery action to save: 这是一些要保存的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>');
});

IF you are using Openlayers 3 or 4, you will find that the syntax of previous (2012) answers does not work anymore. 如果您使用的是Openlayers 3或4,您会发现之前(2012)答案的语法不再有效。

This does: 这样做:

        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;
        }

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

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