Skip to content

Instantly share code, notes, and snippets.

@LuisSevillano
Created January 19, 2024 10:54
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 LuisSevillano/f4531e72181d3641cfc4dbb2a726fc54 to your computer and use it in GitHub Desktop.
Save LuisSevillano/f4531e72181d3641cfc4dbb2a726fc54 to your computer and use it in GitHub Desktop.
Gell all polygons from a rendered Leaflet map
// chrome snippet
var layersMarkers = [];
var layersPolygons = [];
var layers = []
var geojson = {
"type": "FeatureCollection",
"features": []
}
var output = {
"type": "FeatureCollection",
"features": []
}
map.eachLayer(function(layer) {
layers.push(layer);
if (layer instanceof L.Marker)
layersMarkers.push(layer);
if (layer instanceof L.Polygon)
layersMarkers.push(layer);
if (typeof layer.toGeoJSON === 'function')
geojson.features.push(layer.toGeoJSON());
});
output.features = (geojson.features.filter(d=>d.geometry?.type === 'Polygon' && d.geometry?.coordinates[0][0]))
copy(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment