diff --git a/umap/static/umap/js/modules/rendering/ui.js b/umap/static/umap/js/modules/rendering/ui.js index f7779389..8e0e28e5 100644 --- a/umap/static/umap/js/modules/rendering/ui.js +++ b/umap/static/umap/js/modules/rendering/ui.js @@ -255,7 +255,10 @@ const PathMixin = { if (this._map.measureTools?.enabled()) { this._map._umap.tooltip.open({ content: this.getMeasure(), anchor: this }) } else if (this._map._umap.editEnabled && !this._map._umap.editedFeature) { - this._map._umap.tooltip.open({ content: translate('Click to edit'), anchor: this }) + this._map._umap.tooltip.open({ + content: translate('Click to edit'), + anchor: this, + }) } }, @@ -267,7 +270,9 @@ const PathMixin = { this._map.once('moveend', this.makeGeometryEditable, this) const pointsCount = this._parts.reduce((acc, part) => acc + part.length, 0) if (pointsCount > 100 && this._map.getZoom() < this._map.getMaxZoom()) { - this._map._umap.tooltip.open({ content: L._('Please zoom in to edit the geometry') }) + this._map._umap.tooltip.open({ + content: L._('Please zoom in to edit the geometry'), + }) this.disableEdit() } else { this.enableEdit() @@ -380,8 +385,19 @@ export const LeafletPolyline = Polyline.extend({ }, getMeasure: function (shape) { + let shapes + if (shape) { + shapes = [shape] + } else if (LineUtil.isFlat(this._latlngs)) { + shapes = [this._latlngs] + } else { + shapes = this._latlngs + } // FIXME: compute from data in feature (with TurfJS) - const length = L.GeoUtil.lineLength(this._map, shape || this._defaultShape()) + const length = shapes.reduce( + (acc, shape) => acc + L.GeoUtil.lineLength(this._map, shape), + 0 + ) return L.GeoUtil.readableDistance(length, this._map.measureTools.getMeasureUnit()) }, }) diff --git a/umap/tests/integration/test_view_polyline.py b/umap/tests/integration/test_view_polyline.py index 9ed949be..99b52fb9 100644 --- a/umap/tests/integration/test_view_polyline.py +++ b/umap/tests/integration/test_view_polyline.py @@ -49,3 +49,37 @@ def test_should_open_popup_on_click(live_server, map, page, bootstrap): # Close popup page.locator("#map").click() expect(line).to_have_attribute("stroke-opacity", "0.5") + + +def test_can_use_measure_on_name(live_server, map, page): + data = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {"name": "linestring"}, + "geometry": { + "type": "LineString", + "coordinates": [ + [11.25, 53.585984], + [10.151367, 52.975108], + ], + }, + }, + { + "type": "Feature", + "properties": {"name": "multilinestring"}, + "geometry": { + "type": "MultiLineString", + "coordinates": [[[8, 53], [13, 52]], [[12, 51], [15, 52]]], + }, + }, + ], + } + map.settings["properties"]["labelKey"] = "{name} ({measure})" + map.settings["properties"]["onLoadPanel"] = "databrowser" + map.save() + DataLayerFactory(map=map, data=data) + page.goto(f"{live_server.url}{map.get_absolute_url()}#6/10/50") + expect(page.get_by_text("linestring (99.7 km)")).to_be_visible() + expect(page.get_by_text("multilinestring (592 km)")).to_be_visible()