Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Leaflet layer to WKT
function toWKT(layer) {
var lng, lat, coords = [];
if (layer instanceof L.Polygon || layer instanceof L.Polyline) {
var latlngs = layer.getLatLngs();
for (var i = 0; i < latlngs.length; i++) {
latlngs[i]
coords.push(latlngs[i].lng + " " + latlngs[i].lat);
if (i === 0) {
lng = latlngs[i].lng;
lat = latlngs[i].lat;
}
};
if (layer instanceof L.Polygon) {
return "POLYGON((" + coords.join(",") + "," + lng + " " + lat + "))";
} else if (layer instanceof L.Polyline) {
return "LINESTRING(" + coords.join(",") + ")";
}
} else if (layer instanceof L.Marker) {
return "POINT(" + layer.getLatLng().lng + " " + layer.getLatLng().lat + ")";
}
}
@bmcbride
Copy link
Author

bmcbride commented Dec 10, 2012

A simple little function for generating Well Known Text (WKT) from a Leaflet layer. This can be used with the excellent Leaflet.draw plugin when you want to save the layer geometry back to your database. Can be used as follows:

var drawnItems = new L.LayerGroup();
map.on('draw:created', function (e) {
    var type = e.layerType;
    var layer = e.layer;
    drawnItems.addLayer(layer);
    console.log(toWKT(layer));
});
map.addLayer(drawnItems);

@bmcbride
Copy link
Author

bmcbride commented May 10, 2013

Removed jQuery dependency.

@gabryro2210
Copy link

gabryro2210 commented Jul 21, 2013

thank's a lot, this is great

@turgutm
Copy link

turgutm commented Oct 21, 2013

Thanks for sharing!

@bmcbride
Copy link
Author

bmcbride commented Dec 5, 2013

This little hack is no longer necessary now that you can combine Leaflet's toGeoJSON() method with the excellent Terraformer library. Make sure to include Terraformer core and the WKT Parser scripts to your Leaflet page and have the draw event spit out some WKT for inserting into your database:

map.on('draw:created', function (e) {
    var geojson = e.layer.toGeoJSON();
    var wkt = Terraformer.WKT.convert(geojson.geometry);
    console.log(wkt);
    drawnItems.addLayer(e.layer);
});

@stefanocudini
Copy link

stefanocudini commented Jun 18, 2014

WTF at line 6????

latlngs[i]

@hmarina
Copy link

hmarina commented Jun 2, 2015

Hello every body.

I tried to get wkt when draw.editstop but it doesnt work. What is wrong or what I need you to do, to get wkt when I edit a shape and stop draw.
Thank you.

map.on('draw:editstop', function (drawnItems) {
var geojson = drawnItems.toGeoJSON();
var wkt = Terraformer.WKT.convert(geojson.geometry);
alert(wkt);
});

@gisprogrammer
Copy link

gisprogrammer commented Jun 7, 2017

My IMO better version.

function toWKT (layer) {
    var lng, lat, coords = [];
		if (layer instanceof L.Polygon || layer instanceof L.Polyline) {
			var latlngs = layer.getLatLngs();
		for (var i = 0; i < latlngs.length; i++) {
				var latlngs1 = latlngs[i];
				if (latlngs1.length){
				for (var j = 0; j < latlngs1.length; j++) {
					coords.push(latlngs1[j].lng + " " + latlngs1[j].lat);
					if (j === 0) {
						lng = latlngs1[j].lng;
						lat = latlngs1[j].lat;
					}
				}}
				else
				{
					coords.push(latlngs[i].lng + " " + latlngs[i].lat);
					if (i === 0) {
						lng = latlngs[i].lng;
						lat = latlngs[i].lat;
					}}
		};
			if (layer instanceof L.Polygon) {
				return "POLYGON((" + coords.join(",") + "," + lng + " " + lat + "))";
			} else if (layer instanceof L.Polyline) {
				return "LINESTRING(" + coords.join(",") + ")";
			}
		} else if (layer instanceof L.Marker) {
			return "POINT(" + layer.getLatLng().lng + " " + layer.getLatLng().lat + ")";
		}
	};
	map.on('draw:edited', function (e) {
		e.layers.eachLayer(function(layer) {
    		console.log(toWKT(layer));
    	});
	});
	map.on('draw:created', function (e) {
		var layer = e.layer;
		console.log(toWKT(layer));
		});

@Operator27
Copy link

Operator27 commented Oct 29, 2018

My IMO better version.

function toWKT (layer) {
    var lng, lat, coords = [];
		if (layer instanceof L.Polygon || layer instanceof L.Polyline) {
			var latlngs = layer.getLatLngs();
		for (var i = 0; i < latlngs.length; i++) {
				var latlngs1 = latlngs[i];
				if (latlngs1.length){
				for (var j = 0; j < latlngs1.length; j++) {
					coords.push(latlngs1[j].lng + " " + latlngs1[j].lat);
					if (j === 0) {
						lng = latlngs1[j].lng;
						lat = latlngs1[j].lat;
					}
				}}
				else
				{
					coords.push(latlngs[i].lng + " " + latlngs[i].lat);
					if (i === 0) {
						lng = latlngs[i].lng;
						lat = latlngs[i].lat;
					}}
		};
			if (layer instanceof L.Polygon) {
				return "POLYGON((" + coords.join(",") + "," + lng + " " + lat + "))";
			} else if (layer instanceof L.Polyline) {
				return "LINESTRING(" + coords.join(",") + ")";
			}
		} else if (layer instanceof L.Marker) {
			return "POINT(" + layer.getLatLng().lng + " " + layer.getLatLng().lat + ")";
		}
	};
	map.on('draw:edited', function (e) {
		e.layers.eachLayer(function(layer) {
    		console.log(toWKT(layer));
    	});
	});
	map.on('draw:created', function (e) {
		var layer = e.layer;
		console.log(toWKT(layer));
		});

Works very well, thank you!!

@manojmsrit
Copy link

manojmsrit commented May 10, 2020

hello...Can I use this code to convert leaflettowkt ?? Do I need to add any reference ?

@deviirmr
Copy link

deviirmr commented Nov 19, 2020

@bmcbride Thank you :)
I confirm this solution is still valid in 2020
Add this two lib¨

<script type="text/javascript" src="https://unpkg.com/terraformer@1.0.7/terraformer.js"></script>
<script type="text/javascript" src="https://unpkg.com/terraformer-wkt-parser@1.1.2/terraformer-wkt-parser.js"></script>

This little hack is no longer necessary now that you can combine Leaflet's toGeoJSON() method with the excellent Terraformer library. Make sure to include Terraformer core and the WKT Parser scripts to your Leaflet page and have the draw event spit out some WKT for inserting into your database:

map.on('draw:created', function (e) {
    var geojson = e.layer.toGeoJSON();
    var wkt = Terraformer.WKT.convert(geojson.geometry);
    console.log(wkt);
    drawnItems.addLayer(e.layer);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment