Group different geometries in a single vector layer
Closed this issue · 4 comments
Enable the ability to group all geometries drawn in a single layer to download all together.
A specific layer group was created for the geometries drawn. When you create one, it is automatically saved in that group. They can be removed all together from the group menu or using LeafletDraw options. They can also be edited, both their position and their style. The function that removes layers and the way to create them in map.js.
mapa.on('draw:created', (e) => {
/* ... */
mapa.editableLayers[type].push(layer);
if (Object.values(drawnItems._layers).length === 0) {
if (mapa.groupLayers["dibujos"] === undefined) {
mapa.groupLayers["dibujos"] = [];
}
mapa.addLayerToGroup(name, "dibujos");
addedLayers.push({
id: "dibujos",
layer: [layer],
name: "dibujos",
type: "dibujos",
isActive: true,
section: "Dibujos"
});
menu_ui.addFileLayer("Dibujos", "dibujos", "dibujos", "dibujos", "dibujos", true); updateNumberofLayers("Dibujos");
} else {
mapa.addLayerToGroup(name, "dibujos");
addedLayers.forEach(lyr => {
if (lyr.id === "dibujos") {
lyr.layer.push(layer);
}
});
updateNumberofLayers("Dibujos");
}
drawnItems.addLayer(layer);
/* ... */
mapa.methodsEvents['add-layer'].forEach(method => method(mapa.editableLayers));
});
Finally, the geometries created to execute the geo processes are removed from the group of drawings after their execution.
removeGeometryFromDrawingsGroup(selectedGeom) {
if (mapa.groupLayers.hasOwnProperty("dibujos")) { // Remove the rectangle from groupLayers["dibujos"]
const layerIdx = mapa.groupLayers["dibujos"].findIndex(lyr => lyr === selectedGeom.name);
if (layerIdx >= 0)
mapa.groupLayers["dibujos"].splice(layerIdx, 1);
}
let layerSection;
addedLayers.forEach(lyr => {
let i = 0;
if (lyr.id === "dibujos") {
lyr.layer.forEach(e => { // Remove rectangle from addedLayers whith "dibujos" id
if (selectedGeom.name === e.name) {
layerSection = lyr.section;
lyr.layer.splice(i, 1);
updateNumberofLayers(layerSection);
showTotalNumberofLayers();
}
i++;
});
}
});
if (mapa.groupLayers["dibujos"].length === 0) { // If the rectangle was the only one on the map, remove groupLayers["dibujos"], addedLayers whith "dibujos" id and "Dibujos" section.
delete mapa.groupLayers["dibujos"];
let section;
addedLayers.forEach(lyr => {
if (lyr.id === "dibujos") {
section = lyr.section;
}
});
delFileItembyID("dibujos");
deleteLayerGeometry("dibujos", true);
updateNumberofLayers(section);
showTotalNumberofLayers();
}
}
Changed the function that added the layers so that it can be reused.
function addLayerToDrawingsGroup(name, layer, section, groupId, group) {
if (mapa.groupLayers[group] === undefined) {
mapa.groupLayers[group] = [];
mapa.addLayerToGroup(name, group);
addedLayers.push({
id: groupId,
layer: L.layerGroup([layer]),
name: groupId,
type: groupId,
isActive: true,
section: section
});
menu_ui.addFileLayer(section, groupId, groupId, groupId, groupId, true);
} else {
mapa.addLayerToGroup(name, group);
addedLayers.forEach(lyr => {
if (lyr.id === groupId) {
lyr.layer.addLayer(layer);
}
});
}
updateNumberofLayers(section);
}
Temporarily, L.Draw was modified, since it is currently in charge of managing the geometries.
L.Draw was returned to its original state and now the functionalities have been modified in map.js by means of an extend.