Openlayers write and save KML based on your map

Can I write and save KML from OpenLayers? Does anyone know an example of exporting one?

+7
source share
3 answers

You can only export vector functions in 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

To force the browser to load the KML string as a KML file, you need to send this string back to the server side so that it can be returned to the browser as a download file.

You did not specify which language / platform / etc you use on the server side. But this is what I did in C #.

I created a handler that takes a file name from the query string and KML from the textarea form.

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, on my part of javascript, I just call this to start the download:

 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(); 
+9
source

Here is some jQuery save action:

 $('#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>'); }); 
+3
source

IF you use Openlayers 3 or 4, you will find that the syntax of the previous (2012) answers no longer works.

It 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; } 
0
source

All Articles