@defra/interactive-map 0.0.52-alpha → 0.0.53-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/im-core.js +1 -1
- package/dist/umd/im-core.js +1 -1
- package/dist/umd/index.js +1 -1
- package/docs/api/context.md +18 -12
- package/docs/api/marker-config.md +16 -8
- package/docs/api/symbol-config.md +24 -9
- package/docs/api/symbol-registry.md +49 -14
- package/docs/plugins/datasets.md +4 -10
- package/docs/plugins/draw.md +2 -1
- package/docs/plugins/map-key.md +6 -4
- package/package.json +1 -1
- package/plugins/beta/map-styles/dist/esm/im-map-styles-plugin.js +1 -1
- package/plugins/beta/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
- package/plugins/beta/map-styles/dist/umd/index.js +1 -1
- package/plugins/datasets/dist/esm/im-datasets-ml-adapter.js +1 -1
- package/plugins/datasets/dist/esm/im-datasets-ol-adapter.js +1 -1
- package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
- package/plugins/datasets/dist/esm/mapboxStyleLayerAdapter.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
- package/plugins/datasets/src/adapters/maplibre/layerBuilders.js +9 -9
- package/plugins/datasets/src/adapters/maplibre/layerBuilders.test.js +19 -3
- package/plugins/datasets/src/adapters/maplibre/maplibreLayerAdapter.js +9 -3
- package/plugins/datasets/src/adapters/maplibre/maplibreLayerAdapter.test.js +18 -3
- package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.js +3 -3
- package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.test.js +19 -26
- package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.js +15 -67
- package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.test.js +32 -105
- package/plugins/datasets/src/adapters/openlayers/layerBuilders.js +11 -13
- package/plugins/datasets/src/adapters/openlayers/layerBuilders.test.js +11 -3
- package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.js +48 -19
- package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.test.js +99 -11
- package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.js +26 -26
- package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.test.js +48 -29
- package/plugins/datasets/src/initialise/defaults.js +4 -3
- package/plugins/datasets/src/reducers/__data__/demoDatasets.js +1 -3
- package/plugins/datasets/src/registry/dataset.js +2 -1
- package/plugins/datasets/src/registry/dataset.test.js +13 -0
- package/plugins/datasets/src/registry/mapboxStyleDataset.js +1 -1
- package/plugins/datasets/src/registry/mapboxStyleDataset.test.js +1 -1
- package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
- package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/esm/index.js +1 -1
- package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/umd/index.js +1 -1
- package/plugins/draw/src/DrawInit.jsx +7 -5
- package/plugins/draw/src/DrawInit.test.jsx +18 -2
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +11 -5
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -11
- package/plugins/draw/src/adapters/maplibre/mapboxDraw.js +12 -9
- package/plugins/draw/src/adapters/maplibre/mapboxDraw.test.js +18 -0
- package/plugins/draw/src/adapters/maplibre/pointSymbolImages.js +81 -56
- package/plugins/draw/src/adapters/maplibre/pointSymbolImages.test.js +149 -80
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +7 -6
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +22 -25
- package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.js +26 -8
- package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.test.js +58 -16
- package/plugins/draw/src/adapters/openlayers/core/styles.js +33 -20
- package/plugins/draw/src/adapters/openlayers/core/styles.test.js +47 -21
- package/plugins/draw/src/adapters/openlayers/olDraw.js +16 -12
- package/plugins/draw/src/adapters/openlayers/olDraw.test.js +27 -32
- package/plugins/draw/src/adapters/openlayers/point/pointSymbolImages.js +44 -49
- package/plugins/draw/src/adapters/openlayers/point/pointSymbolImages.test.js +100 -119
- package/plugins/draw/src/defaults.js +2 -6
- package/plugins/draw/src/utils/stripInternalProperties.test.js +2 -1
- package/plugins/draw/src/utils/symbolKeys.js +3 -4
- package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
- package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
- package/plugins/interact/dist/umd/index.js +1 -1
- package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
- package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -2
- package/plugins/map-key/dist/umd/index.js +1 -1
- package/plugins/map-key/src/components/Key/Key.jsx +8 -4
- package/plugins/map-key/src/components/Key/KeyGroupItem.jsx +3 -1
- package/plugins/map-key/src/components/Key/KeyItem.jsx +2 -2
- package/plugins/map-key/src/components/Key/KeySvg.jsx +15 -14
- package/plugins/map-key/src/components/Key/KeySvg.test.jsx +21 -14
- package/plugins/map-key/src/components/Key/KeySvgPattern.jsx +1 -2
- package/plugins/map-key/src/components/Key/KeySvgPattern.test.jsx +3 -1
- package/plugins/map-key/src/components/Key/KeySvgSymbol.jsx +26 -12
- package/plugins/map-key/src/components/Key/KeySvgSymbol.test.jsx +24 -74
- package/plugins/map-key/src/components/Key/MapKey.jsx +3 -1
- package/plugins/map-key/src/components/Key/svgProperties.js +8 -2
- package/plugins/map-key/src/reducers/mergeKeyGroupItems.test.js +15 -0
- package/plugins/map-key/src/registry/index.js +0 -2
- package/plugins/map-key/src/registry/index.test.js +1 -9
- package/plugins/menu/src/registry/index.js +0 -2
- package/plugins/menu/src/registry/index.test.js +1 -9
- package/providers/beta/esri/dist/esm/im-esri-provider.js +1 -1
- package/providers/beta/openlayers/dist/esm/im-openlayers-provider.js +1 -1
- package/providers/beta/openlayers/dist/umd/im-openlayers-provider.js +1 -1
- package/providers/beta/openlayers/dist/umd/index.js +1 -1
- package/providers/beta/openlayers/src/openlayersProvider.js +102 -2
- package/providers/beta/openlayers/src/openlayersProvider.test.js +95 -0
- package/providers/beta/openlayers/src/utils/highlightFeatures.js +20 -25
- package/providers/beta/openlayers/src/utils/highlightFeatures.test.js +73 -38
- package/providers/beta/openlayers/src/utils/patternImages.js +61 -0
- package/providers/beta/openlayers/src/utils/patternImages.test.js +74 -0
- package/providers/beta/openlayers/src/utils/symbolImages.js +103 -95
- package/providers/beta/openlayers/src/utils/symbolImages.test.js +136 -85
- package/providers/mapProvider.js +53 -1
- package/providers/mapProvider.test.js +6 -0
- package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
- package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
- package/providers/maplibre/dist/umd/index.js +1 -1
- package/providers/maplibre/src/maplibreProvider.js +50 -17
- package/providers/maplibre/src/maplibreProvider.test.js +74 -20
- package/providers/maplibre/src/utils/highlightFeatures.js +19 -14
- package/providers/maplibre/src/utils/highlightFeatures.test.js +20 -14
- package/providers/maplibre/src/utils/patternImages.js +46 -29
- package/providers/maplibre/src/utils/patternImages.test.js +52 -4
- package/providers/maplibre/src/utils/symbolImages.js +80 -39
- package/providers/maplibre/src/utils/symbolImages.test.js +157 -54
- package/src/App/components/Markers/Markers.jsx +16 -25
- package/src/App/components/Markers/Markers.test.jsx +34 -48
- package/src/App/components/Markers/SymbolLabelMarker.jsx +1 -1
- package/src/App/components/Markers/SymbolMarker.jsx +1 -1
- package/src/App/store/ServiceProvider.jsx +6 -3
- package/src/App/store/ServiceProvider.test.jsx +24 -0
- package/src/config/appConfig.js +1 -5
- package/src/config/mapSizeScales.js +8 -0
- package/src/config/symbolConfig.js +101 -25
- package/src/services/patternRegistry.js +97 -76
- package/src/services/patternRegistry.test.js +19 -1
- package/src/services/symbolComposer.js +184 -0
- package/src/services/symbolComposer.test.js +132 -0
- package/src/services/symbolRegistry.js +326 -155
- package/src/services/symbolRegistry.test.js +334 -32
- package/src/types.js +9 -3
- package/src/utils/hashString.js +23 -0
- package/src/utils/hashString.test.js +29 -0
- package/src/utils/imageDataCache.js +43 -0
- package/src/utils/imageDataCache.test.js +38 -0
- package/src/utils/pathBounds.js +330 -0
- package/src/utils/pathBounds.test.js +145 -0
- package/src/utils/patternUtils.js +0 -8
- package/src/utils/patternUtils.test.js +0 -25
- package/src/utils/recentImageSets.js +35 -0
- package/src/utils/recentImageSets.test.js +46 -0
- package/src/utils/symbolUtils.js +26 -29
- package/src/utils/symbolUtils.test.js +27 -41
- package/plugins/map-key/dist/umd/im-map-key-plugin.js.LICENSE.txt +0 -1
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import { RecentImageSets } from '../../../../src/utils/recentImageSets.js'
|
|
2
|
+
|
|
1
3
|
const ANCHOR_LOW = 0.25
|
|
2
4
|
const ANCHOR_HIGH = 0.75
|
|
5
|
+
const ANCHOR_CENTRE = 0.5
|
|
6
|
+
const HUNDREDTHS = 100
|
|
3
7
|
|
|
4
8
|
// ─── MapLibre-specific anchor conversion ──────────────────────────────────────
|
|
5
9
|
|
|
@@ -39,10 +43,10 @@ export const anchorToMaplibre = ([ax, ay]) => {
|
|
|
39
43
|
// The discrete fraction (0, 0.5 or 1) icon-anchor actually renders a given axis at — the
|
|
40
44
|
// same left/right/top/bottom/center snapping xAnchor/yAnchor above already do, expressed as
|
|
41
45
|
// a number instead of a string so it can be compared against the true, unsnapped fraction.
|
|
42
|
-
const discreteFraction = (
|
|
43
|
-
if (
|
|
44
|
-
if (
|
|
45
|
-
return
|
|
46
|
+
const discreteFraction = (fraction) => {
|
|
47
|
+
if (fraction <= ANCHOR_LOW) { return 0 }
|
|
48
|
+
if (fraction >= ANCHOR_HIGH) { return 1 }
|
|
49
|
+
return ANCHOR_CENTRE
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
/**
|
|
@@ -61,7 +65,7 @@ const discreteFraction = (a) => {
|
|
|
61
65
|
*/
|
|
62
66
|
// Rounded to 2dp: sub-hundredth-of-a-pixel precision is meaningless for rendering, and
|
|
63
67
|
// without it fractions like 0.8 produce float noise (8.799999999999997, not 8.8).
|
|
64
|
-
const round2dp = (
|
|
68
|
+
const round2dp = (value) => Math.round(value * HUNDREDTHS) / HUNDREDTHS
|
|
65
69
|
|
|
66
70
|
export const anchorToMaplibreOffset = ([ax, ay], viewBox) => {
|
|
67
71
|
const [,, width, height] = viewBox.split(' ').map(Number)
|
|
@@ -72,48 +76,85 @@ export const anchorToMaplibreOffset = ([ax, ay], viewBox) => {
|
|
|
72
76
|
}
|
|
73
77
|
|
|
74
78
|
/**
|
|
75
|
-
*
|
|
76
|
-
* Skips images that are already registered (safe to call on style change).
|
|
77
|
-
* Merges into `map._activeSymbolImageMap` (normal→active) and `map._selectedSymbolImageMap`
|
|
78
|
-
* (normal→selected) rather than replacing them — more than one caller (e.g. the datasets
|
|
79
|
-
* and draw plugins) can register symbols on the same map, and each call must add to that
|
|
80
|
-
* shared registry, not wipe out whatever another caller already registered there.
|
|
79
|
+
* The icon-anchor and icon-offset layout properties that place a symbol image on its point.
|
|
81
80
|
*
|
|
82
|
-
* @param {Object}
|
|
83
|
-
* @
|
|
84
|
-
* @param {Object} mapStyle - Current map style config (provides id, selectedColor, haloColor)
|
|
85
|
-
* @param {Object} symbolRegistry
|
|
86
|
-
* @param {number} [pixelRatio=2] - Device pixel ratio × map size scale factor (computed by caller)
|
|
87
|
-
* @returns {Promise<void>}
|
|
81
|
+
* @param {Object} symbolDef - a sized symbol definition (symbolRegistry.getSymbolDef)
|
|
82
|
+
* @returns {{ 'icon-anchor': string, 'icon-offset': number[] }}
|
|
88
83
|
*/
|
|
89
|
-
export const
|
|
90
|
-
|
|
91
|
-
|
|
84
|
+
export const getSymbolIconLayout = ({ anchor, viewBox }) => ({
|
|
85
|
+
'icon-anchor': anchorToMaplibre(anchor),
|
|
86
|
+
'icon-offset': anchorToMaplibreOffset(anchor, viewBox)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The MapLibre provider's record of its registered symbol images. The images themselves live in
|
|
91
|
+
* the map's own image store (map.addImage); this keeps which active (both rings) and selected
|
|
92
|
+
* (black ring) image belongs to each normal one. One per map, shared by every caller that
|
|
93
|
+
* registers symbols on it (e.g. the datasets and draw plugins).
|
|
94
|
+
*
|
|
95
|
+
* Images are kept for the current and previous map size and style; ones only older sizes and
|
|
96
|
+
* styles used are removed from the map, so they don't build up as the map size or style changes.
|
|
97
|
+
*/
|
|
98
|
+
export class SymbolImageVariants {
|
|
99
|
+
activeImageIds = new Map() // normal imageId → active imageId
|
|
100
|
+
selectedImageIds = new Map() // normal imageId → selected imageId
|
|
101
|
+
recentImageSets = new RecentImageSets()
|
|
102
|
+
|
|
103
|
+
/** @param {Object} map - MapLibre map instance */
|
|
104
|
+
constructor (map) {
|
|
105
|
+
this.map = map
|
|
92
106
|
}
|
|
93
107
|
|
|
94
|
-
|
|
95
|
-
|
|
108
|
+
/** A symbol's active (keyboard cursor) variant, from its normal imageId */
|
|
109
|
+
getActiveImageId (normalId) {
|
|
110
|
+
return this.activeImageIds.get(normalId) ?? null
|
|
111
|
+
}
|
|
96
112
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
113
|
+
/** A symbol's selected variant, from its normal imageId */
|
|
114
|
+
getSelectedImageId (normalId) {
|
|
115
|
+
return this.selectedImageIds.get(normalId) ?? null
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
removeImage (imageId) {
|
|
119
|
+
if (this.map.hasImage(imageId)) {
|
|
120
|
+
this.map.removeImage(imageId)
|
|
102
121
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
122
|
+
this.activeImageIds.delete(imageId)
|
|
123
|
+
this.selectedImageIds.delete(imageId)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Rasterises and adds each symbol's normal, active and selected images to the map, and maps
|
|
128
|
+
* its normal imageId to the other two. Images the map already has are reused. Then removes
|
|
129
|
+
* images only map sizes and styles older than the previous one used.
|
|
130
|
+
*
|
|
131
|
+
* @param {Object[]} styleArray - an array of symbol configs
|
|
132
|
+
* @param {Object} mapStyle - Current map style config (provides id, selectedColor, haloColor)
|
|
133
|
+
* @param {Object} symbolRegistry
|
|
134
|
+
* @param {number} pixelRatio - Device pixel ratio × map size scale factor
|
|
135
|
+
* @returns {Promise<void>}
|
|
136
|
+
*/
|
|
137
|
+
async registerSymbols (styleArray, mapStyle, symbolRegistry, pixelRatio) {
|
|
138
|
+
const { map } = this
|
|
139
|
+
const imageIds = await Promise.all(styleArray.map(async (config) => {
|
|
140
|
+
const variantIds = symbolRegistry.getSymbolImageIds(config, mapStyle, pixelRatio)
|
|
141
|
+
if (!variantIds) {
|
|
142
|
+
return []
|
|
107
143
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
144
|
+
this.activeImageIds.set(variantIds.normal, variantIds.active)
|
|
145
|
+
this.selectedImageIds.set(variantIds.normal, variantIds.selected)
|
|
146
|
+
await Promise.all(Object.entries(variantIds).map(async ([variant, imageId]) => {
|
|
147
|
+
if (map.hasImage(imageId)) {
|
|
148
|
+
return
|
|
112
149
|
}
|
|
113
|
-
|
|
150
|
+
const result = await symbolRegistry.rasteriseSymbolImage(config, mapStyle, variant, pixelRatio)
|
|
151
|
+
if (result && !map.hasImage(result.imageId)) {
|
|
114
152
|
map.addImage(result.imageId, result.imageData, { pixelRatio })
|
|
115
153
|
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
154
|
+
}))
|
|
155
|
+
return Object.values(variantIds)
|
|
156
|
+
}))
|
|
157
|
+
this.recentImageSets.add(`${mapStyle?.id}|${pixelRatio}`, imageIds.flat())
|
|
158
|
+
.forEach((imageId) => this.removeImage(imageId))
|
|
159
|
+
}
|
|
119
160
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { anchorToMaplibre, anchorToMaplibreOffset,
|
|
2
|
-
import {
|
|
1
|
+
import { anchorToMaplibre, anchorToMaplibreOffset, getSymbolIconLayout, SymbolImageVariants } from './symbolImages.js'
|
|
2
|
+
import { createSymbolRegistry } from '../../../../src/services/symbolRegistry.js'
|
|
3
|
+
|
|
4
|
+
const symbolRegistry = createSymbolRegistry()
|
|
3
5
|
|
|
4
6
|
beforeAll(() => {
|
|
5
7
|
globalThis.URL.createObjectURL = jest.fn(() => 'blob:mock')
|
|
@@ -114,104 +116,123 @@ describe('anchorToMaplibreOffset', () => {
|
|
|
114
116
|
})
|
|
115
117
|
})
|
|
116
118
|
|
|
117
|
-
// ───
|
|
119
|
+
// ─── getSymbolIconLayout ──────────────────────────────────────────────────────
|
|
120
|
+
|
|
121
|
+
describe('getSymbolIconLayout', () => {
|
|
122
|
+
it('snaps to the nearest icon-anchor and offsets the rest of the way', () => {
|
|
123
|
+
expect(getSymbolIconLayout({ anchor: [0.5, 0.9], viewBox: '0 0 44 44' })).toEqual({ 'icon-anchor': 'bottom', 'icon-offset': [0, 4.4] })
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('places a sized built-in symbol on its own anchor point', () => {
|
|
127
|
+
const symbolDef = symbolRegistry.getSymbolDef({ symbol: 'pin' })
|
|
128
|
+
const [, , width, height] = symbolDef.viewBox.split(' ').map(Number)
|
|
129
|
+
const { 'icon-offset': [offsetX, offsetY] } = getSymbolIconLayout(symbolDef)
|
|
130
|
+
// icon-offset moves the image, so the point lands that far back from the snapped edge
|
|
131
|
+
expect(width / 2 - offsetX).toBeCloseTo(symbolDef.anchor[0] * width)
|
|
132
|
+
expect(height - offsetY).toBeCloseTo(symbolDef.anchor[1] * height)
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
// ─── SymbolImageVariants ──────────────────────────────────────────────────────
|
|
118
137
|
|
|
119
138
|
const makeMap = (existingIds = []) => ({
|
|
120
|
-
_activeSymbolImageMap: {},
|
|
121
|
-
_selectedSymbolImageMap: {},
|
|
122
139
|
hasImage: jest.fn((id) => existingIds.includes(id)),
|
|
123
140
|
addImage: jest.fn()
|
|
124
141
|
})
|
|
125
142
|
const STYLE_ID = 'test'
|
|
126
143
|
const mapStyle = { id: STYLE_ID }
|
|
144
|
+
const PIXEL_RATIO = 2
|
|
127
145
|
|
|
128
|
-
|
|
129
|
-
|
|
146
|
+
// Registers the configs with a fresh SymbolImageVariants for the map, and returns it
|
|
147
|
+
const register = async (map, configs, pixelRatio = PIXEL_RATIO, symbolImages = new SymbolImageVariants(map)) => {
|
|
148
|
+
await symbolImages.registerSymbols(configs, mapStyle, symbolRegistry, pixelRatio)
|
|
149
|
+
return symbolImages
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
describe('SymbolImageVariants — registration', () => {
|
|
153
|
+
it('does not touch the map for empty configs', async () => {
|
|
130
154
|
const map = makeMap()
|
|
131
|
-
await
|
|
155
|
+
await register(map, [])
|
|
132
156
|
expect(map.hasImage).not.toHaveBeenCalled()
|
|
133
157
|
expect(map.addImage).not.toHaveBeenCalled()
|
|
134
158
|
})
|
|
135
159
|
|
|
136
|
-
it('
|
|
137
|
-
// More than one caller (datasets, draw, ...)
|
|
138
|
-
// a later call must add to the shared registry, not wipe an earlier caller's entries.
|
|
160
|
+
it('adds to the variants already registered rather than replacing them', async () => {
|
|
161
|
+
// More than one caller (datasets, draw, ...) registers symbols on the same map
|
|
139
162
|
const map = makeMap()
|
|
140
|
-
|
|
141
|
-
map
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
expect(
|
|
145
|
-
|
|
146
|
-
expect(
|
|
163
|
+
const symbolImages = await register(map, [{ symbol: 'circle' }])
|
|
164
|
+
await register(map, [{ symbol: 'pin' }], PIXEL_RATIO, symbolImages)
|
|
165
|
+
const circleId = symbolRegistry.getSymbolImageId({ symbol: 'circle' }, mapStyle, 'normal', PIXEL_RATIO)
|
|
166
|
+
const pinId = symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, 'normal', PIXEL_RATIO)
|
|
167
|
+
expect(symbolImages.getActiveImageId(circleId)).not.toBeNull()
|
|
168
|
+
expect(symbolImages.getSelectedImageId(circleId)).not.toBeNull()
|
|
169
|
+
expect(symbolImages.getActiveImageId(pinId)).not.toBeNull()
|
|
147
170
|
})
|
|
148
171
|
|
|
149
172
|
it('calls addImage for normal, active and selected variants', async () => {
|
|
150
173
|
const map = makeMap()
|
|
151
|
-
await
|
|
174
|
+
await register(map, [{ symbol: 'pin' }])
|
|
152
175
|
expect(map.addImage).toHaveBeenCalledTimes(3) // NOSONAR S109 — normal, active, selected
|
|
153
176
|
expect(map.addImage).toHaveBeenCalledWith(expect.stringMatching(/^symbol-[a-z0-9]+-\d+(\.\d+)?x$/), expect.any(Object), { pixelRatio: 2 })
|
|
154
177
|
expect(map.addImage).toHaveBeenCalledWith(expect.stringMatching(/^symbol-act-[a-z0-9]+-\d+(\.\d+)?x$/), expect.any(Object), { pixelRatio: 2 })
|
|
155
178
|
expect(map.addImage).toHaveBeenCalledWith(expect.stringMatching(/^symbol-sel-[a-z0-9]+-\d+(\.\d+)?x$/), expect.any(Object), { pixelRatio: 2 })
|
|
156
179
|
})
|
|
157
180
|
|
|
158
|
-
it('
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
const
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
expect(map._activeSymbolImageMap[normalId]).toBe(activeId)
|
|
181
|
+
it('maps each normal image id to its active and selected variants', async () => {
|
|
182
|
+
const symbolImages = await register(makeMap(), [{ symbol: 'pin' }])
|
|
183
|
+
const normalId = symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, 'normal', PIXEL_RATIO)
|
|
184
|
+
const activeId = symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, 'active', PIXEL_RATIO)
|
|
185
|
+
const selectedId = symbolImages.getSelectedImageId(normalId)
|
|
186
|
+
expect(symbolImages.getActiveImageId(normalId)).toBe(activeId)
|
|
165
187
|
expect(selectedId).toMatch(/^symbol-sel-[a-z0-9]+-\d+(\.\d+)?x$/)
|
|
166
188
|
})
|
|
167
189
|
|
|
168
190
|
it('skips addImage when all three variant images are already registered', async () => {
|
|
169
191
|
// Run once to discover the selected image ID (not derivable without rasterising)
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
const
|
|
173
|
-
const
|
|
174
|
-
const selectedId = setupMap._selectedSymbolImageMap[normalId]
|
|
192
|
+
const setup = await register(makeMap(), [{ symbol: 'circle' }])
|
|
193
|
+
const normalId = symbolRegistry.getSymbolImageId({ symbol: 'circle' }, mapStyle, 'normal', PIXEL_RATIO)
|
|
194
|
+
const activeId = symbolRegistry.getSymbolImageId({ symbol: 'circle' }, mapStyle, 'active', PIXEL_RATIO)
|
|
195
|
+
const selectedId = setup.getSelectedImageId(normalId)
|
|
175
196
|
|
|
176
197
|
const map = makeMap([normalId, activeId, selectedId])
|
|
177
|
-
await
|
|
198
|
+
await register(map, [{ symbol: 'circle' }])
|
|
178
199
|
expect(map.addImage).not.toHaveBeenCalled()
|
|
179
200
|
})
|
|
180
201
|
|
|
181
202
|
it('processes multiple configs independently', async () => {
|
|
182
203
|
const map = makeMap()
|
|
183
|
-
await
|
|
204
|
+
const symbolImages = await register(map, [{ symbol: 'pin' }, { symbol: 'circle' }])
|
|
184
205
|
expect(map.addImage).toHaveBeenCalledTimes(6) // NOSONAR S109 — 2 configs × 3 variants each
|
|
185
|
-
expect(
|
|
186
|
-
expect(
|
|
206
|
+
expect(symbolImages.activeImageIds.size).toBe(2)
|
|
207
|
+
expect(symbolImages.selectedImageIds.size).toBe(2)
|
|
187
208
|
})
|
|
188
209
|
})
|
|
189
210
|
|
|
190
|
-
describe('
|
|
211
|
+
describe('SymbolImageVariants — null results and caching', () => {
|
|
191
212
|
it('does not call addImage when rasteriseSymbolImage returns null', async () => {
|
|
192
|
-
|
|
193
|
-
// but rasteriseSymbolImage must get undefined from getSymbolDef so it returns null.
|
|
194
|
-
// The registry.get call order: [1] getSymbolImageId normal, [2] getSymbolImageId active,
|
|
195
|
-
// [3] rasteriseSymbolImage normal, [4] rasteriseSymbolImage active, [5] rasteriseSymbolImage selected.
|
|
196
|
-
const pinDef = symbolRegistry.get('pin')
|
|
197
|
-
const getSpy = jest.spyOn(symbolRegistry, 'get')
|
|
198
|
-
.mockReturnValueOnce(pinDef)
|
|
199
|
-
.mockReturnValueOnce(pinDef)
|
|
200
|
-
.mockReturnValueOnce(undefined)
|
|
201
|
-
.mockReturnValueOnce(undefined)
|
|
202
|
-
.mockReturnValueOnce(undefined)
|
|
213
|
+
const rasterise = jest.spyOn(symbolRegistry, 'rasteriseSymbolImage').mockResolvedValue(null)
|
|
203
214
|
const map = makeMap()
|
|
204
|
-
await
|
|
215
|
+
await register(map, [{ symbol: 'pin' }])
|
|
205
216
|
expect(map.addImage).not.toHaveBeenCalled()
|
|
206
|
-
|
|
217
|
+
rasterise.mockRestore()
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
it('rasterises nothing for a symbol the map already has every image of', async () => {
|
|
221
|
+
const map = makeMap()
|
|
222
|
+
const symbolImages = await register(map, [{ symbol: 'pin' }])
|
|
223
|
+
const rasterise = jest.spyOn(symbolRegistry, 'rasteriseSymbolImage')
|
|
224
|
+
map.hasImage.mockReturnValue(true)
|
|
225
|
+
await register(map, [{ symbol: 'pin' }], PIXEL_RATIO, symbolImages)
|
|
226
|
+
expect(rasterise).not.toHaveBeenCalled()
|
|
227
|
+
rasterise.mockRestore()
|
|
207
228
|
})
|
|
208
229
|
|
|
209
230
|
it('skips config when symbolDef cannot be resolved', async () => {
|
|
210
231
|
const map = makeMap()
|
|
211
|
-
await
|
|
232
|
+
const symbolImages = await register(map, [{ symbol: 'no-such-symbol' }])
|
|
212
233
|
expect(map.addImage).not.toHaveBeenCalled()
|
|
213
|
-
expect(
|
|
214
|
-
expect(
|
|
234
|
+
expect(symbolImages.activeImageIds.size).toBe(0)
|
|
235
|
+
expect(symbolImages.selectedImageIds.size).toBe(0)
|
|
215
236
|
})
|
|
216
237
|
|
|
217
238
|
it('reuses cached imageData when called again with the same pixelRatio', async () => {
|
|
@@ -220,14 +241,14 @@ describe('addSymbolsToMap — null results and caching', () => {
|
|
|
220
241
|
|
|
221
242
|
const map1 = makeMap()
|
|
222
243
|
const getContextCallsBefore = HTMLCanvasElement.prototype.getContext.mock.calls.length
|
|
223
|
-
await
|
|
244
|
+
await register(map1, [{ symbol: 'pin' }], uniqueRatio)
|
|
224
245
|
const getContextCallsAfterFirst = HTMLCanvasElement.prototype.getContext.mock.calls.length
|
|
225
246
|
// Rasterisation ran — canvas was used
|
|
226
247
|
expect(getContextCallsAfterFirst).toBeGreaterThan(getContextCallsBefore)
|
|
227
248
|
|
|
228
249
|
// Second call with a fresh map (hasImage → false) but same ratio → cache hit
|
|
229
250
|
const map2 = makeMap()
|
|
230
|
-
await
|
|
251
|
+
await register(map2, [{ symbol: 'pin' }], uniqueRatio)
|
|
231
252
|
const getContextCallsAfterSecond = HTMLCanvasElement.prototype.getContext.mock.calls.length
|
|
232
253
|
// No new canvas — rasterisation was skipped via cache
|
|
233
254
|
expect(getContextCallsAfterSecond).toBe(getContextCallsAfterFirst)
|
|
@@ -235,3 +256,85 @@ describe('addSymbolsToMap — null results and caching', () => {
|
|
|
235
256
|
expect(map2.addImage).toHaveBeenCalledTimes(3) // NOSONAR S109 — normal, active, selected
|
|
236
257
|
})
|
|
237
258
|
})
|
|
259
|
+
|
|
260
|
+
describe('SymbolImageVariants — removing images from older map sizes and styles', () => {
|
|
261
|
+
// A map that keeps the images added to it, like MapLibre's image store
|
|
262
|
+
const makeImageStore = () => {
|
|
263
|
+
const images = new Set()
|
|
264
|
+
return {
|
|
265
|
+
images,
|
|
266
|
+
hasImage: jest.fn((id) => images.has(id)),
|
|
267
|
+
addImage: jest.fn((id) => images.add(id)),
|
|
268
|
+
removeImage: jest.fn((id) => images.delete(id))
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
const pinIds = (symbolImages, pixelRatio) => {
|
|
272
|
+
const normalId = symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, 'normal', pixelRatio)
|
|
273
|
+
return [normalId, symbolRegistry.getSymbolImageId({ symbol: 'pin' }, mapStyle, 'active', pixelRatio), symbolImages.getSelectedImageId(normalId)]
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
it('keeps the current and previous size, and removes an older one\'s images when a third arrives', async () => {
|
|
277
|
+
const map = makeImageStore()
|
|
278
|
+
const symbolImages = new SymbolImageVariants(map)
|
|
279
|
+
await register(map, [{ symbol: 'pin' }], 1, symbolImages)
|
|
280
|
+
const oldIds = pinIds(symbolImages, 1)
|
|
281
|
+
await register(map, [{ symbol: 'pin' }], 2, symbolImages)
|
|
282
|
+
oldIds.forEach((imageId) => expect(map.images.has(imageId)).toBe(true))
|
|
283
|
+
|
|
284
|
+
await register(map, [{ symbol: 'pin' }], 3, symbolImages)
|
|
285
|
+
oldIds.forEach((imageId) => expect(map.images.has(imageId)).toBe(false))
|
|
286
|
+
expect(symbolImages.getActiveImageId(oldIds[0])).toBeNull()
|
|
287
|
+
expect(symbolImages.getSelectedImageId(oldIds[0])).toBeNull()
|
|
288
|
+
pinIds(symbolImages, 2).concat(pinIds(symbolImages, 3)).forEach((imageId) => expect(map.images.has(imageId)).toBe(true))
|
|
289
|
+
expect(map.images.size).toBe(6) // NOSONAR S109 — two sizes × normal, active, selected
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
it('keeps an image the map no longer has, e.g. after a style change, without removing it', async () => {
|
|
293
|
+
const map = makeImageStore()
|
|
294
|
+
const symbolImages = new SymbolImageVariants(map)
|
|
295
|
+
await register(map, [{ symbol: 'pin' }], 1, symbolImages)
|
|
296
|
+
map.images.clear()
|
|
297
|
+
await register(map, [{ symbol: 'pin' }], 2, symbolImages)
|
|
298
|
+
await register(map, [{ symbol: 'pin' }], 3, symbolImages)
|
|
299
|
+
expect(map.removeImage).not.toHaveBeenCalled()
|
|
300
|
+
})
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
// End to end across the symbol pipeline: whatever the shape, size or anchor override, MapLibre's
|
|
304
|
+
// snapped icon-anchor plus the icon-offset must land the symbol's true anchor pixel on the
|
|
305
|
+
// coordinate — the same pixel HTML markers and OpenLayers place directly from the fraction.
|
|
306
|
+
describe('anchor placement across symbols, sizes and overrides', () => {
|
|
307
|
+
const ANCHOR_POSITIONS = {
|
|
308
|
+
left: 0, right: 1, top: 0, bottom: 1, center: 0.5
|
|
309
|
+
}
|
|
310
|
+
// icon-anchor names the point of the image placed on the coordinate, as fractions
|
|
311
|
+
const discretePoint = (name) => {
|
|
312
|
+
if (name === 'center') { return [0.5, 0.5] }
|
|
313
|
+
const parts = name.split('-')
|
|
314
|
+
const x = parts.find((p) => p === 'left' || p === 'right')
|
|
315
|
+
const y = parts.find((p) => p === 'top' || p === 'bottom')
|
|
316
|
+
return [x ? ANCHOR_POSITIONS[x] : 0.5, y ? ANCHOR_POSITIONS[y] : 0.5]
|
|
317
|
+
}
|
|
318
|
+
const styles = [
|
|
319
|
+
...['pin', 'circle', 'square', 'hexagon', 'triangle', 'diamond'].map((symbol) => ({ symbol })),
|
|
320
|
+
{ symbolSvgContent: '<rect width="30" height="20"/>', symbolViewBox: '0 0 30 20', symbolAnchor: [0.3, 0.9] }
|
|
321
|
+
]
|
|
322
|
+
const overrides = [undefined, [0, 0], [0.5, 1], [1, 0.25]]
|
|
323
|
+
|
|
324
|
+
styles.forEach((baseStyle) => {
|
|
325
|
+
['small', 'medium', 'large'].forEach((symbolSize) => {
|
|
326
|
+
overrides.forEach((symbolAnchor) => {
|
|
327
|
+
const style = { ...baseStyle, symbolSize, ...(symbolAnchor && { symbolAnchor }) }
|
|
328
|
+
it(`${baseStyle.symbol ?? 'custom svg'} ${symbolSize} anchor ${JSON.stringify(symbolAnchor ?? 'default')}`, () => {
|
|
329
|
+
const { viewBox, anchor } = symbolRegistry.getSymbolDef(style)
|
|
330
|
+
const [,, width, height] = viewBox.split(' ').map(Number)
|
|
331
|
+
const [dx, dy] = discretePoint(anchorToMaplibre(anchor))
|
|
332
|
+
const [ox, oy] = anchorToMaplibreOffset(anchor, viewBox)
|
|
333
|
+
// the discrete anchor point, shifted by the offset, is the true anchor pixel (offset is 2dp)
|
|
334
|
+
expect(dx * width - ox).toBeCloseTo(anchor[0] * width, 1)
|
|
335
|
+
expect(dy * height - oy).toBeCloseTo(anchor[1] * height, 1)
|
|
336
|
+
})
|
|
337
|
+
})
|
|
338
|
+
})
|
|
339
|
+
})
|
|
340
|
+
})
|
|
@@ -6,43 +6,34 @@ import { useMap } from '../../store/mapContext.js'
|
|
|
6
6
|
import { useService } from '../../store/serviceContext.js'
|
|
7
7
|
import { scaleFactor } from '../../../config/appConfig.js'
|
|
8
8
|
import { isStandaloneLabel } from '../../../utils/symbolUtils.js'
|
|
9
|
+
import { DEFAULT_SYMBOL_ANCHOR, DEFAULT_SYMBOL_VIEWBOX } from '../../../config/symbolConfig.js'
|
|
9
10
|
import { EVENTS } from '../../../config/events.js'
|
|
10
11
|
import LabelMarker from './LabelMarker.jsx'
|
|
11
12
|
import SymbolLabelMarker from './SymbolLabelMarker.jsx'
|
|
12
13
|
import SymbolMarker from './SymbolMarker.jsx'
|
|
13
14
|
|
|
14
15
|
// Marker properties handled internally — excluded from style value resolution
|
|
15
|
-
const INTERNAL_KEYS = new Set(['id', 'coords', 'x', 'y', 'isVisible', 'symbol', '
|
|
16
|
+
const INTERNAL_KEYS = new Set(['id', 'coords', 'x', 'y', 'isVisible', 'symbol', 'svgContent', 'size', 'viewBox', 'anchor', 'selectedColor', 'label', 'showLabel'])
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
// The keyboard cursor's rings take priority over the selected ring
|
|
19
|
+
const markerVariant = (isSelected, isActive) => {
|
|
20
|
+
if (isActive) {
|
|
21
|
+
return 'active'
|
|
22
|
+
}
|
|
23
|
+
return isSelected ? 'selected' : 'normal'
|
|
22
24
|
}
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
marker.anchor ?? defaults.anchor ?? symbolDef?.anchor ?? [0.5, 0.5]
|
|
29
|
-
|
|
30
|
-
const resolveSymbolProps = (marker, defaults, symbolRegistry, mapStyle, mapSize, isSelected, isActive) => {
|
|
31
|
-
const symbolDef = resolveSymbolDef(marker, defaults, symbolRegistry)
|
|
26
|
+
// The marker's definition carries the viewBox and anchor for its size (see
|
|
27
|
+
// symbolRegistry.getMarkerSymbolDef)
|
|
28
|
+
const resolveSymbolProps = (marker, defaults, symbolRegistry, mapStyle, mapSize, variant) => {
|
|
29
|
+
const symbolDef = symbolRegistry.getMarkerSymbolDef(marker)
|
|
32
30
|
const styleValues = Object.fromEntries(
|
|
33
31
|
Object.entries(marker).filter(([k]) => !INTERNAL_KEYS.has(k))
|
|
34
32
|
)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
resolvedSvg = symbolRegistry.resolveActive(symbolDef, styleValues, mapStyle)
|
|
38
|
-
} else if (isSelected) {
|
|
39
|
-
resolvedSvg = symbolRegistry.resolveSelected(symbolDef, styleValues, mapStyle)
|
|
40
|
-
} else {
|
|
41
|
-
resolvedSvg = symbolRegistry.resolve(symbolDef, styleValues, mapStyle)
|
|
42
|
-
}
|
|
43
|
-
const viewBox = resolveViewBox(marker, defaults, symbolDef)
|
|
33
|
+
const resolvedSvg = symbolRegistry.resolveVariant(symbolDef, styleValues, mapStyle, variant)
|
|
34
|
+
const viewBox = symbolDef?.viewBox ?? DEFAULT_SYMBOL_VIEWBOX
|
|
44
35
|
const [,, svgWidth, svgHeight] = viewBox.split(' ').map(Number)
|
|
45
|
-
const anchor =
|
|
36
|
+
const anchor = symbolDef?.anchor ?? DEFAULT_SYMBOL_ANCHOR
|
|
46
37
|
const shapeId = marker.symbol || defaults.symbol
|
|
47
38
|
const scale = scaleFactor[mapSize] ?? 1
|
|
48
39
|
return { resolvedSvg, viewBox, anchor, shapeId, scaledWidth: svgWidth * scale, scaledHeight: svgHeight * scale }
|
|
@@ -98,7 +89,7 @@ export const Markers = () => {
|
|
|
98
89
|
return <LabelMarker key={marker.id} marker={marker} mapId={id} markerRef={markerRef} />
|
|
99
90
|
}
|
|
100
91
|
|
|
101
|
-
const symbolProps = resolveSymbolProps(marker, defaults, symbolRegistry, mapStyle, mapSize, isSelected, isActive)
|
|
92
|
+
const symbolProps = resolveSymbolProps(marker, defaults, symbolRegistry, mapStyle, mapSize, markerVariant(isSelected, isActive))
|
|
102
93
|
|
|
103
94
|
if (marker.showLabel && marker.label) {
|
|
104
95
|
return <SymbolLabelMarker key={marker.id} marker={marker} mapId={id} markerRef={markerRef} isSelected={isSelected} symbolProps={symbolProps} />
|