Skip to content

Instantly share code, notes, and snippets.

@bmcbride
Last active June 7, 2022 02:17
Show Gist options
  • Star 42 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save bmcbride/4248238 to your computer and use it in GitHub Desktop.
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
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

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

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

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

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