From f3a2ad9854a7a945b630929b79c2c863fa64b758 Mon Sep 17 00:00:00 2001 From: balladaniel <96133731+balladaniel@users.noreply.github.com> Date: Thu, 23 Nov 2023 20:44:33 +0100 Subject: [PATCH] MultiPoint support - support for MultiPoint features (MultiLineString and MultiPolygon features were and are working as expected - report any issues you might encounter) - reworked feature styling process: -- now is a switch -- added an error message if feature type is unknown --- leaflet-dataclassification.js | 67 ++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/leaflet-dataclassification.js b/leaflet-dataclassification.js index dd16ba7..71246d9 100644 --- a/leaflet-dataclassification.js +++ b/leaflet-dataclassification.js @@ -1164,32 +1164,51 @@ L.DataClassification = L.GeoJSON.extend({ // apply symbology to features this.eachLayer(function(layer) { if (layer.feature.properties[this._field] == null && nodataignore) { - layer.remove() + layer.remove(); } else { - if (layer.feature.geometry.type == "Point" || layer.feature.geometry.type == "MultiPoint") { - var coords = layer.feature.geometry.coordinates; - var style = (mode_point == "color" ? stylePoint_color(layer.feature.properties[this._field]) : stylePoint_size(layer.feature.properties[this._field], this.options)) - style.shape = ps; - - const svgIcon = L.divIcon({ - html: svgCreator({shape: style.shape, size: style.radius, color: style.fillColor}), - className: "", - iconSize: [25, 25], - iconAnchor: [17, 25/2], - }); - layer.setIcon(svgIcon); - } - if (layer.feature.geometry.type == "LineString" || layer.feature.geometry.type == "MultiLineString") { - layer.setStyle((mode_line == "width" ? styleLine_width(layer.feature.properties[this._field]) : styleLine_color(layer.feature.properties[this._field])))/*.addTo(map)*/; + switch (layer.feature.geometry.type) { + case "Point": + var coords = layer.feature.geometry.coordinates; + var style = (mode_point == "color" ? stylePoint_color(layer.feature.properties[this._field]) : stylePoint_size(layer.feature.properties[this._field], this.options)) + style.shape = ps; + + const svgIcon = L.divIcon({ + html: svgCreator({shape: style.shape, size: style.radius, color: style.fillColor}), + className: "", + iconSize: [25, 25], + iconAnchor: [17, 25/2], + }); + layer.setIcon(svgIcon); + break; + case "MultiPoint": + var style = (mode_point == "color" ? stylePoint_color(layer.feature.properties[this._field]) : stylePoint_size(layer.feature.properties[this._field], this.options)) + var mpfeatures = layer._layers; + for (const property in mpfeatures) { + mpfeatures[property].setIcon(L.divIcon({ + html: svgCreator({shape: style.shape, size: style.radius, color: style.fillColor}), + className: "", + iconSize: [25, 25], + iconAnchor: [17, 25/2], + })); + } + break; + case "LineString": + case "MultiLineString": + layer.setStyle((mode_line == "width" ? styleLine_width(layer.feature.properties[this._field]) : styleLine_color(layer.feature.properties[this._field])))/*.addTo(map)*/; + break; + case "Polygon": + case "MultiPolygon": + if (mode_polygon == "hatch") { + layer._path.style['fill'] = stylePolygon_hatch(layer.feature.properties[this._field], this.options); // this messy workaround is needed due to Leaflet ignoring `className` in layer.setStyle(). See https://github.com/leaflet/leaflet/issues/2662. + layer._path.style['fill-opacity'] = (options.style.fillOpacity != null ? options.style.fillOpacity : 0.7); + } else { + layer.setStyle(stylePolygon_color(layer.feature.properties[this._field], this.options))/*.addTo(map)*/; + } + break; + default: + console.error('Error: Unknown feature type: ', layer.feature.geometry.type, layer.feature) + break; } - if (layer.feature.geometry.type == "Polygon" || layer.feature.geometry.type == "MultiPolygon") { - if (mode_polygon == "hatch") { - layer._path.style['fill'] = stylePolygon_hatch(layer.feature.properties[this._field], this.options); // this messy workaround is needed due to Leaflet ignoring `className` in layer.setStyle(). See https://github.com/leaflet/leaflet/issues/2662. - layer._path.style['fill-opacity'] = (options.style.fillOpacity != null ? options.style.fillOpacity : 0.7); - } else { - layer.setStyle(stylePolygon_color(layer.feature.properties[this._field], this.options))/*.addTo(map)*/; - } - } } });