Skip to content

Commit 534fa16

Browse files
Natalia Kowalczykmelitele
authored andcommitted
global-state expression support in layout properties
complements `global-state` expression support in filter and paint properties maplibre#5613
1 parent c9493a1 commit 534fa16

File tree

4 files changed

+106
-7
lines changed

4 files changed

+106
-7
lines changed

src/source/worker_tile.test.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import {type WorkerTileParameters} from './worker_source';
77
import {type VectorTile} from '@mapbox/vector-tile';
88
import {SubdivisionGranularitySetting} from '../render/subdivision_granularity_settings';
99

10-
function createWorkerTile() {
10+
function createWorkerTile(params?: {globalState?: Record<string, any>}): WorkerTile {
1111
return new WorkerTile({
1212
uid: '',
1313
zoom: 0,
1414
maxZoom: 20,
1515
tileSize: 512,
1616
source: 'source',
1717
tileID: new OverscaledTileID(1, 0, 1, 1, 1),
18-
overscaling: 1
18+
overscaling: 1,
19+
globalState: params?.globalState
1920
} as any as WorkerTileParameters);
2021
}
2122

@@ -27,6 +28,14 @@ function createWrapper() {
2728
} as any as Feature]);
2829
}
2930

31+
function createLineWrapper() {
32+
return new GeoJSONWrapper([{
33+
type: 2,
34+
geometry: [[0, 0], [1, 1]],
35+
tags: {}
36+
} as any as Feature]);
37+
}
38+
3039
describe('worker tile', () => {
3140
test('WorkerTile.parse', async () => {
3241
const layerIndex = new StyleLayerIndex([{
@@ -40,6 +49,40 @@ describe('worker tile', () => {
4049
expect(result.buckets[0]).toBeTruthy();
4150
});
4251

52+
test('WorkerTile.parse layer with layout property', async () => {
53+
const layerIndex = new StyleLayerIndex([{
54+
id: 'test',
55+
source: 'source',
56+
type: 'line',
57+
layout: {
58+
'line-join': 'bevel'
59+
}
60+
}]);
61+
62+
const tile = createWorkerTile();
63+
const result = await tile.parse(createLineWrapper(), layerIndex, [], {} as any, SubdivisionGranularitySetting.noSubdivision);
64+
expect(result.buckets[0]).toBeTruthy();
65+
expect(result.buckets[0].layers[0].layout._values['line-join'].value.value).toBe('bevel');
66+
});
67+
68+
test('WorkerTile.parse layer with layout property using global-state', async () => {
69+
const layerIndex = new StyleLayerIndex([{
70+
id: 'test',
71+
source: 'source',
72+
type: 'line',
73+
layout: {
74+
'line-join': ['global-state', 'test']
75+
}
76+
}]);
77+
78+
const tile = createWorkerTile({
79+
globalState: {test: 'bevel'}
80+
});
81+
const result = await tile.parse(createLineWrapper(), layerIndex, [], {} as any, SubdivisionGranularitySetting.noSubdivision);
82+
expect(result.buckets[0]).toBeTruthy();
83+
expect(result.buckets[0].layers[0].layout._values['line-join'].value.value).toBe('bevel');
84+
});
85+
4386
test('WorkerTile.parse skips hidden layers', async () => {
4487
const layerIndex = new StyleLayerIndex([{
4588
id: 'test-hidden',

src/source/worker_tile.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class WorkerTile {
113113
if (layer.maxzoom && this.zoom >= layer.maxzoom) continue;
114114
if (layer.visibility === 'none') continue;
115115

116-
recalculateLayers(family, this.zoom, availableImages);
116+
recalculateLayers(family, this.zoom, availableImages, this.globalState);
117117

118118
const bucket = buckets[layer.id] = layer.createBucket({
119119
index: featureIndex.bucketLayerIDs.length,
@@ -169,7 +169,7 @@ export class WorkerTile {
169169
for (const key in buckets) {
170170
const bucket = buckets[key];
171171
if (bucket instanceof SymbolBucket) {
172-
recalculateLayers(bucket.layers, this.zoom, availableImages);
172+
recalculateLayers(bucket.layers, this.zoom, availableImages, this.globalState);
173173
performSymbolLayout({
174174
bucket,
175175
glyphMap,
@@ -184,7 +184,7 @@ export class WorkerTile {
184184
(bucket instanceof LineBucket ||
185185
bucket instanceof FillBucket ||
186186
bucket instanceof FillExtrusionBucket)) {
187-
recalculateLayers(bucket.layers, this.zoom, availableImages);
187+
recalculateLayers(bucket.layers, this.zoom, availableImages, this.globalState);
188188
bucket.addFeatures(options, this.tileID.canonical, imageAtlas.patternPositions);
189189
}
190190
}
@@ -204,9 +204,9 @@ export class WorkerTile {
204204
}
205205
}
206206

207-
function recalculateLayers(layers: ReadonlyArray<StyleLayer>, zoom: number, availableImages: Array<string>) {
207+
function recalculateLayers(layers: ReadonlyArray<StyleLayer>, zoom: number, availableImages: Array<string>, globalState: Record<string, any>) {
208208
// Layers are shared and may have been used by a WorkerTile with a different zoom.
209-
const parameters = new EvaluationParameters(zoom);
209+
const parameters = new EvaluationParameters(zoom, {globalState});
210210
for (const layer of layers) {
211211
layer.recalculate(parameters, availableImages);
212212
}
3.79 KB
Loading
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"version": 8,
3+
"metadata": {
4+
"test": {
5+
"height": 256,
6+
"width": 256
7+
}
8+
},
9+
"center": [ 0, 0 ],
10+
"zoom": 0,
11+
"state": {
12+
"prefix": { "default": "pre_"},
13+
"suffix": { "default": "_suf" }
14+
},
15+
"sources": {
16+
"point": {
17+
"type": "geojson",
18+
"data": {
19+
"type": "FeatureCollection",
20+
"features": [
21+
{
22+
"type": "Feature",
23+
"properties": {
24+
"name": "Napoli"
25+
},
26+
"geometry": {
27+
"type": "Point",
28+
"coordinates": [ 0, 0 ]
29+
}
30+
}
31+
]
32+
}
33+
}
34+
},
35+
"glyphs": "local://glyphs/{fontstack}/{range}.pbf",
36+
"layers": [
37+
{
38+
"id": "text",
39+
"type": "symbol",
40+
"source": "point",
41+
"layout": {
42+
"text-field": [
43+
"concat",
44+
["global-state", "prefix"],
45+
["get", "name"],
46+
["global-state", "suffix"]
47+
],
48+
"text-font": [
49+
"Open Sans Semibold",
50+
"Arial Unicode MS Bold"
51+
],
52+
"text-size": 32
53+
}
54+
}
55+
]
56+
}

0 commit comments

Comments
 (0)