Skip to content

Instantly share code, notes, and snippets.

@bmcbride
Last active June 7, 2022 02:17
Show Gist options
  • Select an option

  • Save bmcbride/4248238 to your computer and use it in GitHub Desktop.

Select an option

Save bmcbride/4248238 to your computer and use it in GitHub Desktop.
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
Copy Markdown
Author

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
Copy Markdown
Author

Removed jQuery dependency.

@gabryro2210

Copy link
Copy Markdown

thank's a lot, this is great

@turgutm

turgutm commented Oct 21, 2013

Copy link
Copy Markdown

Thanks for sharing!

@bmcbride

bmcbride commented Dec 5, 2013

Copy link
Copy Markdown
Author

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
Copy Markdown

WTF at line 6????

latlngs[i]

@hmarina

hmarina commented Jun 2, 2015

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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

@deviirmr

deviirmr commented Nov 19, 2020

Copy link
Copy Markdown

@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