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

This comment has been minimized.

Copy link
Owner Author

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

This comment has been minimized.

Copy link
Owner Author

commented May 10, 2013

Removed jQuery dependency.

@gabryro2210

This comment has been minimized.

Copy link

commented Jul 21, 2013

thank's a lot, this is great

@turgutm

This comment has been minimized.

Copy link

commented Oct 21, 2013

Thanks for sharing!

@bmcbride

This comment has been minimized.

Copy link
Owner Author

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

This comment has been minimized.

Copy link

commented Jun 18, 2014

WTF at line 6????

latlngs[i]
@hmarina

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.