Last active
June 7, 2022 02:17
-
-
Save bmcbride/4248238 to your computer and use it in GitHub Desktop.
Leaflet layer to WKT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
@bmcbride Thank you :)
I confirm this solution is still valid in 2020
Add this two lib¨