Skip to content

Instantly share code, notes, and snippets.

@pierrelorioux
Forked from bmcbride/LeafletToWKT.js
Created August 13, 2013 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pierrelorioux/6225893 to your computer and use it in GitHub Desktop.
Save pierrelorioux/6225893 to your computer and use it in GitHub Desktop.
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 + ")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment