@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.
Files changed (147) hide show
  1. package/dist/esm/im-core.js +1 -1
  2. package/dist/umd/im-core.js +1 -1
  3. package/dist/umd/index.js +1 -1
  4. package/docs/api/context.md +18 -12
  5. package/docs/api/marker-config.md +16 -8
  6. package/docs/api/symbol-config.md +24 -9
  7. package/docs/api/symbol-registry.md +49 -14
  8. package/docs/plugins/datasets.md +4 -10
  9. package/docs/plugins/draw.md +2 -1
  10. package/docs/plugins/map-key.md +6 -4
  11. package/package.json +1 -1
  12. package/plugins/beta/map-styles/dist/esm/im-map-styles-plugin.js +1 -1
  13. package/plugins/beta/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
  14. package/plugins/beta/map-styles/dist/umd/index.js +1 -1
  15. package/plugins/datasets/dist/esm/im-datasets-ml-adapter.js +1 -1
  16. package/plugins/datasets/dist/esm/im-datasets-ol-adapter.js +1 -1
  17. package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
  18. package/plugins/datasets/dist/esm/mapboxStyleLayerAdapter.js +1 -1
  19. package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
  20. package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
  21. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  22. package/plugins/datasets/src/adapters/maplibre/layerBuilders.js +9 -9
  23. package/plugins/datasets/src/adapters/maplibre/layerBuilders.test.js +19 -3
  24. package/plugins/datasets/src/adapters/maplibre/maplibreLayerAdapter.js +9 -3
  25. package/plugins/datasets/src/adapters/maplibre/maplibreLayerAdapter.test.js +18 -3
  26. package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.js +3 -3
  27. package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.test.js +19 -26
  28. package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.js +15 -67
  29. package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.test.js +32 -105
  30. package/plugins/datasets/src/adapters/openlayers/layerBuilders.js +11 -13
  31. package/plugins/datasets/src/adapters/openlayers/layerBuilders.test.js +11 -3
  32. package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.js +48 -19
  33. package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.test.js +99 -11
  34. package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.js +26 -26
  35. package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.test.js +48 -29
  36. package/plugins/datasets/src/initialise/defaults.js +4 -3
  37. package/plugins/datasets/src/reducers/__data__/demoDatasets.js +1 -3
  38. package/plugins/datasets/src/registry/dataset.js +2 -1
  39. package/plugins/datasets/src/registry/dataset.test.js +13 -0
  40. package/plugins/datasets/src/registry/mapboxStyleDataset.js +1 -1
  41. package/plugins/datasets/src/registry/mapboxStyleDataset.test.js +1 -1
  42. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  43. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  44. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  45. package/plugins/draw/dist/esm/index.js +1 -1
  46. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  47. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  48. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  49. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  50. package/plugins/draw/dist/umd/index.js +1 -1
  51. package/plugins/draw/src/DrawInit.jsx +7 -5
  52. package/plugins/draw/src/DrawInit.test.jsx +18 -2
  53. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +11 -5
  54. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -11
  55. package/plugins/draw/src/adapters/maplibre/mapboxDraw.js +12 -9
  56. package/plugins/draw/src/adapters/maplibre/mapboxDraw.test.js +18 -0
  57. package/plugins/draw/src/adapters/maplibre/pointSymbolImages.js +81 -56
  58. package/plugins/draw/src/adapters/maplibre/pointSymbolImages.test.js +149 -80
  59. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +7 -6
  60. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +22 -25
  61. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.js +26 -8
  62. package/plugins/draw/src/adapters/openlayers/core/OLDrawManager.test.js +58 -16
  63. package/plugins/draw/src/adapters/openlayers/core/styles.js +33 -20
  64. package/plugins/draw/src/adapters/openlayers/core/styles.test.js +47 -21
  65. package/plugins/draw/src/adapters/openlayers/olDraw.js +16 -12
  66. package/plugins/draw/src/adapters/openlayers/olDraw.test.js +27 -32
  67. package/plugins/draw/src/adapters/openlayers/point/pointSymbolImages.js +44 -49
  68. package/plugins/draw/src/adapters/openlayers/point/pointSymbolImages.test.js +100 -119
  69. package/plugins/draw/src/defaults.js +2 -6
  70. package/plugins/draw/src/utils/stripInternalProperties.test.js +2 -1
  71. package/plugins/draw/src/utils/symbolKeys.js +3 -4
  72. package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
  73. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  74. package/plugins/interact/dist/umd/index.js +1 -1
  75. package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
  76. package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -2
  77. package/plugins/map-key/dist/umd/index.js +1 -1
  78. package/plugins/map-key/src/components/Key/Key.jsx +8 -4
  79. package/plugins/map-key/src/components/Key/KeyGroupItem.jsx +3 -1
  80. package/plugins/map-key/src/components/Key/KeyItem.jsx +2 -2
  81. package/plugins/map-key/src/components/Key/KeySvg.jsx +15 -14
  82. package/plugins/map-key/src/components/Key/KeySvg.test.jsx +21 -14
  83. package/plugins/map-key/src/components/Key/KeySvgPattern.jsx +1 -2
  84. package/plugins/map-key/src/components/Key/KeySvgPattern.test.jsx +3 -1
  85. package/plugins/map-key/src/components/Key/KeySvgSymbol.jsx +26 -12
  86. package/plugins/map-key/src/components/Key/KeySvgSymbol.test.jsx +24 -74
  87. package/plugins/map-key/src/components/Key/MapKey.jsx +3 -1
  88. package/plugins/map-key/src/components/Key/svgProperties.js +8 -2
  89. package/plugins/map-key/src/reducers/mergeKeyGroupItems.test.js +15 -0
  90. package/plugins/map-key/src/registry/index.js +0 -2
  91. package/plugins/map-key/src/registry/index.test.js +1 -9
  92. package/plugins/menu/src/registry/index.js +0 -2
  93. package/plugins/menu/src/registry/index.test.js +1 -9
  94. package/providers/beta/esri/dist/esm/im-esri-provider.js +1 -1
  95. package/providers/beta/openlayers/dist/esm/im-openlayers-provider.js +1 -1
  96. package/providers/beta/openlayers/dist/umd/im-openlayers-provider.js +1 -1
  97. package/providers/beta/openlayers/dist/umd/index.js +1 -1
  98. package/providers/beta/openlayers/src/openlayersProvider.js +102 -2
  99. package/providers/beta/openlayers/src/openlayersProvider.test.js +95 -0
  100. package/providers/beta/openlayers/src/utils/highlightFeatures.js +20 -25
  101. package/providers/beta/openlayers/src/utils/highlightFeatures.test.js +73 -38
  102. package/providers/beta/openlayers/src/utils/patternImages.js +61 -0
  103. package/providers/beta/openlayers/src/utils/patternImages.test.js +74 -0
  104. package/providers/beta/openlayers/src/utils/symbolImages.js +103 -95
  105. package/providers/beta/openlayers/src/utils/symbolImages.test.js +136 -85
  106. package/providers/mapProvider.js +53 -1
  107. package/providers/mapProvider.test.js +6 -0
  108. package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
  109. package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
  110. package/providers/maplibre/dist/umd/index.js +1 -1
  111. package/providers/maplibre/src/maplibreProvider.js +50 -17
  112. package/providers/maplibre/src/maplibreProvider.test.js +74 -20
  113. package/providers/maplibre/src/utils/highlightFeatures.js +19 -14
  114. package/providers/maplibre/src/utils/highlightFeatures.test.js +20 -14
  115. package/providers/maplibre/src/utils/patternImages.js +46 -29
  116. package/providers/maplibre/src/utils/patternImages.test.js +52 -4
  117. package/providers/maplibre/src/utils/symbolImages.js +80 -39
  118. package/providers/maplibre/src/utils/symbolImages.test.js +157 -54
  119. package/src/App/components/Markers/Markers.jsx +16 -25
  120. package/src/App/components/Markers/Markers.test.jsx +34 -48
  121. package/src/App/components/Markers/SymbolLabelMarker.jsx +1 -1
  122. package/src/App/components/Markers/SymbolMarker.jsx +1 -1
  123. package/src/App/store/ServiceProvider.jsx +6 -3
  124. package/src/App/store/ServiceProvider.test.jsx +24 -0
  125. package/src/config/appConfig.js +1 -5
  126. package/src/config/mapSizeScales.js +8 -0
  127. package/src/config/symbolConfig.js +101 -25
  128. package/src/services/patternRegistry.js +97 -76
  129. package/src/services/patternRegistry.test.js +19 -1
  130. package/src/services/symbolComposer.js +184 -0
  131. package/src/services/symbolComposer.test.js +132 -0
  132. package/src/services/symbolRegistry.js +326 -155
  133. package/src/services/symbolRegistry.test.js +334 -32
  134. package/src/types.js +9 -3
  135. package/src/utils/hashString.js +23 -0
  136. package/src/utils/hashString.test.js +29 -0
  137. package/src/utils/imageDataCache.js +43 -0
  138. package/src/utils/imageDataCache.test.js +38 -0
  139. package/src/utils/pathBounds.js +330 -0
  140. package/src/utils/pathBounds.test.js +145 -0
  141. package/src/utils/patternUtils.js +0 -8
  142. package/src/utils/patternUtils.test.js +0 -25
  143. package/src/utils/recentImageSets.js +35 -0
  144. package/src/utils/recentImageSets.test.js +46 -0
  145. package/src/utils/symbolUtils.js +26 -29
  146. package/src/utils/symbolUtils.test.js +27 -41
  147. package/plugins/map-key/dist/umd/im-map-key-plugin.js.LICENSE.txt +0 -1
