Skip to content

Commit

Permalink
fix: compute length of all shapes for MultiLineString not only first
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed Dec 2, 2024
1 parent 401efc0 commit 666a92e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
22 changes: 19 additions & 3 deletions umap/static/umap/js/modules/rendering/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
},

Expand All @@ -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()
Expand Down Expand Up @@ -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())
},
})
Expand Down
34 changes: 34 additions & 0 deletions umap/tests/integration/test_view_polyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 666a92e

Please sign in to comment.