Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/api/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const center = context.mapProvider.getCenter()
context.mapProvider.setView({ zoom: 10 })
```

#### `mapProvider.registerPatterns(patternConfigs, mapStyleId, patternRegistry)`
#### `mapProvider.addPatternsToMap(patternConfigs, mapStyleId, patternRegistry)`

Rasterises and registers pattern fill images with the map engine. Plugin layer adapters call this instead of importing provider internals directly, keeping cross-package boundaries clean.

Expand All @@ -56,12 +56,12 @@ Rasterises and registers pattern fill images with the map engine. Plugin layer a

```js
// In a plugin's MapLibre layer adapter
await mapProvider.registerPatterns(getPatternConfigs(datasets, patternRegistry), mapStyleId, patternRegistry)
await mapProvider.addPatternsToMap(getPatternConfigs(datasets, patternRegistry), mapStyleId, patternRegistry)
```

---

#### `mapProvider.registerSymbols(symbolConfigs, mapStyleId, symbolRegistry)`
#### `mapProvider.addSymbolsToMap(symbolConfigs, mapStyleId, symbolRegistry)`

Rasterises and registers symbol images with the map engine. Plugin layer adapters call this instead of importing provider internals directly, keeping cross-package boundaries clean.

Expand All @@ -71,7 +71,7 @@ Rasterises and registers symbol images with the map engine. Plugin layer adapter

```js
// In a plugin's MapLibre layer adapter
await mapProvider.registerSymbols(getSymbolConfigs(datasets), mapStyleId, symbolRegistry)
await mapProvider.addSymbolsToMap(getSymbolConfigs(datasets), mapStyleId, symbolRegistry)
```

---
Expand Down
22 changes: 11 additions & 11 deletions plugins/beta/datasets/src/adapters/maplibre/maplibreLayerAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { scaleFactor } from '../../../../../../src/config/appConfig.js'
* - Style-change recovery (re-adding layers after basemap swap)
*
* Symbol image rasterisation is delegated to the map provider via
* `mapProvider.registerSymbols()`, keeping this adapter free of provider internals.
* `mapProvider.addSymbolsToMap()`, keeping this adapter free of provider internals.
*/
export default class MaplibreLayerAdapter {
/**
Expand Down Expand Up @@ -47,8 +47,8 @@ export default class MaplibreLayerAdapter {
const mapStyleId = mapStyle.id
const pixelRatio = this._pixelRatio
await Promise.all([
this._mapProvider.registerPatterns(getPatternConfigs(datasets, this._patternRegistry), mapStyleId, this._patternRegistry),
this._mapProvider.registerSymbols(getSymbolConfigs(datasets), mapStyle, this._symbolRegistry)
this._mapProvider.addPatternsToMap(getPatternConfigs(datasets, this._patternRegistry), mapStyleId, this._patternRegistry),
this._mapProvider.addSymbolsToMap(getSymbolConfigs(datasets), mapStyle, this._symbolRegistry)
])
this._symbolLayerIds.clear()
datasets.forEach(dataset => this._addLayers(dataset, mapStyle, pixelRatio))
Expand Down Expand Up @@ -92,8 +92,8 @@ export default class MaplibreLayerAdapter {
const newStyleId = newMapStyle.id
const pixelRatio = this._pixelRatio
await Promise.all([
this._mapProvider.registerPatterns(getPatternConfigs(datasets, this._patternRegistry), newStyleId, this._patternRegistry),
this._mapProvider.registerSymbols(getSymbolConfigs(datasets), newMapStyle, this._symbolRegistry)
this._mapProvider.addPatternsToMap(getPatternConfigs(datasets, this._patternRegistry), newStyleId, this._patternRegistry),
this._mapProvider.addSymbolsToMap(getSymbolConfigs(datasets), newMapStyle, this._symbolRegistry)
])
this._symbolLayerIds.clear()
datasets.forEach(dataset => this._addLayers(dataset, newMapStyle, pixelRatio))
Expand Down Expand Up @@ -121,8 +121,8 @@ export default class MaplibreLayerAdapter {
async onSizeChange (datasets, mapStyle) {
const pixelRatio = this._pixelRatio
await Promise.all([
this._mapProvider.registerSymbols(getSymbolConfigs(datasets), mapStyle, this._symbolRegistry),
this._mapProvider.registerPatterns(getPatternConfigs(datasets, this._patternRegistry), mapStyle.id, this._patternRegistry)
this._mapProvider.addSymbolsToMap(getSymbolConfigs(datasets), mapStyle, this._symbolRegistry),
this._mapProvider.addPatternsToMap(getPatternConfigs(datasets, this._patternRegistry), mapStyle.id, this._patternRegistry)
])
datasets.forEach(dataset => {
getAllLayerIds(dataset).forEach(layerId => {
Expand Down Expand Up @@ -279,8 +279,8 @@ export default class MaplibreLayerAdapter {
this._symbolLayerIds.delete(layerId)
})
await Promise.all([
this._mapProvider.registerPatterns(getPatternConfigs([dataset], this._patternRegistry), mapStyleId, this._patternRegistry),
this._mapProvider.registerSymbols(getSymbolConfigs([dataset]), mapStyle, this._symbolRegistry)
this._mapProvider.addPatternsToMap(getPatternConfigs([dataset], this._patternRegistry), mapStyleId, this._patternRegistry),
this._mapProvider.addSymbolsToMap(getSymbolConfigs([dataset]), mapStyle, this._symbolRegistry)
])
this._addLayers(dataset, mapStyle, pixelRatio)
}
Expand All @@ -307,8 +307,8 @@ export default class MaplibreLayerAdapter {
return
}
await Promise.all([
this._mapProvider.registerPatterns(getPatternConfigs([dataset], this._patternRegistry), mapStyleId, this._patternRegistry),
this._mapProvider.registerSymbols(getSymbolConfigs([dataset]), mapStyle, this._symbolRegistry)
this._mapProvider.addPatternsToMap(getPatternConfigs([dataset], this._patternRegistry), mapStyleId, this._patternRegistry),
this._mapProvider.addSymbolsToMap(getSymbolConfigs([dataset]), mapStyle, this._symbolRegistry)
])
const sourceId = this._datasetSourceMap.get(dataset.id)
const sourceLayer = dataset.tiles?.length ? dataset.sourceLayer : undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export { hasPattern, getPatternInnerContent, getPatternImageId, getKeyPatternPat

/**
* Returns a flat list of datasets and merged sublayers that require pattern images.
* Handles sublayer merging so callers (e.g. mapProvider.registerPatterns) receive ready-to-use configs.
* Handles sublayer merging so callers (e.g. mapProvider.addPatternsToMap) receive ready-to-use configs.
*
* @param {Object[]} datasets
* @param {Object} patternRegistry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export { anchorToMaplibre, getSymbolImageId } from '../../../../../../providers/

/**
* Returns a flat list of datasets and merged sublayers that require symbol images.
* Handles sublayer merging so callers (e.g. mapProvider.registerSymbols) receive ready-to-use configs.
* Handles sublayer merging so callers (e.g. mapProvider.addSymbolsToMap) receive ready-to-use configs.
*
* @param {Object[]} datasets
* @returns {Object[]}
Expand Down
12 changes: 6 additions & 6 deletions providers/maplibre/src/maplibreProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { createMapLabelNavigator } from './utils/labels.js'
import { updateHighlightedFeatures } from './utils/highlightFeatures.js'
import { queryFeatures } from './utils/queryFeatures.js'
import { setupHoverCursor } from './utils/hoverCursor.js'
import { registerSymbols } from './utils/symbolImages.js'
import { registerPatterns } from './utils/patternImages.js'
import { addSymbolsToMap } from './utils/symbolImages.js'
import { addPatternsToMap } from './utils/patternImages.js'

/**
* MapLibre GL JS implementation of the MapProvider interface.
Expand Down Expand Up @@ -316,9 +316,9 @@ export default class MapLibreProvider {
* @param {Object} symbolRegistry
* @returns {Promise<void>}
*/
async registerSymbols (symbolConfigs, mapStyle, symbolRegistry) {
async addSymbolsToMap (symbolConfigs, mapStyle, symbolRegistry) {
const pixelRatio = (this.map.getPixelRatio() || 1) * (scaleFactor[this.mapSize] || 1)
return registerSymbols(this.map, symbolConfigs, mapStyle, symbolRegistry, pixelRatio)
return addSymbolsToMap(this.map, symbolConfigs, mapStyle, symbolRegistry, pixelRatio)
}

/**
Expand All @@ -332,9 +332,9 @@ export default class MapLibreProvider {
* @param {Object} patternRegistry
* @returns {Promise<void>}
*/
async registerPatterns (patternConfigs, mapStyleId, patternRegistry) {
async addPatternsToMap (patternConfigs, mapStyleId, patternRegistry) {
const pixelRatio = (this.map.getPixelRatio() || 1) * (scaleFactor[this.mapSize] || 1)
return registerPatterns(this.map, patternConfigs, mapStyleId, patternRegistry, pixelRatio)
return addPatternsToMap(this.map, patternConfigs, mapStyleId, patternRegistry, pixelRatio)
}

// ==========================
Expand Down
44 changes: 22 additions & 22 deletions providers/maplibre/src/maplibreProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { attachAppEvents } from './appEvents.js'
import { createMapLabelNavigator } from './utils/labels.js'
import { updateHighlightedFeatures } from './utils/highlightFeatures.js'
import { queryFeatures } from './utils/queryFeatures.js'
import { registerSymbols } from './utils/symbolImages.js'
import { registerPatterns } from './utils/patternImages.js'
import { addSymbolsToMap } from './utils/symbolImages.js'
import { addPatternsToMap } from './utils/patternImages.js'
import { getAreaDimensions, getCardinalMove, getResolution, getPaddedBounds, isGeometryObscured } from './utils/spatial.js'

jest.mock('./defaults.js', () => ({
Expand Down Expand Up @@ -35,8 +35,8 @@ jest.mock('./utils/labels.js', () => ({
}))
jest.mock('./utils/highlightFeatures.js', () => ({ updateHighlightedFeatures: jest.fn(() => []) }))
jest.mock('./utils/queryFeatures.js', () => ({ queryFeatures: jest.fn(() => []) }))
jest.mock('./utils/symbolImages.js', () => ({ registerSymbols: jest.fn(() => Promise.resolve()) }))
jest.mock('./utils/patternImages.js', () => ({ registerPatterns: jest.fn(() => Promise.resolve()) }))
jest.mock('./utils/symbolImages.js', () => ({ addSymbolsToMap: jest.fn(() => Promise.resolve()) }))
jest.mock('./utils/patternImages.js', () => ({ addPatternsToMap: jest.fn(() => Promise.resolve()) }))

describe('MapLibreProvider', () => {
let map, eventBus, maplibreModule, loadCallback
Expand Down Expand Up @@ -250,63 +250,63 @@ describe('MapLibreProvider', () => {
})
})

test('registerSymbols delegates to utility with map instance', async () => {
test('addSymbolsToMap delegates to utility with map instance', async () => {
const p = makeProvider()
await doInitMap(p)
const configs = [{ symbol: 'pin' }]
const mapStyle = { id: 'test', selectedColor: '#0b0c0c' }
const registry = {}
await p.registerSymbols(configs, mapStyle, registry)
expect(registerSymbols).toHaveBeenCalledWith(map, configs, mapStyle, registry, expect.any(Number))
await p.addSymbolsToMap(configs, mapStyle, registry)
expect(addSymbolsToMap).toHaveBeenCalledWith(map, configs, mapStyle, registry, expect.any(Number))
})

test('registerSymbols computes pixelRatio from getPixelRatio and mapSize scale factor', async () => {
test('addSymbolsToMap computes pixelRatio from getPixelRatio and mapSize scale factor', async () => {
const p = makeProvider()
await doInitMap(p)
map.getPixelRatio.mockReturnValue(2)
p.mapSize = 'medium' // scaleFactor['medium'] = 1.5
const registry = {}
await p.registerSymbols([], { id: 'test' }, registry)
expect(registerSymbols).toHaveBeenCalledWith(map, [], { id: 'test' }, registry, 3) // 2 * 1.5
await p.addSymbolsToMap([], { id: 'test' }, registry)
expect(addSymbolsToMap).toHaveBeenCalledWith(map, [], { id: 'test' }, registry, 3) // 2 * 1.5
})

test('registerSymbols falls back to pixelRatio 1 when getPixelRatio returns 0', async () => {
test('addSymbolsToMap falls back to pixelRatio 1 when getPixelRatio returns 0', async () => {
const p = makeProvider()
await doInitMap(p)
map.getPixelRatio.mockReturnValue(0)
p.mapSize = 'small' // scaleFactor['small'] = 1
const registry = {}
await p.registerSymbols([], { id: 'test' }, registry)
expect(registerSymbols).toHaveBeenCalledWith(map, [], { id: 'test' }, registry, 1) // (0 || 1) * 1
await p.addSymbolsToMap([], { id: 'test' }, registry)
expect(addSymbolsToMap).toHaveBeenCalledWith(map, [], { id: 'test' }, registry, 1) // (0 || 1) * 1
})

test('registerPatterns delegates to utility with map instance and pixelRatio', async () => {
test('addPatternsToMap delegates to utility with map instance and pixelRatio', async () => {
const p = makeProvider()
await doInitMap(p)
const configs = [{ fillPattern: 'dot' }]
const registry = {}
await p.registerPatterns(configs, 'test', registry)
expect(registerPatterns).toHaveBeenCalledWith(map, configs, 'test', registry, 1) // getPixelRatio()=1, mapSize unset → 1*1
await p.addPatternsToMap(configs, 'test', registry)
expect(addPatternsToMap).toHaveBeenCalledWith(map, configs, 'test', registry, 1) // getPixelRatio()=1, mapSize unset → 1*1
})

test('registerPatterns computes pixelRatio from getPixelRatio and mapSize scale factor', async () => {
test('addPatternsToMap computes pixelRatio from getPixelRatio and mapSize scale factor', async () => {
const p = makeProvider()
await doInitMap(p)
map.getPixelRatio.mockReturnValue(2)
p.mapSize = 'medium' // scaleFactor['medium'] = 1.5
const registry = {}
await p.registerPatterns([], 'test', registry)
expect(registerPatterns).toHaveBeenCalledWith(map, [], 'test', registry, 3) // 2 * 1.5
await p.addPatternsToMap([], 'test', registry)
expect(addPatternsToMap).toHaveBeenCalledWith(map, [], 'test', registry, 3) // 2 * 1.5
})

test('registerPatterns falls back to pixelRatio 1 when getPixelRatio returns 0', async () => {
test('addPatternsToMap falls back to pixelRatio 1 when getPixelRatio returns 0', async () => {
const p = makeProvider()
await doInitMap(p)
map.getPixelRatio.mockReturnValue(0)
p.mapSize = 'small' // scaleFactor['small'] = 1
const registry = {}
await p.registerPatterns([], 'test', registry)
expect(registerPatterns).toHaveBeenCalledWith(map, [], 'test', registry, 1) // (0 || 1) * 1
await p.addPatternsToMap([], 'test', registry)
expect(addPatternsToMap).toHaveBeenCalledWith(map, [], 'test', registry, 1) // (0 || 1) * 1
})

describe('setHoverCursor', () => {
Expand Down
8 changes: 4 additions & 4 deletions providers/maplibre/src/utils/patternImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ const rasterisePattern = async (dataset, mapStyleId, patternRegistry, pixelRatio
* (see `getPatternConfigs` in the datasets plugin adapter).
*
* @param {Object} map - MapLibre map instance
* @param {Object[]} patternConfigs - Flat list of datasets/merged-sublayers with a pattern config
* @param {Object[]} styleArray - Flat list of datasets/merged-sublayers with a pattern config
* @param {string} mapStyleId
* @param {Object} patternRegistry
* @param {number} pixelRatio
* @returns {Promise<void>}
*/
export const registerPatterns = async (map, patternConfigs, mapStyleId, patternRegistry, pixelRatio) => {
if (!patternConfigs.length) {
export const addPatternsToMap = async (map, styleArray, mapStyleId, patternRegistry, pixelRatio) => {
if (!styleArray.length) {
return
}

const effectiveRatio = Math.max(PATTERN_MIN_PIXEL_RATIO, pixelRatio)
await Promise.all(patternConfigs.map(async (config) => {
await Promise.all(styleArray.map(async (config) => {
const imageId = getPatternImageId(config, mapStyleId, patternRegistry, pixelRatio)
if (!imageId || map.hasImage(imageId)) {
return
Expand Down
Loading
Loading