From 0389e4659c0ffaaad184d22eeb6b311474adee78 Mon Sep 17 00:00:00 2001 From: lrnselfreliance <33263708+lrnselfreliance@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:17:31 -0700 Subject: [PATCH] An overlay's layers which the basemap already draws start hidden (#969) A file of features often carries a basemap's layers too (roads, water, buildings, place names). Drawn over the basemap they double every road and label. Those layers, by the names of the Protomaps and OpenMapTiles schemas, are now off until they are checked; every other layer is on as before, and a choice already made is kept. Co-authored-by: Claude Fable 5.2 --- app/src/components/MapViewerOverlays.test.js | 13 +++++++-- app/src/components/mapOverlayStyle.test.js | 30 ++++++++++---------- app/src/components/mapOverlays.test.js | 30 ++++++++++++++++++++ app/src/map-shared/overlays.js | 23 +++++++++++++-- docker/help/wrolpi-help | 2 +- modules/map/static/js/overlays.js | 23 +++++++++++++-- 6 files changed, 99 insertions(+), 22 deletions(-) diff --git a/app/src/components/MapViewerOverlays.test.js b/app/src/components/MapViewerOverlays.test.js index 97ee28ec..148e0923 100644 --- a/app/src/components/MapViewerOverlays.test.js +++ b/app/src/components/MapViewerOverlays.test.js @@ -228,7 +228,8 @@ describe('an overlay in the layer control', () => { const map = await renderViewer([PLANET, FEATURES, SATELLITE]); await click(screen.getByLabelText('Topographic Features')); - expect([WATER, POWER_LINES, TOWERS].map(id => map.visibility(id))).toEqual(['visible', 'visible', 'visible']); + // The basemap draws water already, so the file's own is left off. + expect([WATER, POWER_LINES, TOWERS].map(id => map.visibility(id))).toEqual(['none', 'visible', 'visible']); // The other overlay is untouched. expect(map.visibility(RASTER)).toBe('none'); expect(JSON.parse(localStorage.getItem(STORAGE_KEY_OVERLAYS))) @@ -244,7 +245,7 @@ describe('an overlay in the layer control', () => { })); const map = await renderViewer([PLANET, FEATURES]); expect(screen.getByLabelText('Topographic Features')).toBeChecked(); - expect([WATER, POWER_LINES, TOWERS].map(id => map.visibility(id))).toEqual(['visible', 'visible', 'none']); + expect([WATER, POWER_LINES, TOWERS].map(id => map.visibility(id))).toEqual(['none', 'visible', 'none']); }); test('each layer of a vector file has its own checkbox', async () => { @@ -252,6 +253,12 @@ describe('an overlay in the layer control', () => { await click(screen.getByLabelText('Topographic Features')); await click(screen.getByRole('button', {name: 'Show layers of Topographic Features'})); + // A layer of something the basemap draws starts unchecked, and can be checked. + expect(screen.getByLabelText('water')).not.toBeChecked(); + expect(screen.getByLabelText('power_lines')).toBeChecked(); + await click(screen.getByLabelText('water')); + expect(screen.getByLabelText('water')).toBeChecked(); + await click(screen.getByLabelText('towers')); expect([WATER, POWER_LINES, TOWERS].map(id => map.visibility(id))).toEqual(['visible', 'visible', 'none']); @@ -275,6 +282,8 @@ describe('the basemap layer groups', () => { test("do not show or hide an overlay's layer of the same name", async () => { const map = await renderViewer([PLANET, FEATURES]); await click(screen.getByLabelText('Topographic Features')); + await click(screen.getByRole('button', {name: 'Show layers of Topographic Features'})); + await click(screen.getByLabelText('water')); expect(map.visibility('water')).toBe('visible'); expect(map.visibility(WATER)).toBe('visible'); diff --git a/app/src/components/mapOverlayStyle.test.js b/app/src/components/mapOverlayStyle.test.js index a8a9e758..b1901ab6 100644 --- a/app/src/components/mapOverlayStyle.test.js +++ b/app/src/components/mapOverlayStyle.test.js @@ -16,7 +16,7 @@ const FILE = { name: 'features.pmtiles', map_path: 'extras/features.pmtiles', path: 'map/extras/features.pmtiles', kind: 'overlay', tile_type: 'mvt', visible: true, style_path: 'map/extras/style.json', - vector_layers: [layer('repeaters'), layer('roads'), layer('parks')], + vector_layers: [layer('repeaters'), layer('trails'), layer('parks')], }; const SOURCE = 'overlay:extras/features.pmtiles'; // The map's own icons: every image a style names is one of these. @@ -37,7 +37,7 @@ const STYLE = { {id: 'relief', type: 'raster', source: 'relief'}, {id: 'parks', type: 'fill', source: 'features', 'source-layer': 'parks', paint: {'fill-color': '#9c9'}}, {id: 'contours', type: 'line', source: 'contours', 'source-layer': 'contours'}, - {id: 'roads', type: 'line', source: 'features', 'source-layer': 'roads', paint: {'line-color': '#c60'}}, + {id: 'trails', type: 'line', source: 'features', 'source-layer': 'trails', paint: {'line-color': '#c60'}}, // A layer of the file's source which this file does not have. {id: 'buildings', type: 'fill', source: 'features', 'source-layer': 'buildings'}, { @@ -58,11 +58,11 @@ describe('adaptOverlayStyle', () => { test("keeps the layers which draw this file, in the style's order", () => { const result = adapted(); expect(result.layers.map(l => l.id)).toEqual([ - `${SOURCE}:parks:parks`, `${SOURCE}:roads:roads`, `${SOURCE}:repeaters:repeaters`, + `${SOURCE}:parks:parks`, `${SOURCE}:trails:trails`, `${SOURCE}:repeaters:repeaters`, ]); expect(result.skipped).toBe(0); // What the style said of a layer is kept. - expect(byId(result, 'roads').paint).toEqual({'line-color': '#c60'}); + expect(byId(result, 'trails').paint).toEqual({'line-color': '#c60'}); expect(byId(result, 'repeaters').minzoom).toBe(10); }); @@ -79,12 +79,12 @@ describe('adaptOverlayStyle', () => { ...STYLE, layers: [ // Another source has a layer of the same name. - {id: 'other-roads', type: 'line', source: 'contours', 'source-layer': 'roads'}, + {id: 'other-trails', type: 'line', source: 'contours', 'source-layer': 'trails'}, ...STYLE.layers, ], }; expect(adapted(style).layers.map(l => l.id)).toEqual([ - `${SOURCE}:parks:parks`, `${SOURCE}:roads:roads`, `${SOURCE}:repeaters:repeaters`, + `${SOURCE}:parks:parks`, `${SOURCE}:trails:trails`, `${SOURCE}:repeaters:repeaters`, ]); }); @@ -114,15 +114,15 @@ describe('adaptOverlayStyle', () => { const style = { ...STYLE, layers: [ - {id: 'bad-paint', type: 'line', source: 'features', 'source-layer': 'roads', paint: {'line-width': 'wide'}}, - {id: 'bad-type', type: 'sparkles', source: 'features', 'source-layer': 'roads'}, + {id: 'bad-paint', type: 'line', source: 'features', 'source-layer': 'trails', paint: {'line-width': 'wide'}}, + {id: 'bad-type', type: 'sparkles', source: 'features', 'source-layer': 'trails'}, ...STYLE.layers, ], }; const result = adapted(style); expect(result.skipped).toBe(2); expect(result.layers.map(l => l.id)).toEqual([ - `${SOURCE}:parks:parks`, `${SOURCE}:roads:roads`, `${SOURCE}:repeaters:repeaters`, + `${SOURCE}:parks:parks`, `${SOURCE}:trails:trails`, `${SOURCE}:repeaters:repeaters`, ]); }); @@ -205,7 +205,7 @@ describe('fonts', () => { const style = {...STYLE, layers: [{...STYLE.layers[6], layout: {'text-field': ['get', 'callsign']}}]}; expect(byId(adapted(style), 'repeaters').layout['text-font']).toEqual(['Noto Sans Regular']); // A layer without text is not given a font. - expect(byId(adapted(), 'roads').layout).toBeUndefined(); + expect(byId(adapted(), 'trails').layout).toBeUndefined(); }); }); @@ -326,7 +326,7 @@ describe('icons', () => { 'icon-color': '#c00', 'icon-halo-color': '#ffffff', 'icon-halo-width': 1, 'text-color': '#00c', }); // A layer without an icon is not given an icon's paint. - expect(byId(adapted(), 'roads').paint).toEqual({'line-color': '#c60'}); + expect(byId(adapted(), 'trails').paint).toEqual({'line-color': '#c60'}); }); }); @@ -480,7 +480,7 @@ describe('a styled overlay on the map', () => { test('is drawn with its style, not the generic colors', () => { const layers = buildOverlayLayers(styled, setOverlayEnabled({}, styled, true)); expect(layers.map(l => [l.id, l.type])).toEqual([ - [`${SOURCE}:parks:parks`, 'fill'], [`${SOURCE}:roads:roads`, 'line'], + [`${SOURCE}:parks:parks`, 'fill'], [`${SOURCE}:trails:trails`, 'line'], [`${SOURCE}:repeaters:repeaters`, 'symbol'], ]); expect(layers[2].layout['icon-image']).toBe(`${ICONS}:ham_radio`); @@ -490,7 +490,7 @@ describe('a styled overlay on the map', () => { expect(visibility({})).toEqual(['none', 'none', 'none']); let state = setOverlayEnabled({}, styled, true); expect(visibility(state)).toEqual(['visible', 'visible', 'visible']); - state = setOverlayLayerEnabled(state, styled, 'roads', false); + state = setOverlayLayerEnabled(state, styled, 'trails', false); expect(visibility(state)).toEqual(['visible', 'none', 'visible']); }); @@ -505,9 +505,9 @@ describe('a styled overlay on the map', () => { test('only the layers its style draws can be chosen', () => { const file = {...styled, vector_layers: [...FILE.vector_layers, layer('buildings')]}; - expect(overlayToggleLayers(file).map(l => l.id)).toEqual(['repeaters', 'roads', 'parks']); + expect(overlayToggleLayers(file).map(l => l.id)).toEqual(['repeaters', 'trails', 'parks']); // Without a style, every layer of the file is drawn. expect(overlayToggleLayers({...file, styleLayers: undefined}).map(l => l.id)) - .toEqual(['repeaters', 'roads', 'parks', 'buildings']); + .toEqual(['repeaters', 'trails', 'parks', 'buildings']); }); }); diff --git a/app/src/components/mapOverlays.test.js b/app/src/components/mapOverlays.test.js index aadacad0..6679f1a1 100644 --- a/app/src/components/mapOverlays.test.js +++ b/app/src/components/mapOverlays.test.js @@ -3,6 +3,7 @@ import { addOverlaysToStyle, buildOverlayLayers, buildOverlaySource, + isBasemapLayer, isOverlayEnabled, isOverlayLayerEnabled, loadOverlayState, @@ -122,6 +123,35 @@ describe('what the user chose to see', () => { expect(isOverlayEnabled(state, FEATURES)).toBe(true); }); + test('a layer the basemap already draws is off until it is turned on', () => { + // Both schemas a basemap is commonly made in, whatever the case. + for (const id of ['roads', 'transportation', 'transportation_name', 'water', 'building', 'Place']) { + expect(isBasemapLayer(id)).toBe(true); + } + for (const id of ['towers', 'power_lines', 'housenumber', 'water_towers', '', undefined]) { + expect(isBasemapLayer(id)).toBe(false); + } + + const file = { + ...FEATURES, + vector_layers: [{id: 'towers', geometry: 'Point'}, {id: 'transportation', geometry: 'LineString'}], + }; + let state = setOverlayEnabled({}, file, true); + expect(isOverlayLayerEnabled(state, file, 'towers')).toBe(true); + expect(isOverlayLayerEnabled(state, file, 'transportation')).toBe(false); + const visibility = () => Object.fromEntries( + buildOverlayLayers(file, state).map(l => [l['source-layer'], l.layout.visibility])); + expect(visibility()).toEqual({towers: 'visible', transportation: 'none'}); + + // The choice is the user's. + state = setOverlayLayerEnabled(state, file, 'transportation', true); + expect(isOverlayLayerEnabled(state, file, 'transportation')).toBe(true); + expect(visibility()).toEqual({towers: 'visible', transportation: 'visible'}); + state = setAllOverlayLayersEnabled(state, file, false); + state = setAllOverlayLayersEnabled(state, file, true); + expect(visibility()).toEqual({towers: 'visible', transportation: 'visible'}); + }); + test('all the layers of a file can be turned off and on', () => { let state = setAllOverlayLayersEnabled(setOverlayEnabled({}, FEATURES, true), FEATURES, false); expect(FEATURES.vector_layers.map(l => isOverlayLayerEnabled(state, FEATURES, l.id))) diff --git a/app/src/map-shared/overlays.js b/app/src/map-shared/overlays.js index 73f45766..05b47006 100644 --- a/app/src/map-shared/overlays.js +++ b/app/src/map-shared/overlays.js @@ -126,9 +126,28 @@ export function isOverlayEnabled(state, file) { return state?.[file.map_path]?.enabled === true; } -// Once a file is on, each of its layers is drawn until the user turns that layer off. +// The layers of the schemas a basemap is commonly made in (Protomaps, which the map's own basemap +// is, and OpenMapTiles). A file which also has other things often carries these too, and drawing +// them again over the basemap doubles every road and label. +const BASEMAP_LAYERS = new Set([ + // Protomaps + "earth", "water", "roads", "landuse", "landcover", "buildings", "boundaries", "places", "pois", + "transit", "natural", "physical_line", "physical_point", + // OpenMapTiles + "waterway", "water_name", "landuse", "landcover", "park", "boundary", "aeroway", "aerodrome_label", + "transportation", "transportation_name", "building", "place", "poi", "mountain_peak", +]); + +// Whether a layer of an overlay is of something the basemap already draws. +export function isBasemapLayer(layerId) { + return BASEMAP_LAYERS.has(String(layerId ?? "").toLowerCase()); +} + +// Once a file is on, each of its layers is as the user chose. A layer the user has not chosen for +// is drawn, unless the basemap already draws the same thing. export function isOverlayLayerEnabled(state, file, layerId) { - return state?.[file.map_path]?.layers?.[layerId] !== false; + const chosen = state?.[file.map_path]?.layers?.[layerId]; + return typeof chosen === "boolean" ? chosen : !isBasemapLayer(layerId); } export function setOverlayEnabled(state, file, enabled) { diff --git a/docker/help/wrolpi-help b/docker/help/wrolpi-help index 5e7771ad..a265ff87 160000 --- a/docker/help/wrolpi-help +++ b/docker/help/wrolpi-help @@ -1 +1 @@ -Subproject commit 5e7771adf7706380400fea22a86041e837bb7c41 +Subproject commit a265ff870edca69497aec7eaffcf7423956232f3 diff --git a/modules/map/static/js/overlays.js b/modules/map/static/js/overlays.js index 73f45766..05b47006 100644 --- a/modules/map/static/js/overlays.js +++ b/modules/map/static/js/overlays.js @@ -126,9 +126,28 @@ export function isOverlayEnabled(state, file) { return state?.[file.map_path]?.enabled === true; } -// Once a file is on, each of its layers is drawn until the user turns that layer off. +// The layers of the schemas a basemap is commonly made in (Protomaps, which the map's own basemap +// is, and OpenMapTiles). A file which also has other things often carries these too, and drawing +// them again over the basemap doubles every road and label. +const BASEMAP_LAYERS = new Set([ + // Protomaps + "earth", "water", "roads", "landuse", "landcover", "buildings", "boundaries", "places", "pois", + "transit", "natural", "physical_line", "physical_point", + // OpenMapTiles + "waterway", "water_name", "landuse", "landcover", "park", "boundary", "aeroway", "aerodrome_label", + "transportation", "transportation_name", "building", "place", "poi", "mountain_peak", +]); + +// Whether a layer of an overlay is of something the basemap already draws. +export function isBasemapLayer(layerId) { + return BASEMAP_LAYERS.has(String(layerId ?? "").toLowerCase()); +} + +// Once a file is on, each of its layers is as the user chose. A layer the user has not chosen for +// is drawn, unless the basemap already draws the same thing. export function isOverlayLayerEnabled(state, file, layerId) { - return state?.[file.map_path]?.layers?.[layerId] !== false; + const chosen = state?.[file.map_path]?.layers?.[layerId]; + return typeof chosen === "boolean" ? chosen : !isBasemapLayer(layerId); } export function setOverlayEnabled(state, file, enabled) {