@@ -1,11 +1,16 @@
1
1
  import { MapLibreDataset } from './mapLibreDataset.js'
2
2
  import { datasetRegistry } from '../../../registry/datasetRegistry.js'
3
3
  import { logger } from '../../../../../../src/services/logger.js'
4
+ import { hashString } from '../../../../../../src/utils/hashString.js'
4
5
  // Use the mock datasetRegistry with the demo datasets attached before each test
5
6
  // so we can test Dataset methods that depend on parent/sublayer relationships and styles
6
7
  jest.mock('../../../registry/datasetRegistry.js')
7
8
  jest.mock('../../../../../../src/services/logger.js')
8
9
 
10
+ // The demo existing-fields tiles URL and static geojson URL, as sourceId hashes them
11
+ const EXISTING_FIELDS_SOURCE_ID = `tiles-${hashString('https://farming-tiles-702a60f45633.herokuapp.com/field_parcels_with_hedges_wgs84/{z}/{x}/{y}')}`
12
+ const STATIC_GEOJSON_SOURCE_ID = `geojson-${hashString('https://example.com/static.geojson')}`
13
+
9
14
  describe('MapLibreDataset', () => {
10
15
  beforeEach(() => {
11
16
  logger.warn.mockClear()
@@ -212,12 +217,12 @@ describe('MapLibreDataset', () => {
212
217
 
213
218
  it('returns a tiles-based id for a tile dataset (array tiles)', () => {
214
219
  const dataset = datasetRegistry.getDataset('existing-fields')
215
- expect(dataset.sourceId).toBe('tiles-35m5lrb')
220
+ expect(dataset.sourceId).toBe(EXISTING_FIELDS_SOURCE_ID)
216
221
  })
217
222
 
218
223
  it('returns a tiles-based id when tiles is a plain string (line 76 non-array branch)', () => {
219
224
  const dataset = datasetRegistry.getDataset('existing-fields')
220
- expect(dataset.sourceId).toBe('tiles-35m5lrb')
225
+ expect(dataset.sourceId).toBe(EXISTING_FIELDS_SOURCE_ID)
221
226
  })
222
227
 
223
228
  it('returns geojson-dynamic-{id} for a dynamic geojson source', () => {
@@ -225,7 +230,7 @@ describe('MapLibreDataset', () => {
225
230
  })
226
231
 
227
232
  it('returns geojson-{hash} for a static string geojson url', () => {
228
- expect(datasetRegistry.getDataset('ds-static-url').sourceId).toBe('geojson-1u4xay')
233
+ expect(datasetRegistry.getDataset('ds-static-url').sourceId).toBe(STATIC_GEOJSON_SOURCE_ID)
229
234
  })
230
235
 
231
236
  it('returns geojson-{id} for an object geojson source', () => {
@@ -328,43 +333,31 @@ describe('MapLibreDataset', () => {
328
333
  filter: ['in', ['get', 'category'], 'prehistoric']
329
334
  }
330
335
 
331
- it('returns a symbol layer spec with the correct shape', () => {
332
- expect(datasetRegistry.getDataset('historic-monuments-prehistoric').getSymbolSource('my-icon', null, null)).toEqual({
333
- ...prehistoricBase,
334
- layout: { visibility: 'visible', 'icon-image': 'my-icon', 'icon-anchor': 'center', 'icon-allow-overlap': true }
335
- })
336
- })
337
-
338
- it('uses the provided anchor when given', () => {
339
- expect(datasetRegistry.getDataset('historic-monuments-prehistoric').getSymbolSource('icon', [0.1, 0.9], null)).toEqual({
340
- ...prehistoricBase,
341
- layout: { visibility: 'visible', 'icon-image': 'icon', 'icon-anchor': 'bottom-left', 'icon-allow-overlap': true }
342
- })
343
- })
336
+ const ICON_LAYOUT = { 'icon-anchor': 'bottom', 'icon-offset': [0, 4.7] }
344
337
 
345
- it('falls back to symbolDef.anchor when no anchor is provided', () => {
346
- expect(datasetRegistry.getDataset('historic-monuments-prehistoric').getSymbolSource('icon', null, { anchor: [0.5, 0] })).toEqual({
338
+ it('returns a symbol layer spec with the correct shape', () => {
339
+ expect(datasetRegistry.getDataset('historic-monuments-prehistoric').getSymbolSource('my-icon', ICON_LAYOUT)).toEqual({
347
340
  ...prehistoricBase,
348
- layout: { visibility: 'visible', 'icon-image': 'icon', 'icon-anchor': 'top', 'icon-allow-overlap': true }
341
+ layout: { visibility: 'visible', 'icon-image': 'my-icon', ...ICON_LAYOUT, 'icon-allow-overlap': true }
349
342
  })
350
343
  })
351
344
 
352
345
  it('does not include a filter property when filter is null', () => {
353
- expect(datasetRegistry.getDataset('historic-monuments').getSymbolSource('icon', null, null)).toEqual({
346
+ expect(datasetRegistry.getDataset('historic-monuments').getSymbolSource('icon', ICON_LAYOUT)).toEqual({
354
347
  id: null,
355
348
  type: 'symbol',
356
349
  source: 'geojson-historic-monuments',
357
350
  'source-layer': undefined,
358
351
  minzoom: 10,
359
352
  maxzoom: 24,
360
- layout: { visibility: 'visible', 'icon-image': 'icon', 'icon-anchor': 'center', 'icon-allow-overlap': true }
353
+ layout: { visibility: 'visible', 'icon-image': 'icon', ...ICON_LAYOUT, 'icon-allow-overlap': true }
361
354
  })
362
355
  })
363
356
 
364
357
  it('includes a filter property when the dataset has a filter', () => {
365
- expect(datasetRegistry.getDataset('historic-monuments-prehistoric').getSymbolSource('icon', null, null)).toEqual({
358
+ expect(datasetRegistry.getDataset('historic-monuments-prehistoric').getSymbolSource('icon', ICON_LAYOUT)).toEqual({
366
359
  ...prehistoricBase,
367
- layout: { visibility: 'visible', 'icon-image': 'icon', 'icon-anchor': 'center', 'icon-allow-overlap': true }
360
+ layout: { visibility: 'visible', 'icon-image': 'icon', ...ICON_LAYOUT, 'icon-allow-overlap': true }
368
361
  })
369
362
  })
370
363
  })
@@ -395,7 +388,7 @@ describe('MapLibreDataset', () => {
395
388
  expect(datasetRegistry.getDataset('existing-fields').getFillSource({})).toEqual({
396
389
  id: 'existing-fields',
397
390
  type: 'fill',
398
- source: 'tiles-35m5lrb',
391
+ source: EXISTING_FIELDS_SOURCE_ID,
399
392
  'source-layer': 'field_parcels_wgs84',
400
393
  minzoom: 10,
401
394
  maxzoom: 24,
@@ -413,7 +406,7 @@ describe('MapLibreDataset', () => {
413
406
  .toEqual({
414
407
  id: 'hedge-control',
415
408
  type: 'line',
416
- source: 'tiles-35m5lrb',
409
+ source: EXISTING_FIELDS_SOURCE_ID,
417
410
  'source-layer': 'hedge_control',
418
411
  minzoom: 10,
419
412
  maxzoom: 24,
@@ -428,7 +421,7 @@ describe('MapLibreDataset', () => {
428
421
  .toEqual({
429
422
  id: 'existing-fields-stroke',
430
423
  type: 'line',
431
- source: 'tiles-35m5lrb',
424
+ source: EXISTING_FIELDS_SOURCE_ID,
432
425
  'source-layer': 'field_parcels_wgs84',
433
426
  minzoom: 10,
434
427
  maxzoom: 24,
@@ -1,78 +1,23 @@
1
1
  import Style from 'ol/style/Style.js'
2
- import Fill from 'ol/style/Fill.js'
3
2
  import Stroke from 'ol/style/Stroke.js'
4
3
  import { getValueForStyle } from '../../../../../src/utils/getValueForStyle.js'
5
- import { buildFilterEvaluator } from '../../../../../providers/beta/openlayers/src/utils/filterEvaluator.js'
6
4
 
7
5
  const DEFAULT_STROKE_WIDTH = 1
8
6
 
9
7
  /**
10
- * The Canvas-only escape hatch for genuinely crisp fill patterns, at any pixelRatio.
8
+ * The Canvas-only style for genuinely crisp fill patterns, at any pixelRatio.
11
9
  *
12
- * ol/style/flat's declarative fill-pattern-src/fill-pattern-size crops a pattern to its display
13
- * size rather than scaling it, so it can't hold more source detail than it shows on screen. But
14
- * ol/style/Fill also accepts a raw CanvasPattern as its colour, built directly from a
15
- * pixelRatio-scaled source image with no further transform — genuinely resolution-independent,
16
- * since OL's pattern coordinate space is physical-pixel-equivalent (confirmed empirically).
10
+ * ol/style/flat's declarative fill-pattern-src crops a pattern to its display size rather than
11
+ * scaling it, so it can't hold more source detail than it shows on screen. The OL provider instead
12
+ * caches each pattern as an ol/style/Fill whose colour is a CanvasPattern built from the
13
+ * pixelRatio-scaled image (see its addPatternsToMap/getPatternFill), and this builds a style
14
+ * function around that fill.
17
15
  *
18
16
  * Building actual ol/style/Style objects only works on Canvas — WebGLVector requires flat-style
19
17
  * JSON (compiled to GLSL ahead of time), so this module is Canvas-only. See layerBuilders.js's
20
18
  * resolveLayerStyle for where this is picked over the shared flatStyle path.
21
19
  */
22
20
 
23
- const crispPatternCache = new Map() // imageId (already pixelRatio-scoped, see patternRegistry) → Fill
24
-
25
- const cacheKey = (imageId) => imageId
26
-
27
- export const getCachedCrispPatternFill = (imageId) => crispPatternCache.get(cacheKey(imageId))
28
-
29
- const buildPatternFill = (imageData) => {
30
- const source = document.createElement('canvas')
31
- source.width = imageData.width
32
- source.height = imageData.height
33
- source.getContext('2d').putImageData(imageData, 0, 0)
34
- const scratch = document.createElement('canvas')
35
- const pattern = scratch.getContext('2d').createPattern(source, 'repeat')
36
- return new Fill({ color: pattern })
37
- }
38
-
39
- /**
40
- * Rasterise-once-cache-forever — a synchronous cache lookup at style-build time needs this
41
- * already resolved.
42
- * @param {Object} style - Dataset style with fillPattern* properties
43
- * @param {string} mapStyleId
44
- * @param {Object} patternRegistry
45
- * @param {number} pixelRatio - the map's current pixelRatio; re-called on resize
46
- * @returns {Promise<void>}
47
- */
48
- export const registerCrispCanvasPattern = async (style, mapStyleId, patternRegistry, pixelRatio) => {
49
- const imageId = patternRegistry.getPatternImageId(style, mapStyleId, pixelRatio)
50
- if (!imageId || crispPatternCache.has(cacheKey(imageId))) {
51
- return
52
- }
53
- const result = await patternRegistry.rasterisePatternImage(style, mapStyleId, pixelRatio)
54
- if (!result) {
55
- return
56
- }
57
- const key = cacheKey(result.imageId)
58
- if (!crispPatternCache.has(key)) {
59
- crispPatternCache.set(key, buildPatternFill(result.imageData))
60
- }
61
- }
62
-
63
- /** Register every pattern style in the given array, in parallel. */
64
- export const registerCrispCanvasPatterns = async (styleArray, mapStyleId, patternRegistry, pixelRatio) => {
65
- if (!styleArray.length) {
66
- return
67
- }
68
- await Promise.all(styleArray.map(style => registerCrispCanvasPattern(style, mapStyleId, patternRegistry, pixelRatio)))
69
- }
70
-
71
- /** Mainly for testing — clears every cached Fill. */
72
- export const clearCrispPatternCache = () => {
73
- crispPatternCache.clear()
74
- }
75
-
76
21
  const buildStroke = (registryDataset, mapStyleId) => {
77
22
  if (!registryDataset.hasStroke) {
78
23
  return undefined
@@ -86,16 +31,19 @@ const buildStroke = (registryDataset, mapStyleId) => {
86
31
 
87
32
  /**
88
33
  * Builds an OL style function for a Canvas-rendered, pattern-bearing dataset/sublayer — uses a
89
- * real CanvasPattern (see module doc), bypassing OpenLayersDataset.flatStyle for this one case.
34
+ * real CanvasPattern (see module doc), bypassing OpenLayersDataset.getFlatStyle for this one case.
90
35
  * @param {Object} registryDataset - an OpenLayersDataset with hasPattern true
91
- * @param {string} mapStyleId
92
- * @param {number} pixelRatio - must match what registerCrispCanvasPattern was last called with
93
- * @param {Object} patternRegistry
36
+ * @param {Object} context - the adapter's style context
37
+ * @param {string} context.mapStyleId
38
+ * @param {number} context.pixelRatio - the ratio the adapter last registered patterns at
39
+ * @param {Object} context.patternRegistry
40
+ * @param {Function} context.getPatternFill - the OL provider's registered pattern fill lookup
41
+ * @param {Function} context.buildFilterEvaluator - the OL provider's filter compiler
94
42
  * @returns {import('ol/style/Style.js').StyleFunction}
95
43
  */
96
- export const buildCanvasPatternStyle = (registryDataset, mapStyleId, pixelRatio, patternRegistry) => {
44
+ export const buildCanvasPatternStyle = (registryDataset, { mapStyleId, pixelRatio, patternRegistry, getPatternFill, buildFilterEvaluator }) => {
97
45
  const imageId = patternRegistry.getPatternImageId(registryDataset.style, mapStyleId, pixelRatio)
98
- const fill = imageId && getCachedCrispPatternFill(imageId)
46
+ const fill = imageId && getPatternFill(imageId)
99
47
  const style = fill ? new Style({ fill, stroke: buildStroke(registryDataset, mapStyleId) }) : undefined
100
48
  const matchesFilter = buildFilterEvaluator(registryDataset.filter)
101
49
 
@@ -1,16 +1,26 @@
1
1
  import Style from 'ol/style/Style.js'
2
2
  import Feature from 'ol/Feature.js'
3
3
  import Point from 'ol/geom/Point.js'
4
- import {
5
- registerCrispCanvasPattern,
6
- registerCrispCanvasPatterns,
7
- getCachedCrispPatternFill,
8
- clearCrispPatternCache,
9
- buildCanvasPatternStyle
10
- } from './canvasPatternStyle.js'
4
+ import { buildCanvasPatternStyle } from './canvasPatternStyle.js'
5
+ import { buildFilterEvaluator } from '../../../../../providers/beta/openlayers/src/utils/filterEvaluator.js'
6
+ import { PatternImageCache } from '../../../../../providers/beta/openlayers/src/utils/patternImages.js'
11
7
 
12
8
  const OUTDOOR = 'outdoor'
13
9
 
10
+ // Stands in for the OL provider's pattern fills
11
+ let patternImages
12
+
13
+ // The adapter's style context, with the OL provider's real filter compiler and pattern fills
14
+ const styleContext = (patternRegistry) => ({
15
+ mapStyleId: OUTDOOR,
16
+ pixelRatio: 1,
17
+ patternRegistry,
18
+ getPatternFill: (imageId) => patternImages.getFill(imageId),
19
+ buildFilterEvaluator
20
+ })
21
+
22
+ const registerPattern = (style, patternRegistry) => patternImages.registerPatterns([style], OUTDOOR, patternRegistry, 1)
23
+
14
24
  const makePatternRegistry = () => ({
15
25
  getPatternImageId: jest.fn((style, mapStyleId, pixelRatio) => `pattern-${style.fillPattern}-${mapStyleId}-${pixelRatio}`),
16
26
  rasterisePatternImage: jest.fn(async (style, mapStyleId, pixelRatio) => ({
@@ -19,115 +29,32 @@ const makePatternRegistry = () => ({
19
29
  }))
20
30
  })
21
31
 
22
- let createPatternCalls
23
32
  beforeEach(() => {
24
- clearCrispPatternCache()
25
- createPatternCalls = []
33
+ patternImages = new PatternImageCache()
26
34
  HTMLCanvasElement.prototype.getContext = jest.fn(function () {
27
35
  this._ctx ??= {
28
36
  putImageData: jest.fn(),
29
- createPattern: jest.fn((source, repetition) => {
30
- const pattern = { source, repetition }
31
- createPatternCalls.push(pattern)
32
- return pattern
33
- })
37
+ createPattern: jest.fn((source, repetition) => ({ source, repetition }))
34
38
  }
35
39
  return this._ctx
36
40
  })
37
41
  })
38
42
 
39
- describe('registerCrispCanvasPattern', () => {
40
- it('caches a Fill built from a real CanvasPattern for a valid pattern config', async () => {
41
- const patternRegistry = makePatternRegistry()
42
- const style = { fillPattern: 'dot' }
43
- await registerCrispCanvasPattern(style, OUTDOOR, patternRegistry, 2)
44
- const imageId = patternRegistry.getPatternImageId(style, OUTDOOR, 2)
45
- const fill = getCachedCrispPatternFill(imageId)
46
- expect(fill.getColor()).toEqual(expect.objectContaining({ repetition: 'repeat' }))
47
- })
48
-
49
- it('rasterises at the given pixelRatio, not a fixed constant', async () => {
50
- const patternRegistry = makePatternRegistry()
51
- await registerCrispCanvasPattern({ fillPattern: 'dot' }, OUTDOOR, patternRegistry, 3)
52
- expect(patternRegistry.rasterisePatternImage).toHaveBeenCalledWith({ fillPattern: 'dot' }, OUTDOOR, 3)
53
- })
54
-
55
- it('does nothing when getPatternImageId returns null', async () => {
56
- const patternRegistry = makePatternRegistry()
57
- patternRegistry.getPatternImageId.mockReturnValue(null)
58
- await registerCrispCanvasPattern({ fillPattern: 'unknown' }, OUTDOOR, patternRegistry, 1)
59
- expect(patternRegistry.rasterisePatternImage).not.toHaveBeenCalled()
60
- })
61
-
62
- it('does nothing when rasterisePatternImage returns null', async () => {
63
- const patternRegistry = makePatternRegistry()
64
- patternRegistry.rasterisePatternImage.mockResolvedValue(null)
65
- const style = { fillPattern: 'dot' }
66
- await registerCrispCanvasPattern(style, OUTDOOR, patternRegistry, 1)
67
- const imageId = patternRegistry.getPatternImageId(style, OUTDOOR, 1)
68
- expect(getCachedCrispPatternFill(imageId)).toBeUndefined()
69
- })
70
-
71
- it('skips rasterising again once cached for the same imageId', async () => {
72
- const patternRegistry = makePatternRegistry()
73
- const style = { fillPattern: 'dot' }
74
- await registerCrispCanvasPattern(style, OUTDOOR, patternRegistry, 1)
75
- await registerCrispCanvasPattern(style, OUTDOOR, patternRegistry, 1)
76
- expect(patternRegistry.rasterisePatternImage).toHaveBeenCalledTimes(1)
77
- })
78
-
79
- it('building the same pattern config twice concurrently still leaves exactly one cached Fill', async () => {
80
- // registerCrispCanvasPatterns (plural) can be handed two different dataset styles that
81
- // happen to resolve to the same imageId (identical pattern/colours) — both start rasterising
82
- // before either has cached its result, so the second one back must skip re-caching rather
83
- // than clobbering the first.
84
- const patternRegistry = makePatternRegistry()
85
- const style = { fillPattern: 'dot' }
86
- await registerCrispCanvasPatterns([style, { ...style }], OUTDOOR, patternRegistry, 1)
87
- const imageId = patternRegistry.getPatternImageId(style, OUTDOOR, 1)
88
- expect(getCachedCrispPatternFill(imageId)).toBeDefined()
89
- })
90
-
91
- it('caches separately per pixelRatio, since imageId is pixelRatio-scoped', async () => {
92
- const patternRegistry = makePatternRegistry()
93
- const style = { fillPattern: 'dot' }
94
- await registerCrispCanvasPattern(style, OUTDOOR, patternRegistry, 1)
95
- await registerCrispCanvasPattern(style, OUTDOOR, patternRegistry, 4)
96
- expect(patternRegistry.rasterisePatternImage).toHaveBeenCalledTimes(2)
97
- expect(getCachedCrispPatternFill(patternRegistry.getPatternImageId(style, OUTDOOR, 1))).toBeDefined()
98
- expect(getCachedCrispPatternFill(patternRegistry.getPatternImageId(style, OUTDOOR, 4))).toBeDefined()
99
- })
100
- })
101
-
102
- describe('registerCrispCanvasPatterns', () => {
103
- it('registers every config in parallel', async () => {
104
- const patternRegistry = makePatternRegistry()
105
- await registerCrispCanvasPatterns([{ fillPattern: 'dot' }, { fillPattern: 'stripes' }], OUTDOOR, patternRegistry, 1)
106
- expect(patternRegistry.rasterisePatternImage).toHaveBeenCalledTimes(2)
107
- })
108
-
109
- it('is a no-op for an empty array', async () => {
110
- const patternRegistry = makePatternRegistry()
111
- await registerCrispCanvasPatterns([], OUTDOOR, patternRegistry, 1)
112
- expect(patternRegistry.rasterisePatternImage).not.toHaveBeenCalled()
113
- })
114
- })
115
-
116
43
  describe('buildCanvasPatternStyle', () => {
117
44
  const feature = (props) => new Feature({ geometry: new Point([0, 0]), ...props })
118
45
 
119
46
  it('returns undefined when the pattern has not been registered yet', () => {
120
47
  const patternRegistry = makePatternRegistry()
121
48
  const registryDataset = { style: { fillPattern: 'dot' }, filter: null }
122
- const styleFn = buildCanvasPatternStyle(registryDataset, OUTDOOR, 1, patternRegistry)
49
+ const styleFn = buildCanvasPatternStyle(registryDataset, styleContext(patternRegistry))
123
50
  expect(styleFn(feature())).toBeUndefined()
124
51
  })
125
52
 
126
53
  it('returns a Style with the cached pattern Fill once registered, for an unfiltered dataset', async () => {
127
54
  const patternRegistry = makePatternRegistry()
128
- await registerCrispCanvasPattern({ fillPattern: 'dot' }, OUTDOOR, patternRegistry, 1)
55
+ await registerPattern({ fillPattern: 'dot' }, patternRegistry)
129
56
  const registryDataset = { style: { fillPattern: 'dot' }, filter: null, hasStroke: false }
130
- const styleFn = buildCanvasPatternStyle(registryDataset, OUTDOOR, 1, patternRegistry)
57
+ const styleFn = buildCanvasPatternStyle(registryDataset, styleContext(patternRegistry))
131
58
  const style = styleFn(feature())
132
59
  expect(style).toBeInstanceOf(Style)
133
60
  expect(style.getFill().getColor()).toEqual(expect.objectContaining({ repetition: 'repeat' }))
@@ -135,13 +62,13 @@ describe('buildCanvasPatternStyle', () => {
135
62
 
136
63
  it('includes a Stroke when the dataset has one', async () => {
137
64
  const patternRegistry = makePatternRegistry()
138
- await registerCrispCanvasPattern({ fillPattern: 'dot' }, OUTDOOR, patternRegistry, 1)
65
+ await registerPattern({ fillPattern: 'dot' }, patternRegistry)
139
66
  const registryDataset = {
140
67
  style: { fillPattern: 'dot', stroke: '#ff0000', strokeWidth: 3 },
141
68
  filter: null,
142
69
  hasStroke: true
143
70
  }
144
- const styleFn = buildCanvasPatternStyle(registryDataset, OUTDOOR, 1, patternRegistry)
71
+ const styleFn = buildCanvasPatternStyle(registryDataset, styleContext(patternRegistry))
145
72
  const style = styleFn(feature())
146
73
  expect(style.getStroke().getColor()).toBe('#ff0000')
147
74
  expect(style.getStroke().getWidth()).toBe(3)
@@ -149,50 +76,50 @@ describe('buildCanvasPatternStyle', () => {
149
76
 
150
77
  it('defaults the Stroke width to 1 when strokeWidth is not set', async () => {
151
78
  const patternRegistry = makePatternRegistry()
152
- await registerCrispCanvasPattern({ fillPattern: 'dot' }, OUTDOOR, patternRegistry, 1)
79
+ await registerPattern({ fillPattern: 'dot' }, patternRegistry)
153
80
  const registryDataset = {
154
81
  style: { fillPattern: 'dot', stroke: '#ff0000' },
155
82
  filter: null,
156
83
  hasStroke: true
157
84
  }
158
- const styleFn = buildCanvasPatternStyle(registryDataset, OUTDOOR, 1, patternRegistry)
85
+ const styleFn = buildCanvasPatternStyle(registryDataset, styleContext(patternRegistry))
159
86
  expect(styleFn(feature()).getStroke().getWidth()).toBe(1)
160
87
  })
161
88
 
162
89
  it('evaluates a filter correctly for a feature with no geometry', async () => {
163
90
  const patternRegistry = makePatternRegistry()
164
- await registerCrispCanvasPattern({ fillPattern: 'dot' }, OUTDOOR, patternRegistry, 1)
91
+ await registerPattern({ fillPattern: 'dot' }, patternRegistry)
165
92
  const registryDataset = {
166
93
  style: { fillPattern: 'dot' },
167
94
  filter: ['==', ['get', 'category'], 'a'],
168
95
  hasStroke: false
169
96
  }
170
- const styleFn = buildCanvasPatternStyle(registryDataset, OUTDOOR, 1, patternRegistry)
97
+ const styleFn = buildCanvasPatternStyle(registryDataset, styleContext(patternRegistry))
171
98
  const featureWithNoGeometry = new Feature({ category: 'a' })
172
99
  expect(styleFn(featureWithNoGeometry)).toBeInstanceOf(Style)
173
100
  })
174
101
 
175
102
  it('hides a feature that does not match the dataset filter', async () => {
176
103
  const patternRegistry = makePatternRegistry()
177
- await registerCrispCanvasPattern({ fillPattern: 'dot' }, OUTDOOR, patternRegistry, 1)
104
+ await registerPattern({ fillPattern: 'dot' }, patternRegistry)
178
105
  const registryDataset = {
179
106
  style: { fillPattern: 'dot' },
180
107
  filter: ['==', ['get', 'category'], 'a'],
181
108
  hasStroke: false
182
109
  }
183
- const styleFn = buildCanvasPatternStyle(registryDataset, OUTDOOR, 1, patternRegistry)
110
+ const styleFn = buildCanvasPatternStyle(registryDataset, styleContext(patternRegistry))
184
111
  expect(styleFn(feature({ category: 'b' }))).toBeUndefined()
185
112
  })
186
113
 
187
114
  it('shows a feature that matches the dataset filter', async () => {
188
115
  const patternRegistry = makePatternRegistry()
189
- await registerCrispCanvasPattern({ fillPattern: 'dot' }, OUTDOOR, patternRegistry, 1)
116
+ await registerPattern({ fillPattern: 'dot' }, patternRegistry)
190
117
  const registryDataset = {
191
118
  style: { fillPattern: 'dot' },
192
119
  filter: ['==', ['get', 'category'], 'a'],
193
120
  hasStroke: false
194
121
  }
195
- const styleFn = buildCanvasPatternStyle(registryDataset, OUTDOOR, 1, patternRegistry)
122
+ const styleFn = buildCanvasPatternStyle(registryDataset, styleContext(patternRegistry))
196
123
  expect(styleFn(feature({ category: 'a' }))).toBeInstanceOf(Style)
197
124
  })
198
125
  })
@@ -7,13 +7,11 @@ import MVT from 'ol/format/MVT.js'
7
7
  import Feature from 'ol/Feature.js'
8
8
  import TileGrid from 'ol/tilegrid/TileGrid.js'
9
9
  import { logger } from '../../../../../src/services/logger.js'
10
- import { BNG_CRS } from '../../../../../providers/beta/openlayers/src/utils/bngProjection.js'
11
10
  import { buildCanvasPatternStyle } from './canvasPatternStyle.js'
12
11
 
13
- // Data and map both use BNG, so no coordinate transformation is needed. Importing bngProjection.js
14
- // (rather than just the string) also registers EPSG:27700 with proj4/OL, needed for
15
- // VectorTileSource's projection option below to resolve to a real projection.
16
- const PROJECTION = BNG_CRS
12
+ // Data and map both use British National Grid, so no coordinate transformation is needed. The
13
+ // OpenLayers provider registers the projection with OL.
14
+ const PROJECTION = 'EPSG:27700'
17
15
  const format = new GeoJSON({ dataProjection: PROJECTION, featureProjection: PROJECTION })
18
16
 
19
17
  // The Mapbox style spec's minzoom/maxzoom are inclusive-min, exclusive-max; OL's own semantics
@@ -135,18 +133,18 @@ export const createDatasetSource = (registryDataset) => {
135
133
 
136
134
  /**
137
135
  * The style value to hand to layer.setStyle()/the layer constructor: registryDataset's own
138
- * flatStyle, except for a pattern fill, which needs the genuinely crisp, CanvasPattern-based
136
+ * flat style, except for a pattern fill, which needs the genuinely crisp, CanvasPattern-based
139
137
  * style function from canvasPatternStyle.js instead — see that module's doc for why flat-style's
140
138
  * fill-pattern-src can't achieve this.
141
139
  * @param {Object} registryDataset - an OpenLayersDataset
142
- * @param {{ mapStyleId: string, pixelRatio: number, patternRegistry: Object }} context
140
+ * @param {{ mapStyleId: string, pixelRatio: number, patternRegistry: Object, symbolRegistry: Object, getSymbolDataUri: Function, getPatternFill: Function, buildFilterEvaluator: Function }} context
143
141
  * @returns {Object|Array<Object>|import('ol/style/Style.js').StyleFunction}
144
142
  */
145
- export const resolveLayerStyle = (registryDataset, { mapStyleId, pixelRatio, patternRegistry } = {}) => {
143
+ export const resolveLayerStyle = (registryDataset, context = {}) => {
146
144
  if (registryDataset.hasPattern) {
147
- return buildCanvasPatternStyle(registryDataset, mapStyleId, pixelRatio, patternRegistry)
145
+ return buildCanvasPatternStyle(registryDataset, context)
148
146
  }
149
- return registryDataset.flatStyle
147
+ return registryDataset.getFlatStyle(context)
150
148
  }
151
149
 
152
150
  /**
@@ -155,7 +153,7 @@ export const resolveLayerStyle = (registryDataset, { mapStyleId, pixelRatio, pat
155
153
  * style pipeline (see resolveLayerStyle).
156
154
  * @param {Object} registryDataset - an OpenLayersDataset
157
155
  * @param {import('ol/source/Vector.js').default} olSource
158
- * @param {{ mapStyleId: string, pixelRatio: number, patternRegistry: Object }} context
156
+ * @param {{ mapStyleId: string, pixelRatio: number, patternRegistry: Object, symbolRegistry: Object, getSymbolDataUri: Function, getPatternFill: Function, buildFilterEvaluator: Function }} context
159
157
  * @returns {import('ol/layer/Layer.js').default}
160
158
  */
161
159
  export const createDatasetLayer = (registryDataset, olSource, context) => {
@@ -197,9 +195,9 @@ export const createDatasetLayer = (registryDataset, olSource, context) => {
197
195
  layer.set('filter', registryDataset.filter)
198
196
  }
199
197
  // Lets highlightFeatures.js build a select/active highlight for a dataset symbol point —
200
- // see OpenLayersDataset.symbolMeta's doc.
198
+ // see OpenLayersDataset.getSymbolMeta's doc.
201
199
  if (registryDataset.hasSymbol) {
202
- layer.set('symbolMeta', registryDataset.symbolMeta)
200
+ layer.set('symbolMeta', registryDataset.getSymbolMeta(context))
203
201
  }
204
202
  return layer
205
203
  }
@@ -4,6 +4,8 @@ import VectorTileSource from 'ol/source/VectorTile.js'
4
4
  import VectorTileLayer from 'ol/layer/VectorTile.js'
5
5
  import { createDatasetSource, createDatasetLayer, readGeoJSONFeatures } from './layerBuilders.js'
6
6
  import { logger } from '../../../../../src/services/logger.js'
7
+ // The OL provider registers British National Grid when it loads
8
+ import '../../../../../providers/beta/openlayers/src/utils/bngProjection.js'
7
9
 
8
10
  jest.mock('../../../../../src/services/logger.js', () => ({ logger: { warn: jest.fn() } }))
9
11
 
@@ -140,7 +142,7 @@ describe('createDatasetLayer', () => {
140
142
  // canvas 2D context, which jsdom returns null for without the `canvas` npm package. Hex
141
143
  // colours take OL's fast lane and skip that path entirely (see ol/color.js#parseRgba).
142
144
  // Named colours work fine in a real browser; this is a test-environment gap only.
143
- const registryDataset = { flatStyle: { 'fill-color': '#0000ff' }, opacity: 0.5, visibility: 'visible', minZoom: 1, maxZoom: 10, hasPattern: false }
145
+ const registryDataset = { getFlatStyle: () => ({ 'fill-color': '#0000ff' }), opacity: 0.5, visibility: 'visible', minZoom: 1, maxZoom: 10, hasPattern: false }
144
146
 
145
147
  it('builds a Canvas VectorLayer', () => {
146
148
  const layer = createDatasetLayer(registryDataset, new VectorSource())
@@ -203,13 +205,19 @@ describe('createDatasetLayer', () => {
203
205
  })
204
206
 
205
207
  it('gives a symbol dataset a higher zIndex, so it always renders above fill/stroke layers', () => {
206
- const layer = createDatasetLayer({ ...registryDataset, hasSymbol: true }, new VectorSource())
208
+ const layer = createDatasetLayer({ ...registryDataset, hasSymbol: true, getSymbolMeta: () => null }, new VectorSource())
207
209
  expect(layer.getZIndex()).toBeGreaterThan(0)
208
210
  })
211
+
212
+ it('tags a symbol layer with its symbolMeta at the context pixelRatio, for highlightFeatures.js', () => {
213
+ const getSymbolMeta = jest.fn(({ pixelRatio }) => ({ imageId: 'img', anchor: [0.5, 0.5], pixelRatio }))
214
+ const layer = createDatasetLayer({ ...registryDataset, hasSymbol: true, getSymbolMeta }, new VectorSource(), { pixelRatio: 3 })
215
+ expect(layer.get('symbolMeta')).toEqual({ imageId: 'img', anchor: [0.5, 0.5], pixelRatio: 3 })
216
+ })
209
217
  })
210
218
 
211
219
  describe('tile-backed dataset', () => {
212
- const tileRegistryDataset = { flatStyle: { 'stroke-color': '#0000ff' }, opacity: 1, visibility: 'visible', hasPattern: false, id: 'existing-fields' }
220
+ const tileRegistryDataset = { getFlatStyle: () => ({ 'stroke-color': '#0000ff' }), opacity: 1, visibility: 'visible', hasPattern: false, id: 'existing-fields' }
213
221
  // A real VectorTileSource, built the same way createDatasetSource does — a bare
214
222
  // `new VectorTileSource()` throws without a resolved projection.
215
223
  const tileSource = () => createDatasetSource({ id: 'existing-fields', idStrategy: null, source: { type: 'vector', tiles: ['https://example.com/{z}/{x}/{y}'] } })