fix: compute length of all shapes for MultiLineString (not only first) (#2310)

This commit is contained in:
Yohan Boniface 2024-12-02 19:46:45 +01:00 committed by GitHub
commit 8569b827ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 53 additions and 3 deletions

View file

@ -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())
},
})

View file

@ -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()