@defra/interactive-map 0.0.52-alpha → 0.0.54-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/css/index.css +1 -1
- package/dist/esm/im-core.js +1 -1
- package/dist/esm/im-shell.js +1 -1
- package/dist/umd/im-core.js +1 -1
- package/dist/umd/index.js +1 -1
- package/docs/api/context.md +30 -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/api.md +11 -3
- package/docs/plugins/datasets.md +6 -10
- package/docs/plugins/draw.md +2 -1
- package/docs/plugins/map-key.md +47 -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/esri/esriLayerAdapter.js +6 -6
- package/plugins/datasets/src/adapters/esri/registry/esriDataset.test.js +1 -1
- package/plugins/datasets/src/adapters/maplibre/layerBuilders.js +10 -10
- package/plugins/datasets/src/adapters/maplibre/layerBuilders.test.js +22 -6
- 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 +20 -27
- package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.js +16 -70
- package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.test.js +30 -115
- 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 +101 -13
- package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.js +27 -28
- package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.test.js +51 -32
- package/plugins/datasets/src/fetch/createDynamicSource.js +2 -2
- package/plugins/datasets/src/initialise/DatasetsInit.jsx +2 -2
- package/plugins/datasets/src/initialise/defaults.js +16 -27
- package/plugins/datasets/src/initialise/defaults.test.js +25 -57
- package/plugins/datasets/src/initialise/initialiseDatasets.js +1 -4
- package/plugins/datasets/src/reducers/__data__/demoDatasets.js +1 -3
- package/plugins/datasets/src/reducers/mappedDatasetsReducer.test.js +1 -1
- package/plugins/datasets/src/registry/dataset.js +23 -17
- package/plugins/datasets/src/registry/dataset.test.js +114 -11
- 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/css/index.css +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/Key.module.scss +144 -53
- package/plugins/map-key/src/components/Key/KeyGroupItem.jsx +36 -3
- package/plugins/map-key/src/components/Key/KeyGroupItem.test.jsx +44 -2
- package/plugins/map-key/src/components/Key/KeyItem.jsx +2 -2
- package/plugins/map-key/src/components/Key/KeySvg.jsx +19 -18
- package/plugins/map-key/src/components/Key/KeySvg.test.jsx +25 -18
- 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/KeySvgRamp.jsx +35 -6
- package/plugins/map-key/src/components/Key/KeySvgRamp.test.jsx +56 -16
- 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/components/Key/useRampOrientation.js +61 -0
- package/plugins/map-key/src/components/Key/useRampOrientation.test.jsx +155 -0
- package/plugins/map-key/src/index.js +34 -0
- 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/map-key/src/utils/groupStyles.js +5 -2
- package/plugins/menu/src/registry/index.js +0 -2
- package/plugins/menu/src/registry/index.test.js +1 -9
- package/plugins/search/dist/esm/im-search-plugin.js +1 -1
- package/plugins/search/dist/umd/im-search-plugin.js +1 -1
- package/plugins/search/dist/umd/index.js +1 -1
- package/plugins/search/src/Search.jsx +0 -1
- package/plugins/search/src/components/Form/Form.jsx +3 -3
- package/plugins/search/src/components/Suggestions/Suggestions.jsx +2 -0
- package/plugins/search/src/components/Suggestions/Suggestions.test.jsx +6 -0
- package/plugins/search/src/defaults.js +3 -0
- package/plugins/search/src/events/formHandlers.js +3 -9
- package/plugins/search/src/events/formHandlers.test.js +6 -9
- package/plugins/search/src/events/inputHandlers.js +8 -2
- package/plugins/search/src/events/inputHandlers.test.js +20 -0
- package/plugins/search/src/events/suggestionHandlers.js +3 -5
- package/plugins/search/src/events/suggestionHandlers.test.js +5 -8
- package/plugins/search/src/utils/showResult.js +9 -0
- package/plugins/search/src/utils/showResult.test.js +16 -0
- 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/Hints/Hints.module.scss +1 -1
- 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/components/Viewport/Viewport.jsx +6 -4
- package/src/App/components/Viewport/Viewport.test.jsx +18 -1
- package/src/App/hooks/useMapAnnouncements.js +7 -2
- package/src/App/hooks/useMapAnnouncements.test.js +13 -2
- package/src/App/hooks/useModalPanelBehaviour.js +3 -1
- package/src/App/initialiseApp.js +1 -1
- package/src/App/store/ServiceProvider.jsx +12 -3
- package/src/App/store/ServiceProvider.test.jsx +39 -0
- package/src/InteractiveMap/InteractiveMap.js +3 -3
- package/src/config/appConfig.js +1 -5
- package/src/config/mapSizeScales.js +8 -0
- package/src/config/symbolConfig.js +101 -25
- package/src/services/mapFocus.js +64 -0
- package/src/services/mapFocus.test.js +76 -0
- 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 +11 -5
- package/src/utils/detectInterfaceType.js +16 -5
- package/src/utils/detectInterfaceType.test.js +32 -0
- 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
|
@@ -5,7 +5,7 @@ import OlFeature from 'ol/Feature.js'
|
|
|
5
5
|
import Point from 'ol/geom/Point.js'
|
|
6
6
|
import Icon from 'ol/style/Icon.js'
|
|
7
7
|
import { updateHighlightedFeatures } from './highlightFeatures.js'
|
|
8
|
-
import {
|
|
8
|
+
import { SymbolImageCache } from './symbolImages.js'
|
|
9
9
|
|
|
10
10
|
const HIGHLIGHT_MARKER = '_highlight'
|
|
11
11
|
|
|
@@ -42,8 +42,17 @@ const drawLayer = (features = []) => {
|
|
|
42
42
|
|
|
43
43
|
const selEntry = (id, geometry = { type: 'Point', coordinates: [1, 2] }) => ({ layerId: 'draw', featureId: id, geometry })
|
|
44
44
|
|
|
45
|
+
// The provider's own cache of rasterised symbol images, passed to updateHighlightedFeatures
|
|
46
|
+
let symbolImages
|
|
47
|
+
const addImage = (imageId, imageData) => {
|
|
48
|
+
symbolImages.addImage(imageId, imageData)
|
|
49
|
+
return symbolImages.getImage(imageId)
|
|
50
|
+
}
|
|
51
|
+
|
|
45
52
|
beforeEach(() => {
|
|
46
|
-
|
|
53
|
+
symbolImages = new SymbolImageCache()
|
|
54
|
+
// jsdom has no canvas encoding; registering a normal image makes its data URI
|
|
55
|
+
HTMLCanvasElement.prototype.toDataURL = jest.fn(() => 'data:image/png;base64,mock')
|
|
47
56
|
HTMLCanvasElement.prototype.getContext = jest.fn(function () {
|
|
48
57
|
this._ctx ??= { putImageData: jest.fn() }
|
|
49
58
|
return this._ctx
|
|
@@ -57,31 +66,31 @@ describe('updateHighlightedFeatures', () => {
|
|
|
57
66
|
|
|
58
67
|
test('creates the highlight overlay layer once and reuses it on later calls', () => {
|
|
59
68
|
const map = createFakeMap([drawLayer()])
|
|
60
|
-
updateHighlightedFeatures(map, [], [], {})
|
|
69
|
+
updateHighlightedFeatures(map, [], [], {}, symbolImages)
|
|
61
70
|
expect(map.addLayer).toHaveBeenCalledTimes(1)
|
|
62
|
-
updateHighlightedFeatures(map, [], [], {})
|
|
71
|
+
updateHighlightedFeatures(map, [], [], {}, symbolImages)
|
|
63
72
|
expect(map.addLayer).toHaveBeenCalledTimes(1) // reused, not re-added
|
|
64
73
|
})
|
|
65
74
|
|
|
66
75
|
test('clears previous overlay features before adding the current selection', () => {
|
|
67
76
|
const map = createFakeMap([drawLayer([['f1', {}]])])
|
|
68
77
|
const stylesMap = { draw: { stroke: '#000', selectionStroke: '#000', fill: 'transparent', strokeWidth: 2, activeStrokeWidth: 2 } }
|
|
69
|
-
updateHighlightedFeatures(map, [{ ...selEntry('f1'), geometry: { type: 'LineString', coordinates: [[0, 0], [1, 1]] } }], [], stylesMap)
|
|
78
|
+
updateHighlightedFeatures(map, [{ ...selEntry('f1'), geometry: { type: 'LineString', coordinates: [[0, 0], [1, 1]] } }], [], stylesMap, symbolImages)
|
|
70
79
|
expect(getHighlightLayer(map).getSource().getFeatures()).toHaveLength(1)
|
|
71
|
-
updateHighlightedFeatures(map, [], [], stylesMap)
|
|
80
|
+
updateHighlightedFeatures(map, [], [], stylesMap, symbolImages)
|
|
72
81
|
expect(getHighlightLayer(map).getSource().getFeatures()).toHaveLength(0)
|
|
73
82
|
})
|
|
74
83
|
|
|
75
84
|
test('a feature with no geometry is skipped', () => {
|
|
76
85
|
const map = createFakeMap([drawLayer()])
|
|
77
86
|
const stylesMap = { draw: { stroke: '#000', selectionStroke: '#000', fill: 'transparent', strokeWidth: 2, activeStrokeWidth: 2 } }
|
|
78
|
-
updateHighlightedFeatures(map, [{ layerId: 'draw' }], [], stylesMap)
|
|
87
|
+
updateHighlightedFeatures(map, [{ layerId: 'draw' }], [], stylesMap, symbolImages)
|
|
79
88
|
expect(getHighlightLayer(map).getSource().getFeatures()).toHaveLength(0)
|
|
80
89
|
})
|
|
81
90
|
|
|
82
91
|
test('a feature whose layerId has no stylesMap entry and no symbol properties renders nothing', () => {
|
|
83
92
|
const map = createFakeMap([drawLayer([['f1', {}]])])
|
|
84
|
-
updateHighlightedFeatures(map, [{ ...selEntry('f1'), geometry: { type: 'LineString', coordinates: [[0, 0], [1, 1]] } }], [], {})
|
|
93
|
+
updateHighlightedFeatures(map, [{ ...selEntry('f1'), geometry: { type: 'LineString', coordinates: [[0, 0], [1, 1]] } }], [], {}, symbolImages)
|
|
85
94
|
expect(getHighlightLayer(map).getSource().getFeatures()).toHaveLength(0)
|
|
86
95
|
})
|
|
87
96
|
|
|
@@ -89,7 +98,7 @@ describe('updateHighlightedFeatures', () => {
|
|
|
89
98
|
const map = createFakeMap([drawLayer([['f1', {}]])])
|
|
90
99
|
const stylesMap = { draw: { stroke: '#000', selectionStroke: '#111', fill: '#222', strokeWidth: 3, activeStrokeWidth: 5 } }
|
|
91
100
|
const feature = { ...selEntry('f1'), geometry: { type: 'Polygon', coordinates: [[[0, 0], [1, 0], [1, 1], [0, 0]]] } }
|
|
92
|
-
updateHighlightedFeatures(map, [feature], [], stylesMap)
|
|
101
|
+
updateHighlightedFeatures(map, [feature], [], stylesMap, symbolImages)
|
|
93
102
|
const [hlFeature] = getHighlightLayer(map).getSource().getFeatures()
|
|
94
103
|
const styles = hlFeature.getStyle()
|
|
95
104
|
expect(styles.some(s => s.getFill())).toBe(true) // selected polygons get a fill
|
|
@@ -102,13 +111,14 @@ describe('updateHighlightedFeatures', () => {
|
|
|
102
111
|
symbolImageId: imageId,
|
|
103
112
|
symbolSelectedImageId: variantImageId,
|
|
104
113
|
symbolActiveImageId: variantImageId,
|
|
114
|
+
symbolImageAnchor: [0.5, 1],
|
|
105
115
|
symbolPixelRatio: 2
|
|
106
116
|
})
|
|
107
117
|
|
|
108
118
|
test('renders the selected-variant Icon for a selected symbol point, scaled down by symbolPixelRatio', () => {
|
|
109
|
-
const canvas =
|
|
119
|
+
const canvas = addImage('sym-sel', { width: 88, height: 88 })
|
|
110
120
|
const map = createFakeMap([drawLayer([['f1', symbolProperties('sym-sel')]])])
|
|
111
|
-
updateHighlightedFeatures(map, [selEntry('f1')], [], {})
|
|
121
|
+
updateHighlightedFeatures(map, [selEntry('f1')], [], {}, symbolImages)
|
|
112
122
|
const [hlFeature] = getHighlightLayer(map).getSource().getFeatures()
|
|
113
123
|
const [style] = hlFeature.getStyle()
|
|
114
124
|
expect(style.getImage()).toBeInstanceOf(Icon)
|
|
@@ -117,9 +127,9 @@ describe('updateHighlightedFeatures', () => {
|
|
|
117
127
|
})
|
|
118
128
|
|
|
119
129
|
test('renders the active-variant Icon for the keyboard-cursor item', () => {
|
|
120
|
-
const canvas =
|
|
130
|
+
const canvas = addImage('sym-act', { width: 44, height: 44 })
|
|
121
131
|
const map = createFakeMap([drawLayer([['f1', symbolProperties('sym-act')]])])
|
|
122
|
-
updateHighlightedFeatures(map, [], [selEntry('f1')], {})
|
|
132
|
+
updateHighlightedFeatures(map, [], [selEntry('f1')], {}, symbolImages)
|
|
123
133
|
const [hlFeature] = getHighlightLayer(map).getSource().getFeatures()
|
|
124
134
|
const [style] = hlFeature.getStyle()
|
|
125
135
|
expect(style.getImage().getImage(1)).toBe(canvas)
|
|
@@ -130,37 +140,53 @@ describe('updateHighlightedFeatures', () => {
|
|
|
130
140
|
// variants (point/pointSymbolImages.js) and rewrites them onto the live feature — the
|
|
131
141
|
// highlight must pick up the NEW id, not whatever was true when the user first selected.
|
|
132
142
|
test('picks up a re-resolved symbolSelectedImageId after a map style change, not whatever was true at selection time', () => {
|
|
133
|
-
const canvasLight =
|
|
134
|
-
const canvasDark =
|
|
143
|
+
const canvasLight = addImage('sym-sel-light', { width: 44, height: 44 })
|
|
144
|
+
const canvasDark = addImage('sym-sel-dark', { width: 44, height: 44 })
|
|
135
145
|
const layer = drawLayer([['f1', symbolProperties('sym-sel-light')]])
|
|
136
146
|
const map = createFakeMap([layer])
|
|
137
147
|
|
|
138
|
-
updateHighlightedFeatures(map, [selEntry('f1')], [], {})
|
|
148
|
+
updateHighlightedFeatures(map, [selEntry('f1')], [], {}, symbolImages)
|
|
139
149
|
const before = getHighlightLayer(map).getSource().getFeatures()[0].getStyle()[0]
|
|
140
150
|
expect(before.getImage().getImage(1)).toBe(canvasLight)
|
|
141
151
|
|
|
142
152
|
// Simulate refreshAllPointSymbols re-resolving the point against a new map style.
|
|
143
153
|
layer.getSource().getFeatureById('f1').set('symbolSelectedImageId', 'sym-sel-dark')
|
|
144
|
-
updateHighlightedFeatures(map, [selEntry('f1')], [], {})
|
|
154
|
+
updateHighlightedFeatures(map, [selEntry('f1')], [], {}, symbolImages)
|
|
145
155
|
const after = getHighlightLayer(map).getSource().getFeatures()[0].getStyle()[0]
|
|
146
156
|
expect(after.getImage().getImage(1)).toBe(canvasDark)
|
|
147
157
|
})
|
|
148
158
|
|
|
149
159
|
test('falls back to no highlight (not a crash) when the variant has not been cached yet', () => {
|
|
150
160
|
const map = createFakeMap([drawLayer([['f1', symbolProperties('sym-not-cached')]])])
|
|
151
|
-
updateHighlightedFeatures(map, [selEntry('f1')], [], {})
|
|
161
|
+
updateHighlightedFeatures(map, [selEntry('f1')], [], {}, symbolImages)
|
|
162
|
+
expect(getHighlightLayer(map).getSource().getFeatures()).toHaveLength(0)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
test('uses the anchor draw resolved for the point', () => {
|
|
166
|
+
addImage('sym-sel', { width: 44, height: 44 })
|
|
167
|
+
const map = createFakeMap([drawLayer([['f1', { ...symbolProperties('sym-sel'), symbolImageAnchor: [0.25, 0.75] }]])])
|
|
168
|
+
updateHighlightedFeatures(map, [selEntry('f1')], [], {}, symbolImages)
|
|
169
|
+
const [style] = getHighlightLayer(map).getSource().getFeatures()[0].getStyle()
|
|
170
|
+
expect(style.getImage().getAnchor()).toEqual([11, 33]) // [0.25, 0.75] × 44px
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
test('falls back to no highlight until the point\'s anchor has been resolved', () => {
|
|
174
|
+
addImage('sym-sel', { width: 44, height: 44 })
|
|
175
|
+
const { symbolImageAnchor, ...unresolved } = symbolProperties('sym-sel')
|
|
176
|
+
const map = createFakeMap([drawLayer([['f1', unresolved]])])
|
|
177
|
+
updateHighlightedFeatures(map, [selEntry('f1')], [], {}, symbolImages)
|
|
152
178
|
expect(getHighlightLayer(map).getSource().getFeatures()).toHaveLength(0)
|
|
153
179
|
})
|
|
154
180
|
|
|
155
181
|
test('falls back to no highlight when the live feature cannot be found (e.g. deleted mid-flight)', () => {
|
|
156
182
|
const map = createFakeMap([drawLayer()]) // empty source — 'f1' doesn't exist
|
|
157
|
-
updateHighlightedFeatures(map, [selEntry('f1')], [], {})
|
|
183
|
+
updateHighlightedFeatures(map, [selEntry('f1')], [], {}, symbolImages)
|
|
158
184
|
expect(getHighlightLayer(map).getSource().getFeatures()).toHaveLength(0)
|
|
159
185
|
})
|
|
160
186
|
|
|
161
187
|
test('a plain Point with no symbol config falls through to Stroke/Fill (still no visible highlight)', () => {
|
|
162
188
|
const map = createFakeMap([drawLayer([['f1', {}]])])
|
|
163
|
-
updateHighlightedFeatures(map, [selEntry('f1')], [], {})
|
|
189
|
+
updateHighlightedFeatures(map, [selEntry('f1')], [], {}, symbolImages)
|
|
164
190
|
expect(getHighlightLayer(map).getSource().getFeatures()).toHaveLength(0)
|
|
165
191
|
})
|
|
166
192
|
})
|
|
@@ -190,47 +216,56 @@ describe('updateHighlightedFeatures', () => {
|
|
|
190
216
|
|
|
191
217
|
const registerDatasetSymbol = async () => {
|
|
192
218
|
const symbolRegistry = {
|
|
193
|
-
|
|
219
|
+
getSymbolImageIds: jest.fn(() => ({ normal: 'ds-symbol-normal', active: 'ds-symbol-active', selected: 'ds-symbol-selected' })),
|
|
194
220
|
rasteriseSymbolImage: jest.fn(async (style, mapStyle, variant) => ({
|
|
195
|
-
imageId:
|
|
221
|
+
imageId: `ds-symbol-${variant}`,
|
|
196
222
|
imageData: { width: 30, height: 30 }
|
|
197
223
|
}))
|
|
198
224
|
}
|
|
199
|
-
await registerSymbol({ symbol: 'square' }, {}, symbolRegistry, 3)
|
|
225
|
+
await symbolImages.registerSymbol({ symbol: 'square' }, {}, symbolRegistry, 3)
|
|
200
226
|
}
|
|
201
227
|
|
|
202
228
|
test('renders the selected-variant Icon for a dataset symbol point', async () => {
|
|
203
229
|
await registerDatasetSymbol()
|
|
204
|
-
const selectedCanvas =
|
|
205
|
-
const map = createFakeMap([datasetSymbolLayer([['f1', {}]], { imageId: 'ds-symbol-normal', anchor: [0.5, 1] })])
|
|
230
|
+
const selectedCanvas = symbolImages.getImage('ds-symbol-selected')
|
|
231
|
+
const map = createFakeMap([datasetSymbolLayer([['f1', {}]], { imageId: 'ds-symbol-normal', anchor: [0.5, 1], pixelRatio: 3 })])
|
|
206
232
|
|
|
207
|
-
updateHighlightedFeatures(map, [{ layerId: LAYER_ID, featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], {})
|
|
233
|
+
updateHighlightedFeatures(map, [{ layerId: LAYER_ID, featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], {}, symbolImages)
|
|
208
234
|
const [hlFeature] = getHighlightLayer(map).getSource().getFeatures()
|
|
209
235
|
const [style] = hlFeature.getStyle()
|
|
210
236
|
expect(style.getImage()).toBeInstanceOf(Icon)
|
|
211
237
|
expect(style.getImage().getImage(1)).toBe(selectedCanvas)
|
|
238
|
+
// drawn 1:1 — scaled by the pixelRatio the image was rasterised at
|
|
239
|
+
expect(style.getImage().getScale()).toBeCloseTo(1 / 3)
|
|
212
240
|
})
|
|
213
241
|
|
|
214
242
|
test('renders the active-variant Icon for the keyboard-cursor item', async () => {
|
|
215
243
|
await registerDatasetSymbol()
|
|
216
|
-
const activeCanvas =
|
|
217
|
-
const map = createFakeMap([datasetSymbolLayer([['f1', {}]], { imageId: 'ds-symbol-normal', anchor: [0.5, 1] })])
|
|
244
|
+
const activeCanvas = symbolImages.getImage('ds-symbol-active')
|
|
245
|
+
const map = createFakeMap([datasetSymbolLayer([['f1', {}]], { imageId: 'ds-symbol-normal', anchor: [0.5, 1], pixelRatio: 3 })])
|
|
218
246
|
|
|
219
|
-
updateHighlightedFeatures(map, [], [{ layerId: LAYER_ID, featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], {})
|
|
247
|
+
updateHighlightedFeatures(map, [], [{ layerId: LAYER_ID, featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], {}, symbolImages)
|
|
220
248
|
const [hlFeature] = getHighlightLayer(map).getSource().getFeatures()
|
|
221
249
|
const [style] = hlFeature.getStyle()
|
|
222
250
|
expect(style.getImage().getImage(1)).toBe(activeCanvas)
|
|
223
251
|
})
|
|
224
252
|
|
|
253
|
+
test('falls back to no highlight when no symbol image cache is given', async () => {
|
|
254
|
+
await registerDatasetSymbol()
|
|
255
|
+
const map = createFakeMap([datasetSymbolLayer([['f1', {}]], { imageId: 'ds-symbol-normal', anchor: [0.5, 1], pixelRatio: 3 })])
|
|
256
|
+
updateHighlightedFeatures(map, [{ layerId: LAYER_ID, featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], {})
|
|
257
|
+
expect(getHighlightLayer(map).getSource().getFeatures()).toHaveLength(0)
|
|
258
|
+
})
|
|
259
|
+
|
|
225
260
|
test('falls back to no highlight when the layer has no symbolMeta (not a symbol dataset)', () => {
|
|
226
261
|
const map = createFakeMap([datasetSymbolLayer([['f1', {}]])])
|
|
227
|
-
updateHighlightedFeatures(map, [{ layerId: LAYER_ID, featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], {})
|
|
262
|
+
updateHighlightedFeatures(map, [{ layerId: LAYER_ID, featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], {}, symbolImages)
|
|
228
263
|
expect(getHighlightLayer(map).getSource().getFeatures()).toHaveLength(0)
|
|
229
264
|
})
|
|
230
265
|
|
|
231
266
|
test('falls back to no highlight when the variant has not been rasterised/cached yet', () => {
|
|
232
267
|
const map = createFakeMap([datasetSymbolLayer([['f1', {}]], { imageId: 'ds-symbol-never-registered', anchor: [0.5, 1] })])
|
|
233
|
-
updateHighlightedFeatures(map, [{ layerId: LAYER_ID, featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], {})
|
|
268
|
+
updateHighlightedFeatures(map, [{ layerId: LAYER_ID, featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], {}, symbolImages)
|
|
234
269
|
expect(getHighlightLayer(map).getSource().getFeatures()).toHaveLength(0)
|
|
235
270
|
})
|
|
236
271
|
})
|
|
@@ -241,7 +276,7 @@ describe('VectorTileLayer style-wrap', () => {
|
|
|
241
276
|
const vt = new VectorTileLayer({})
|
|
242
277
|
vt.set('layerType', 'vectorTile')
|
|
243
278
|
const map = createFakeMap([vt, drawLayer()])
|
|
244
|
-
expect(() => updateHighlightedFeatures(map, [], [], {})).not.toThrow()
|
|
279
|
+
expect(() => updateHighlightedFeatures(map, [], [], {}, symbolImages)).not.toThrow()
|
|
245
280
|
})
|
|
246
281
|
|
|
247
282
|
const vtStylesMap = { 'existing-fields': { stroke: '#000', selectionStroke: '#111', fill: 'transparent', strokeWidth: 2, activeStrokeWidth: 2 } }
|
|
@@ -264,7 +299,7 @@ describe('VectorTileLayer style-wrap', () => {
|
|
|
264
299
|
const map = createFakeMap([vt])
|
|
265
300
|
const feature = datasetVtFeature('f1')
|
|
266
301
|
|
|
267
|
-
updateHighlightedFeatures(map, [{ layerId: 'existing-fields', featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], vtStylesMap)
|
|
302
|
+
updateHighlightedFeatures(map, [{ layerId: 'existing-fields', featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], vtStylesMap, symbolImages)
|
|
268
303
|
|
|
269
304
|
const styles = vt.getStyle()(feature, 1)
|
|
270
305
|
expect(Array.isArray(styles)).toBe(true)
|
|
@@ -278,7 +313,7 @@ describe('VectorTileLayer style-wrap', () => {
|
|
|
278
313
|
const map = createFakeMap([vt])
|
|
279
314
|
const feature = datasetVtFeature('f2') // not the selected f1
|
|
280
315
|
|
|
281
|
-
updateHighlightedFeatures(map, [{ layerId: 'existing-fields', featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], vtStylesMap)
|
|
316
|
+
updateHighlightedFeatures(map, [{ layerId: 'existing-fields', featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], vtStylesMap, symbolImages)
|
|
282
317
|
|
|
283
318
|
const base = vt._highlightOriginalStyle(feature, 1)
|
|
284
319
|
const styled = vt.getStyle()(feature, 1)
|
|
@@ -296,12 +331,12 @@ describe('VectorTileLayer style-wrap', () => {
|
|
|
296
331
|
const map = createFakeMap([vt])
|
|
297
332
|
const selection = [{ layerId: 'existing-fields', featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }]
|
|
298
333
|
|
|
299
|
-
updateHighlightedFeatures(map, selection, [], vtStylesMap)
|
|
334
|
+
updateHighlightedFeatures(map, selection, [], vtStylesMap, symbolImages)
|
|
300
335
|
|
|
301
336
|
// Something outside the highlight module (a theme switch) restyles the layer directly.
|
|
302
337
|
vt.setStyle({ 'fill-color': '#ff0000' })
|
|
303
338
|
|
|
304
|
-
updateHighlightedFeatures(map, selection, [], vtStylesMap)
|
|
339
|
+
updateHighlightedFeatures(map, selection, [], vtStylesMap, symbolImages)
|
|
305
340
|
|
|
306
341
|
const otherFeature = datasetVtFeature('not-selected')
|
|
307
342
|
const [styled] = vt.getStyle()(otherFeature, 1)
|
|
@@ -314,10 +349,10 @@ describe('VectorTileLayer style-wrap', () => {
|
|
|
314
349
|
vt.set('layerId', 'existing-fields')
|
|
315
350
|
const map = createFakeMap([vt])
|
|
316
351
|
|
|
317
|
-
updateHighlightedFeatures(map, [{ layerId: 'existing-fields', featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], vtStylesMap)
|
|
352
|
+
updateHighlightedFeatures(map, [{ layerId: 'existing-fields', featureId: 'f1', geometry: { type: 'Point', coordinates: [1, 2] } }], [], vtStylesMap, symbolImages)
|
|
318
353
|
expect(vt._highlightOriginalStyle).toBeDefined()
|
|
319
354
|
|
|
320
|
-
updateHighlightedFeatures(map, [], [], vtStylesMap)
|
|
355
|
+
updateHighlightedFeatures(map, [], [], vtStylesMap, symbolImages)
|
|
321
356
|
expect(vt._highlightOriginalStyle).toBeUndefined()
|
|
322
357
|
})
|
|
323
358
|
})
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import Fill from 'ol/style/Fill.js'
|
|
2
|
+
import { RecentImageSets } from '../../../../../src/utils/recentImageSets.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The OpenLayers provider's pattern fills: each pattern image as an ol/style/Fill whose colour
|
|
6
|
+
* is a CanvasPattern, cached by the imageId patternRegistry computes. A CanvasPattern built
|
|
7
|
+
* straight from the pixelRatio-scaled image stays crisp at any pixel ratio, which a flat style's
|
|
8
|
+
* fill-pattern-src can't — it crops the pattern to its display size rather than scaling it.
|
|
9
|
+
*
|
|
10
|
+
* Fills are kept for the current and previous map size and style; ones only older sizes and
|
|
11
|
+
* styles used are dropped, so they don't build up as the map size or style changes.
|
|
12
|
+
*/
|
|
13
|
+
export class PatternImageCache {
|
|
14
|
+
fills = new Map() // imageId → Fill
|
|
15
|
+
recentImageSets = new RecentImageSets()
|
|
16
|
+
|
|
17
|
+
/** Synchronous lookup for style functions — undefined until the pattern has been registered. */
|
|
18
|
+
getFill (imageId) {
|
|
19
|
+
return this.fills.get(imageId)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
addFill (imageId, imageData) {
|
|
23
|
+
const source = document.createElement('canvas')
|
|
24
|
+
source.width = imageData.width
|
|
25
|
+
source.height = imageData.height
|
|
26
|
+
source.getContext('2d').putImageData(imageData, 0, 0)
|
|
27
|
+
const pattern = document.createElement('canvas').getContext('2d').createPattern(source, 'repeat')
|
|
28
|
+
this.fills.set(imageId, new Fill({ color: pattern }))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Rasterises and caches each pattern's fill. Fills already cached are reused, and styles that
|
|
33
|
+
* resolve to the same image are only rasterised once.
|
|
34
|
+
*
|
|
35
|
+
* @param {Object[]} styles - pattern style configs (fillPattern/fillPatternSvgContent plus colours)
|
|
36
|
+
* @param {string} mapStyleId
|
|
37
|
+
* @param {Object} patternRegistry
|
|
38
|
+
* @param {number} pixelRatio
|
|
39
|
+
* @returns {Promise<void>}
|
|
40
|
+
*/
|
|
41
|
+
async registerPatterns (styles, mapStyleId, patternRegistry, pixelRatio) {
|
|
42
|
+
const stylesByImageId = new Map()
|
|
43
|
+
styles.forEach((style) => {
|
|
44
|
+
const imageId = patternRegistry.getPatternImageId(style, mapStyleId, pixelRatio)
|
|
45
|
+
if (imageId && !stylesByImageId.has(imageId)) {
|
|
46
|
+
stylesByImageId.set(imageId, style)
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
await Promise.all(Array.from(stylesByImageId, async ([imageId, style]) => {
|
|
50
|
+
if (this.fills.has(imageId)) {
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
const result = await patternRegistry.rasterisePatternImage(style, mapStyleId, pixelRatio)
|
|
54
|
+
if (result && !this.fills.has(result.imageId)) {
|
|
55
|
+
this.addFill(result.imageId, result.imageData)
|
|
56
|
+
}
|
|
57
|
+
}))
|
|
58
|
+
this.recentImageSets.add(`${mapStyleId}|${pixelRatio}`, Array.from(stylesByImageId.keys()))
|
|
59
|
+
.forEach((imageId) => this.fills.delete(imageId))
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import Fill from 'ol/style/Fill.js'
|
|
2
|
+
import { PatternImageCache } from './patternImages.js'
|
|
3
|
+
|
|
4
|
+
const OUTDOOR = 'outdoor'
|
|
5
|
+
|
|
6
|
+
const imageIdFor = (style, pixelRatio) => `pattern-${style.fillPattern}-${pixelRatio}x`
|
|
7
|
+
|
|
8
|
+
const makePatternRegistry = () => ({
|
|
9
|
+
getPatternImageId: jest.fn((style, mapStyleId, pixelRatio) => (style.fillPattern ? imageIdFor(style, pixelRatio) : null)),
|
|
10
|
+
rasterisePatternImage: jest.fn(async (style, mapStyleId, pixelRatio) => ({
|
|
11
|
+
imageId: imageIdFor(style, pixelRatio),
|
|
12
|
+
imageData: { width: 16 * pixelRatio, height: 16 * pixelRatio }
|
|
13
|
+
}))
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
let cache
|
|
17
|
+
let createPatternCalls
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
cache = new PatternImageCache()
|
|
21
|
+
createPatternCalls = []
|
|
22
|
+
HTMLCanvasElement.prototype.getContext = jest.fn(function () {
|
|
23
|
+
this._ctx ??= {
|
|
24
|
+
putImageData: jest.fn(),
|
|
25
|
+
createPattern: jest.fn((source, repetition) => {
|
|
26
|
+
const pattern = { source, repetition }
|
|
27
|
+
createPatternCalls.push(pattern)
|
|
28
|
+
return pattern
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
return this._ctx
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
describe('PatternImageCache', () => {
|
|
36
|
+
it('caches a Fill built from a repeating CanvasPattern of the rasterised image', async () => {
|
|
37
|
+
await cache.registerPatterns([{ fillPattern: 'dot' }], OUTDOOR, makePatternRegistry(), 2)
|
|
38
|
+
const fill = cache.getFill('pattern-dot-2x')
|
|
39
|
+
expect(fill).toBeInstanceOf(Fill)
|
|
40
|
+
const [pattern] = createPatternCalls
|
|
41
|
+
expect(pattern.repetition).toBe('repeat')
|
|
42
|
+
expect([pattern.source.width, pattern.source.height]).toEqual([32, 32])
|
|
43
|
+
expect(fill.getColor()).toBe(pattern)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('returns undefined for a pattern that has not been registered', () => {
|
|
47
|
+
expect(cache.getFill('pattern-unseen')).toBeUndefined()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('skips styles with no pattern and patterns that do not rasterise', async () => {
|
|
51
|
+
const patternRegistry = makePatternRegistry()
|
|
52
|
+
patternRegistry.rasterisePatternImage.mockResolvedValueOnce(null)
|
|
53
|
+
await cache.registerPatterns([{ fillColor: 'red' }, { fillPattern: 'missing' }], OUTDOOR, patternRegistry, 1)
|
|
54
|
+
expect(cache.fills.size).toBe(0)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('rasterises a pattern shared by several styles once, and reuses a cached fill', async () => {
|
|
58
|
+
const patternRegistry = makePatternRegistry()
|
|
59
|
+
await cache.registerPatterns([{ fillPattern: 'dot' }, { fillPattern: 'dot' }], OUTDOOR, patternRegistry, 1)
|
|
60
|
+
await cache.registerPatterns([{ fillPattern: 'dot' }], OUTDOOR, patternRegistry, 1)
|
|
61
|
+
expect(patternRegistry.rasterisePatternImage).toHaveBeenCalledTimes(1)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('keeps the current and previous size, and drops an older one\'s fill when a third arrives', async () => {
|
|
65
|
+
const patternRegistry = makePatternRegistry()
|
|
66
|
+
const style = { fillPattern: 'dot' }
|
|
67
|
+
await cache.registerPatterns([style], OUTDOOR, patternRegistry, 1)
|
|
68
|
+
await cache.registerPatterns([style], OUTDOOR, patternRegistry, 2)
|
|
69
|
+
expect(cache.getFill('pattern-dot-1x')).toBeDefined()
|
|
70
|
+
await cache.registerPatterns([style], OUTDOOR, patternRegistry, 3)
|
|
71
|
+
expect(cache.getFill('pattern-dot-1x')).toBeUndefined()
|
|
72
|
+
expect(cache.fills.size).toBe(2)
|
|
73
|
+
})
|
|
74
|
+
})
|
|
@@ -1,114 +1,122 @@
|
|
|
1
|
+
import IconImage from 'ol/style/IconImage.js'
|
|
2
|
+
import { shared as iconImageCache } from 'ol/style/IconImageCache.js'
|
|
3
|
+
import ImageState from 'ol/ImageState.js'
|
|
4
|
+
import { RecentImageSets } from '../../../../../src/utils/recentImageSets.js'
|
|
5
|
+
|
|
1
6
|
/**
|
|
2
7
|
* The OL "last mile" for symbolRegistry's rasterised symbol images: turns the ImageData
|
|
3
8
|
* rasteriseSymbolImage() produces into something OL can render, cached by the same imageId
|
|
4
|
-
* symbolRegistry computes. OL has no map-level image registry like MapLibre's map.addImage,
|
|
5
|
-
*
|
|
9
|
+
* symbolRegistry computes. OL has no map-level image registry like MapLibre's map.addImage, so
|
|
10
|
+
* the OL provider owns one of these per map instead (see OpenLayersProvider.addSymbolsToMap).
|
|
6
11
|
*
|
|
7
|
-
* Two
|
|
8
|
-
*
|
|
9
|
-
* getCachedSymbolImage); OpenLayersDataset.flatStyle's icon-src needs a data URI string instead.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
// Fixed oversampling ratio, unrelated to the map's actual pixelRatio — icon-scale genuinely
|
|
13
|
-
// decouples a symbol's on-screen size from its native pixel detail, so one fixed high-detail
|
|
14
|
-
// raster stays crisp at any pixelRatio with no re-rasterisation needed on resize.
|
|
15
|
-
export const SYMBOL_RASTER_PIXEL_RATIO = 3
|
|
16
|
-
|
|
17
|
-
const imageCache = new Map() // imageId → HTMLCanvasElement
|
|
18
|
-
const dataUriCache = new Map() // imageId → data URI string
|
|
19
|
-
const activeImageMap = new Map() // normalId → activeId
|
|
20
|
-
const selectedImageMap = new Map() // normalId → selectedId
|
|
21
|
-
|
|
22
|
-
/** Synchronous lookup for style functions (which must be synchronous) — undefined until
|
|
23
|
-
* getOrCreateSymbolImage() has resolved it at least once. */
|
|
24
|
-
export const getCachedSymbolImage = (imageId) => imageCache.get(imageId)
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Reverse-map a symbol's normal (base) imageId to its active/selected variant, mirroring
|
|
28
|
-
* MapLibre's own active/selected image maps. Used by highlightFeatures.js to highlight a
|
|
29
|
-
* dataset symbol point, which (unlike a drawn point) has no active/selected id of its own.
|
|
30
|
-
*/
|
|
31
|
-
export const getActiveSymbolImageId = (normalId) => activeImageMap.get(normalId) ?? null
|
|
32
|
-
export const getSelectedSymbolImageId = (normalId) => selectedImageMap.get(normalId) ?? null
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Rasterise-once-cache-forever: draws the given ImageData onto a canvas the first time
|
|
36
|
-
* imageId is seen, and returns the cached canvas on every subsequent call.
|
|
12
|
+
* Two shapes of the same image are kept: a <canvas> for styles that build an ol/style/Icon
|
|
13
|
+
* directly (draw points, highlights), and a data URI for a flat style's icon-src (datasets).
|
|
37
14
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* @returns {HTMLCanvasElement}
|
|
15
|
+
* Images are kept for the current and previous map size and style; ones only older sizes and
|
|
16
|
+
* styles used are dropped, so the cache doesn't build up as the map size or style changes.
|
|
41
17
|
*/
|
|
42
|
-
export
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
18
|
+
export class SymbolImageCache {
|
|
19
|
+
images = new Map() // imageId → HTMLCanvasElement
|
|
20
|
+
dataUris = new Map() // imageId → data URI string
|
|
21
|
+
activeImageIds = new Map() // normal imageId → active imageId
|
|
22
|
+
selectedImageIds = new Map() // normal imageId → selected imageId
|
|
23
|
+
recentImageSets = new RecentImageSets()
|
|
24
|
+
|
|
25
|
+
/** Synchronous lookup for style functions — undefined until the image has been registered. */
|
|
26
|
+
getImage (imageId) {
|
|
27
|
+
return this.images.get(imageId)
|
|
46
28
|
}
|
|
47
|
-
const canvas = document.createElement('canvas')
|
|
48
|
-
canvas.width = imageData.width
|
|
49
|
-
canvas.height = imageData.height
|
|
50
|
-
canvas.getContext('2d').putImageData(imageData, 0, 0)
|
|
51
|
-
imageCache.set(imageId, canvas)
|
|
52
|
-
return canvas
|
|
53
|
-
}
|
|
54
29
|
|
|
55
|
-
/** Synchronous lookup for
|
|
56
|
-
|
|
57
|
-
|
|
30
|
+
/** Synchronous lookup for a flat style's icon-src — undefined until the image has been registered. */
|
|
31
|
+
getDataUri (imageId) {
|
|
32
|
+
return this.dataUris.get(imageId)
|
|
33
|
+
}
|
|
58
34
|
|
|
59
|
-
/**
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
* symbol point the same way it already does for a drawn one.
|
|
63
|
-
* @param {Object} style - Dataset style with symbol/symbolSvgContent properties
|
|
64
|
-
* @param {Object} mapStyle - Current map style config
|
|
65
|
-
* @param {Object} symbolRegistry
|
|
66
|
-
* @param {number} pixelRatio - the map's actual current pixelRatio
|
|
67
|
-
* @returns {Promise<void>}
|
|
68
|
-
*/
|
|
69
|
-
export const registerSymbol = async (style, mapStyle, symbolRegistry, pixelRatio) => {
|
|
70
|
-
const normalId = symbolRegistry.getSymbolImageId(style, mapStyle, false, pixelRatio)
|
|
71
|
-
if (!normalId) {
|
|
72
|
-
return
|
|
35
|
+
/** A symbol's active (keyboard cursor) variant, from its normal imageId */
|
|
36
|
+
getActiveImageId (normalId) {
|
|
37
|
+
return this.activeImageIds.get(normalId) ?? null
|
|
73
38
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
39
|
+
|
|
40
|
+
/** A symbol's selected variant, from its normal imageId */
|
|
41
|
+
getSelectedImageId (normalId) {
|
|
42
|
+
return this.selectedImageIds.get(normalId) ?? null
|
|
77
43
|
}
|
|
78
44
|
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
45
|
+
addImage (imageId, imageData, withDataUri) {
|
|
46
|
+
const canvas = document.createElement('canvas')
|
|
47
|
+
canvas.width = imageData.width
|
|
48
|
+
canvas.height = imageData.height
|
|
49
|
+
canvas.getContext('2d').putImageData(imageData, 0, 0)
|
|
50
|
+
this.images.set(imageId, canvas)
|
|
51
|
+
if (withDataUri) {
|
|
52
|
+
const dataUri = canvas.toDataURL()
|
|
53
|
+
this.dataUris.set(imageId, dataUri)
|
|
54
|
+
// Seed OL's icon cache with the already-drawn canvas under this data URI, so a flat
|
|
55
|
+
// style's icon-src finds a loaded image straight away. Otherwise OL loads the data URI
|
|
56
|
+
// asynchronously and draws nothing for that icon until it has — a visible flicker each
|
|
57
|
+
// time styles switch to new images, as on every map-size change. Set rather than got, so
|
|
58
|
+
// it also replaces an unloaded entry left by removeImage.
|
|
59
|
+
iconImageCache.set(dataUri, null, new IconImage(canvas, dataUri, undefined, ImageState.LOADED, null))
|
|
83
60
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
removeImage (imageId) {
|
|
64
|
+
const dataUri = this.dataUris.get(imageId)
|
|
65
|
+
this.dataUris.delete(imageId)
|
|
66
|
+
// OL's icon cache would otherwise keep the canvas alive; an unloaded entry in its place holds
|
|
67
|
+
// nothing, and OL's own cache limit can clear it. Two images can draw identical pixels, so
|
|
68
|
+
// it's left alone while a kept image still has the same data URI.
|
|
69
|
+
if (dataUri && !Array.from(this.dataUris.values()).includes(dataUri)) {
|
|
70
|
+
iconImageCache.set(dataUri, null, new IconImage(undefined, dataUri, undefined, ImageState.IDLE, null))
|
|
90
71
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
72
|
+
this.images.delete(imageId)
|
|
73
|
+
this.activeImageIds.delete(imageId)
|
|
74
|
+
this.selectedImageIds.delete(imageId)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Rasterises and caches one symbol's normal, active and selected images, and maps its normal
|
|
79
|
+
* imageId to the other two. Images already cached are reused.
|
|
80
|
+
*
|
|
81
|
+
* @param {Object} style - a symbol style (symbol/symbolSvgContent plus token overrides)
|
|
82
|
+
* @param {Object} mapStyle - current map style config
|
|
83
|
+
* @param {Object} symbolRegistry
|
|
84
|
+
* @param {number} pixelRatio
|
|
85
|
+
* @returns {Promise<string[]>} the symbol's image ids
|
|
86
|
+
*/
|
|
87
|
+
async registerSymbol (style, mapStyle, symbolRegistry, pixelRatio) {
|
|
88
|
+
const imageIds = symbolRegistry.getSymbolImageIds(style, mapStyle, pixelRatio)
|
|
89
|
+
if (!imageIds) {
|
|
90
|
+
return []
|
|
96
91
|
}
|
|
97
|
-
|
|
98
|
-
|
|
92
|
+
this.activeImageIds.set(imageIds.normal, imageIds.active)
|
|
93
|
+
this.selectedImageIds.set(imageIds.normal, imageIds.selected)
|
|
99
94
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
95
|
+
await Promise.all(Object.entries(imageIds).map(async ([variant, imageId]) => {
|
|
96
|
+
if (this.images.has(imageId)) {
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
const result = await symbolRegistry.rasteriseSymbolImage(style, mapStyle, variant, pixelRatio)
|
|
100
|
+
if (result && !this.images.has(result.imageId)) {
|
|
101
|
+
this.addImage(result.imageId, result.imageData, variant === 'normal')
|
|
102
|
+
}
|
|
103
|
+
}))
|
|
104
|
+
return Object.values(imageIds)
|
|
104
105
|
}
|
|
105
|
-
await Promise.all(styleArray.map(style => registerSymbol(style, mapStyle, symbolRegistry, pixelRatio)))
|
|
106
|
-
}
|
|
107
106
|
|
|
108
|
-
/**
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Registers several symbols in parallel, then drops images only map sizes and styles older
|
|
109
|
+
* than the previous one used.
|
|
110
|
+
*
|
|
111
|
+
* @param {Object[]} styles
|
|
112
|
+
* @param {Object} mapStyle
|
|
113
|
+
* @param {Object} symbolRegistry
|
|
114
|
+
* @param {number} pixelRatio
|
|
115
|
+
* @returns {Promise<void>}
|
|
116
|
+
*/
|
|
117
|
+
async registerSymbols (styles, mapStyle, symbolRegistry, pixelRatio) {
|
|
118
|
+
const imageIds = await Promise.all(styles.map((style) => this.registerSymbol(style, mapStyle, symbolRegistry, pixelRatio)))
|
|
119
|
+
this.recentImageSets.add(`${mapStyle?.id}|${pixelRatio}`, imageIds.flat())
|
|
120
|
+
.forEach((imageId) => this.removeImage(imageId))
|
|
121
|
+
}
|
|
114
122
|
}
|