@mesh3d/cesium-vectortile-gl 0.4.4 → 0.4.6

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 (51) hide show
  1. package/.gitattributes +11 -0
  2. package/.gitconfig +3 -0
  3. package/.husky/pre-commit +1 -0
  4. package/.prettierignore +5 -0
  5. package/.vscode/settings.json +25 -0
  6. package/LICENSE.md +203 -203
  7. package/README.md +202 -167
  8. package/Source/Cesium.d.ts +2692 -2691
  9. package/Source/VectorTileLOD.js +720 -532
  10. package/Source/VectorTileRenderList.js +70 -70
  11. package/Source/VectorTileset.js +473 -447
  12. package/Source/layers/BackgroundRenderLayer.js +91 -89
  13. package/Source/layers/FillRenderLayer.js +18 -18
  14. package/Source/layers/IRenderLayer.js +160 -152
  15. package/Source/layers/LineRenderLayer.js +104 -94
  16. package/Source/layers/SymbolRenderLayer.js +30 -31
  17. package/Source/layers/index.js +23 -16
  18. package/Source/layers/registerRenderLayer.js +24 -24
  19. package/Source/layers/visualizers/FillLayerVisualizer.js +542 -426
  20. package/Source/layers/visualizers/ILayerVisualizer.js +90 -94
  21. package/Source/layers/visualizers/LineLayerVisualizer.js +702 -571
  22. package/Source/layers/visualizers/SymbolLayerVisualizer.js +514 -244
  23. package/Source/sources/GeoJSONSource.js +53 -46
  24. package/Source/sources/ISource.js +39 -39
  25. package/Source/sources/VectorSource.js +94 -52
  26. package/Source/sources/granularitySettings.js +23 -20
  27. package/Source/sources/index.js +6 -11
  28. package/Source/sources/registerSource.js +17 -19
  29. package/Source/style/StyleLayer.js +43 -43
  30. package/Source/style/StyleLayerProperties.js +44 -43
  31. package/Source/style/index.js +2 -2
  32. package/Source/symbol/SymbolPlacements.js +117 -88
  33. package/Source/workers/VectorTileWorker.js +41 -0
  34. package/Source/workers/ellipsoid.js +47 -0
  35. package/Source/workers/processTileTask.js +329 -0
  36. package/Source/workers/styleEvaluator.js +168 -0
  37. package/benchmark.html +148 -0
  38. package/dist/cvt-gl-worker.js +9274 -0
  39. package/dist/cvt-gl-worker.js.map +1 -0
  40. package/dist/cvt-gl.js +2570 -2001
  41. package/dist/cvt-gl.js.map +1 -1
  42. package/dist/cvt-gl.min.js +3 -3
  43. package/dist/cvt-gl.min.js.map +1 -1
  44. package/eslint.config.mjs +58 -0
  45. package/index.js +9 -6
  46. package/mlt.html +26 -25
  47. package/package.json +64 -41
  48. package/prettier.config.mjs +30 -0
  49. package/vite.config.mjs +43 -0
  50. package/vite.worker.config.mjs +31 -0
  51. package/worker.html +26 -0
@@ -1,46 +1,53 @@
1
- import { ISource } from "./ISource";
2
- import { registerSource } from "./registerSource";
3
- import geojsonvt from "geojson-vt";
4
- import { GeoJSONWrapper } from '@maplibre/vt-pbf'
5
- import { EXTENT } from 'maplibre-gl/src/data/extent';
6
-
7
- export class GeoJSONSource extends ISource {
8
- constructor(styleSource, path) {
9
- super(styleSource, path)
10
- }
11
-
12
- async init() {
13
- /**@type {import("@maplibre/maplibre-gl-style-spec").GeoJSONSourceSpecification} */
14
- const sourceParams = this.styleSource
15
- let data = sourceParams.data
16
- if (typeof data === 'string') {
17
- const url = /^((http)|(https)|(data:)|\/)/.test(data) ? data : this.path + data
18
- try {
19
- data = await Cesium.Resource.fetchJson(url)
20
- } catch (err) {
21
- this.errorEvent.raiseEvent(err)
22
- }
23
- }
24
- if (data && data.features?.length) {
25
- this.tileIndex = new geojsonvt(data, {
26
- extent: EXTENT,
27
- buffer: sourceParams.buffer === undefined ? 128 : sourceParams.buffer,
28
- tolerance: sourceParams.tolerance === sourceParams.tolerance ? 0.375 : sourceParams.tolerance
29
- })
30
- }
31
- }
32
-
33
- async requestTile(x, y, z) {
34
- if (!this.tileIndex) return
35
- try {
36
- const geoJSONTile = this.tileIndex.getTile(z, x, y)
37
- if (!geoJSONTile) return
38
- const geojsonWrapper = new GeoJSONWrapper(geoJSONTile.features, { extent: EXTENT });
39
- return geojsonWrapper
40
- } catch (err) {
41
- this.errorEvent.raiseEvent(err)
42
- }
43
- }
44
- }
45
-
46
- registerSource('geojson', GeoJSONSource)
1
+ import { ISource } from './ISource'
2
+ import { registerSource } from './registerSource'
3
+ import geojsonvt from 'geojson-vt'
4
+ import { GeoJSONWrapper } from '@maplibre/vt-pbf'
5
+ import { EXTENT } from 'maplibre-gl/src/data/extent'
6
+
7
+ export class GeoJSONSource extends ISource {
8
+ constructor(styleSource, path) {
9
+ super(styleSource, path)
10
+ }
11
+
12
+ async init() {
13
+ /**@type {import("@maplibre/maplibre-gl-style-spec").GeoJSONSourceSpecification} */
14
+ const sourceParams = this.styleSource
15
+ let data = sourceParams.data
16
+ if (typeof data === 'string') {
17
+ const url = /^((http)|(https)|(data:)|\/)/.test(data)
18
+ ? data
19
+ : this.path + data
20
+ try {
21
+ data = await Cesium.Resource.fetchJson(url)
22
+ } catch (err) {
23
+ this.errorEvent.raiseEvent(err)
24
+ }
25
+ }
26
+ if (data && data.features?.length) {
27
+ this.tileIndex = new geojsonvt(data, {
28
+ extent: EXTENT,
29
+ buffer: sourceParams.buffer === undefined ? 128 : sourceParams.buffer,
30
+ tolerance:
31
+ sourceParams.tolerance === sourceParams.tolerance
32
+ ? 0.375
33
+ : sourceParams.tolerance
34
+ })
35
+ }
36
+ }
37
+
38
+ async requestTile(x, y, z) {
39
+ if (!this.tileIndex) return
40
+ try {
41
+ const geoJSONTile = this.tileIndex.getTile(z, x, y)
42
+ if (!geoJSONTile) return
43
+ const geojsonWrapper = new GeoJSONWrapper(geoJSONTile.features, {
44
+ extent: EXTENT
45
+ })
46
+ return geojsonWrapper
47
+ } catch (err) {
48
+ this.errorEvent.raiseEvent(err)
49
+ }
50
+ }
51
+ }
52
+
53
+ registerSource('geojson', GeoJSONSource)
@@ -1,39 +1,39 @@
1
- import { VectorTile } from '@mapbox/vector-tile'
2
-
3
- /**
4
- * 数据源基类,定义通用属性和必须实现的方法(init、requestTile)
5
- * @see VectorSource
6
- * @see GeoJSONSource
7
- */
8
- export class ISource {
9
- /**
10
- * 构造数据源实例。注意:该构造函数由VectorTileset调用,请勿在其他模块直接调用
11
- * @param {import('@maplibre/maplibre-gl-style-spec').SourceSpecification} styleSource
12
- * @param {string} [path]
13
- * @see VectorSource
14
- * @see GeoJSONSource
15
- */
16
- constructor(styleSource, path = '') {
17
- /**@type {"vector"|"geojson"} */
18
- this.type = styleSource.type
19
- /**@type {import('@maplibre/maplibre-gl-style-spec').SourceSpecification} */
20
- this.styleSource = styleSource
21
- this.path = path
22
- this.errorEvent = new Cesium.Event()
23
- }
24
-
25
- async init() { }
26
-
27
- /**
28
- * @param {number} x
29
- * @param {number} y
30
- * @param {number} z
31
- * @returns {Promise<VectorTile>}
32
- */
33
- requestTile(x, y, z) { }
34
-
35
- destroy() {
36
- this.styleSource = null
37
- this.errorEvent = null
38
- }
39
- }
1
+ import { VectorTile } from '@mapbox/vector-tile'
2
+
3
+ /**
4
+ * 数据源基类,定义通用属性和必须实现的方法(init、requestTile)
5
+ * @see VectorSource
6
+ * @see GeoJSONSource
7
+ */
8
+ export class ISource {
9
+ /**
10
+ * 构造数据源实例。注意:该构造函数由VectorTileset调用,请勿在其他模块直接调用
11
+ * @param {import('@maplibre/maplibre-gl-style-spec').SourceSpecification} styleSource
12
+ * @param {string} [path]
13
+ * @see VectorSource
14
+ * @see GeoJSONSource
15
+ */
16
+ constructor(styleSource, path = '') {
17
+ /**@type {"vector"|"geojson"} */
18
+ this.type = styleSource.type
19
+ /**@type {import('@maplibre/maplibre-gl-style-spec').SourceSpecification} */
20
+ this.styleSource = styleSource
21
+ this.path = path
22
+ this.errorEvent = new Cesium.Event()
23
+ }
24
+
25
+ async init() {}
26
+
27
+ /**
28
+ * @param {number} x
29
+ * @param {number} y
30
+ * @param {number} z
31
+ * @returns {Promise<VectorTile>}
32
+ */
33
+ requestTile(x, y, z) {}
34
+
35
+ destroy() {
36
+ this.styleSource = null
37
+ this.errorEvent = null
38
+ }
39
+ }
@@ -1,52 +1,94 @@
1
- import { ISource } from "./ISource";
2
- import { registerSource } from "./registerSource";
3
- import { VectorTile, VectorTileFeature } from '@mapbox/vector-tile'
4
- import { MLTVectorTile } from 'maplibre-gl/src/source/vector_tile_mlt'
5
- import Pbf from 'pbf'
6
-
7
- export class VectorSource extends ISource {
8
- constructor(styleSource, path) {
9
- super(styleSource, path)
10
- /**@type {Cesium.WebMercatorTilingScheme} */
11
- this.tilingScheme = new Cesium.WebMercatorTilingScheme()
12
- }
13
-
14
- async init() {
15
- const sourceParams = this.styleSource
16
- let url = sourceParams.url
17
- if (url && !sourceParams.tiles) {
18
- url = /^((http)|(https)|(data:)|\/)/.test(url) ? url : this.path + sourceParams.url
19
- try {
20
- const metadata = await Cesium.Resource.fetchJson(url)
21
- for (const key in metadata) {
22
- if (!sourceParams[key]) {
23
- sourceParams[key] = metadata[key]
24
- }
25
- }
26
- } catch (err) {
27
- this.errorEvent.raiseEvent(err)
28
- }
29
- }
30
- }
31
-
32
- async requestTile(x, y, z) {
33
- const sourceParams = this.styleSource
34
- if (!sourceParams.tiles || !sourceParams.tiles.length) return
35
- if (sourceParams.scheme === 'tms') {
36
- const numOfY = this.tilingScheme.getNumberOfYTilesAtLevel(z)
37
- y = numOfY - y - 1
38
- }
39
- let tileUrl = sourceParams.tiles[0].replace('{x}', x).replace('{y}', y).replace('{z}', z)
40
- tileUrl = /^((http)|(https)|(data:)|\/)/.test(tileUrl) ? tileUrl : this.path + tileUrl
41
-
42
- try {
43
- const tileBuf = await (fetch(tileUrl).then(res => res.arrayBuffer()));
44
- const tileData = sourceParams.encoding == 'mlt' ? new MLTVectorTile(tileBuf) : new VectorTile(new Pbf(tileBuf))
45
- return tileData
46
- } catch (err) {
47
- this.errorEvent.raiseEvent(err)
48
- }
49
- }
50
- }
51
-
52
- registerSource('vector', VectorSource)
1
+ import { ISource } from './ISource'
2
+ import { registerSource } from './registerSource'
3
+ import { VectorTile, VectorTileFeature } from '@mapbox/vector-tile'
4
+ import { MLTVectorTile } from 'maplibre-gl/src/source/vector_tile_mlt'
5
+ import Pbf from 'pbf'
6
+
7
+ export class VectorSource extends ISource {
8
+ constructor(styleSource, path) {
9
+ super(styleSource, path)
10
+ /**@type {Cesium.WebMercatorTilingScheme} */
11
+ this.tilingScheme = new Cesium.WebMercatorTilingScheme()
12
+ }
13
+
14
+ async init() {
15
+ const sourceParams = this.styleSource
16
+ let url = sourceParams.url
17
+ if (url && !sourceParams.tiles) {
18
+ url = /^((http)|(https)|(data:)|\/)/.test(url)
19
+ ? url
20
+ : this.path + sourceParams.url
21
+ try {
22
+ const metadata = await Cesium.Resource.fetchJson(url)
23
+ for (const key in metadata) {
24
+ if (!sourceParams[key]) {
25
+ sourceParams[key] = metadata[key]
26
+ }
27
+ }
28
+ } catch (err) {
29
+ this.errorEvent.raiseEvent(err)
30
+ }
31
+ }
32
+ }
33
+
34
+ async requestTile(x, y, z) {
35
+ const sourceParams = this.styleSource
36
+ if (!sourceParams.tiles || !sourceParams.tiles.length) return
37
+ if (sourceParams.scheme === 'tms') {
38
+ const numOfY = this.tilingScheme.getNumberOfYTilesAtLevel(z)
39
+ y = numOfY - y - 1
40
+ }
41
+ let tileUrl = sourceParams.tiles[0]
42
+ .replace('{x}', x)
43
+ .replace('{y}', y)
44
+ .replace('{z}', z)
45
+ tileUrl = /^((http)|(https)|(data:)|\/)/.test(tileUrl)
46
+ ? tileUrl
47
+ : this.path + tileUrl
48
+
49
+ try {
50
+ const tileBuf = await fetch(tileUrl).then(res => res.arrayBuffer())
51
+ const tileData =
52
+ sourceParams.encoding == 'mlt'
53
+ ? new MLTVectorTile(tileBuf)
54
+ : new VectorTile(new Pbf(tileBuf))
55
+ return tileData
56
+ } catch (err) {
57
+ this.errorEvent.raiseEvent(err)
58
+ }
59
+ }
60
+
61
+ /**
62
+ * 仅拉取瓦片 ArrayBuffer,不解析;供 Web Worker 路径使用。
63
+ * @param {number} x
64
+ * @param {number} y
65
+ * @param {number} z
66
+ * @param {import('../../VectorTileset').VectorTileset} [tileset]
67
+ * @returns {Promise<{buffer:ArrayBuffer,encoding:string}|undefined>}
68
+ */
69
+ async requestTileBuffer(x, y, z, tileset) {
70
+ const sourceParams = this.styleSource
71
+ if (!sourceParams.tiles || !sourceParams.tiles.length) return
72
+ if (sourceParams.scheme === 'tms') {
73
+ const numOfY = this.tilingScheme.getNumberOfYTilesAtLevel(z)
74
+ y = numOfY - y - 1
75
+ }
76
+ let tileUrl = sourceParams.tiles[0]
77
+ .replace('{x}', x)
78
+ .replace('{y}', y)
79
+ .replace('{z}', z)
80
+ tileUrl = /^((http)|(https)|(data:)|\/)/.test(tileUrl)
81
+ ? tileUrl
82
+ : this.path + tileUrl
83
+
84
+ try {
85
+ const buffer = await fetch(tileUrl).then(res => res.arrayBuffer())
86
+ const encoding = sourceParams.encoding === 'mlt' ? 'mlt' : 'mvt'
87
+ return { buffer, encoding }
88
+ } catch (err) {
89
+ this.errorEvent.raiseEvent(err)
90
+ }
91
+ }
92
+ }
93
+
94
+ registerSource('vector', VectorSource)
@@ -1,20 +1,23 @@
1
- import { SubdivisionGranularitySetting, SubdivisionGranularityExpression } from "maplibre-gl/src/render/subdivision_granularity_settings"
2
-
3
- const granularitySettings = {
4
- globe: new SubdivisionGranularitySetting({
5
- fill: new SubdivisionGranularityExpression(128, 2),
6
- line: new SubdivisionGranularityExpression(512, 0),
7
- // Always keep at least some subdivision on raster tiles, etc,
8
- // otherwise they will be visibly warped at high zooms (before mercator transition).
9
- // This si not needed on fill, because fill geometry tends to already be
10
- // highly tessellated and granular at high zooms.
11
- tile: new SubdivisionGranularityExpression(128, 32),
12
- // Stencil granularity must never be higher than fill granularity,
13
- // otherwise we would get seams in the oceans at zoom levels where
14
- // stencil has higher granularity than fill.
15
- stencil: new SubdivisionGranularityExpression(128, 1),
16
- circle: 3
17
- })
18
- }
19
-
20
- export { granularitySettings }
1
+ import {
2
+ SubdivisionGranularitySetting,
3
+ SubdivisionGranularityExpression
4
+ } from 'maplibre-gl/src/render/subdivision_granularity_settings'
5
+
6
+ const granularitySettings = {
7
+ globe: new SubdivisionGranularitySetting({
8
+ fill: new SubdivisionGranularityExpression(128, 2),
9
+ line: new SubdivisionGranularityExpression(512, 0),
10
+ // Always keep at least some subdivision on raster tiles, etc,
11
+ // otherwise they will be visibly warped at high zooms (before mercator transition).
12
+ // This si not needed on fill, because fill geometry tends to already be
13
+ // highly tessellated and granular at high zooms.
14
+ tile: new SubdivisionGranularityExpression(128, 32),
15
+ // Stencil granularity must never be higher than fill granularity,
16
+ // otherwise we would get seams in the oceans at zoom levels where
17
+ // stencil has higher granularity than fill.
18
+ stencil: new SubdivisionGranularityExpression(128, 1),
19
+ circle: 3
20
+ })
21
+ }
22
+
23
+ export { granularitySettings }
@@ -1,11 +1,6 @@
1
-
2
- import { ISource } from './ISource'
3
- import { registerSource, Sources } from './registerSource'
4
- import { VectorSource } from './VectorSource'
5
- import { GeoJSONSource } from './GeoJSONSource'
6
-
7
- export {
8
- ISource, registerSource, Sources,
9
- VectorSource,
10
- GeoJSONSource
11
- }
1
+ import { ISource } from './ISource'
2
+ import { registerSource, Sources } from './registerSource'
3
+ import { VectorSource } from './VectorSource'
4
+ import { GeoJSONSource } from './GeoJSONSource'
5
+
6
+ export { ISource, registerSource, Sources, VectorSource, GeoJSONSource }
@@ -1,19 +1,17 @@
1
- import { ISource } from "./ISource"
2
-
3
- /**
4
- * @type {{[sourceType:string]:typeof ISource}}
5
- */
6
- const Sources = {}
7
-
8
- /**
9
- * 注册数据源类型,设置数据源类
10
- * @param {*} type
11
- * @param {*} sourceCls
12
- */
13
- function registerSource(type, sourceCls) {
14
- Sources[type] = sourceCls
15
- }
16
-
17
- export {
18
- Sources, registerSource
19
- }
1
+ import { ISource } from './ISource'
2
+
3
+ /**
4
+ * @type {{[sourceType:string]:typeof ISource}}
5
+ */
6
+ const Sources = {}
7
+
8
+ /**
9
+ * 注册数据源类型,设置数据源类
10
+ * @param {*} type
11
+ * @param {*} sourceCls
12
+ */
13
+ function registerSource(type, sourceCls) {
14
+ Sources[type] = sourceCls
15
+ }
16
+
17
+ export { Sources, registerSource }
@@ -1,43 +1,43 @@
1
- import { featureFilter, Color } from "@maplibre/maplibre-gl-style-spec";
2
- import { StyleLayerProperties } from "./StyleLayerProperties";
3
-
4
- export class StyleLayer {
5
- /**
6
- * @param {import('@maplibre/maplibre-gl-style-spec').LayerSpecification} layer
7
- */
8
- constructor(layer) {
9
- this.data = layer
10
- this.type = layer.type
11
- this.id = layer.id
12
- this.minzoom = layer.minzoom || 0
13
- this.maxzoom = layer.maxzoom || 24
14
- this.source = layer.source
15
- /**@type {string} */
16
- this.sourceLayer = layer['source-layer']
17
- /**@type {import("@maplibre/maplibre-gl-style-spec").FeatureFilter|null} */
18
- this.filter = null
19
- this.paint = new StyleLayerProperties('paint_' + layer.type, layer.paint)
20
- this.layout = new StyleLayerProperties('layout_' + layer.type, layer.layout)
21
- if (layer.filter) {
22
- this.filter = featureFilter(layer.filter)
23
- }
24
- }
25
-
26
- /**
27
- * 转换图层样式颜色,内部进行预乘Alpha的逆处理,@maplibre/maplibre-gl-style-spec内部会自动对颜色进行premultiplyAlpha操作,直接使用会出现明显的色差
28
- * @param {Color} styleColor
29
- * @param {Cesium.Color} [result]
30
- * @returns
31
- */
32
- convertColor(styleColor, result) {
33
- const alphaScalar = styleColor.a > 0 ? 1. / styleColor.a : 1
34
- if (!result) {
35
- result = new Cesium.Color()
36
- }
37
- result.red = styleColor.r * alphaScalar
38
- result.green = styleColor.g * alphaScalar
39
- result.blue = styleColor.b * alphaScalar
40
- result.alpha = styleColor.a
41
- return result
42
- }
43
- }
1
+ import { featureFilter, Color } from '@maplibre/maplibre-gl-style-spec'
2
+ import { StyleLayerProperties } from './StyleLayerProperties'
3
+
4
+ export class StyleLayer {
5
+ /**
6
+ * @param {import('@maplibre/maplibre-gl-style-spec').LayerSpecification} layer
7
+ */
8
+ constructor(layer) {
9
+ this.data = layer
10
+ this.type = layer.type
11
+ this.id = layer.id
12
+ this.minzoom = layer.minzoom || 0
13
+ this.maxzoom = layer.maxzoom || 24
14
+ this.source = layer.source
15
+ /**@type {string} */
16
+ this.sourceLayer = layer['source-layer']
17
+ /**@type {import("@maplibre/maplibre-gl-style-spec").FeatureFilter|null} */
18
+ this.filter = null
19
+ this.paint = new StyleLayerProperties('paint_' + layer.type, layer.paint)
20
+ this.layout = new StyleLayerProperties('layout_' + layer.type, layer.layout)
21
+ if (layer.filter) {
22
+ this.filter = featureFilter(layer.filter)
23
+ }
24
+ }
25
+
26
+ /**
27
+ * 转换图层样式颜色,内部进行预乘Alpha的逆处理,@maplibre/maplibre-gl-style-spec内部会自动对颜色进行premultiplyAlpha操作,直接使用会出现明显的色差
28
+ * @param {Color} styleColor
29
+ * @param {Cesium.Color} [result]
30
+ * @returns
31
+ */
32
+ convertColor(styleColor, result) {
33
+ const alphaScalar = styleColor.a > 0 ? 1 / styleColor.a : 1
34
+ if (!result) {
35
+ result = new Cesium.Color()
36
+ }
37
+ result.red = styleColor.r * alphaScalar
38
+ result.green = styleColor.g * alphaScalar
39
+ result.blue = styleColor.b * alphaScalar
40
+ result.alpha = styleColor.a
41
+ return result
42
+ }
43
+ }
@@ -1,43 +1,44 @@
1
- import { expression, latest } from '@maplibre/maplibre-gl-style-spec'
2
-
3
- export class StyleLayerProperties {
4
- constructor(groupName, styleProperties = {}) {
5
- this.data = styleProperties
6
- /**@type {Map<string,import('@maplibre/maplibre-gl-style-spec').StylePropertyExpression>} */
7
- this.props = new Map()
8
-
9
- const groupReference = latest[groupName]
10
- for (const key in groupReference) {
11
- if (Object.hasOwnProperty.call(groupReference, key)) {
12
- const reference = groupReference[key];
13
- const value = styleProperties[key];
14
- const property = expression.normalizePropertyExpression(
15
- value === undefined ? reference.default : value, reference,
16
- )
17
- this.props.set(key, property)
18
- }
19
- }
20
- }
21
-
22
- /**
23
- * Replace tokens in a string template with values in an object
24
- *
25
- * @param properties - a key/value relationship between tokens and replacements
26
- * @param text - the template string
27
- * @returns the template with tokens replaced
28
- */
29
- resolveTokens(properties, text) {
30
- return text.replace(/{([^{}]+)}/g, (match, key) => {
31
- return properties && key in properties ? String(properties[key]) : '';
32
- });
33
- }
34
- getDataConstValue(name, zoom) {
35
- const expr = this.props.get(name)
36
- return expr && expr.evaluate({ zoom })
37
- }
38
-
39
- getDataValue(name, zoom, feature) {
40
- const expr = this.props.get(name)
41
- return expr && expr.evaluate({ zoom }, feature)
42
- }
43
- }
1
+ import { expression, latest } from '@maplibre/maplibre-gl-style-spec'
2
+
3
+ export class StyleLayerProperties {
4
+ constructor(groupName, styleProperties = {}) {
5
+ this.data = styleProperties
6
+ /**@type {Map<string,import('@maplibre/maplibre-gl-style-spec').StylePropertyExpression>} */
7
+ this.props = new Map()
8
+
9
+ const groupReference = latest[groupName]
10
+ for (const key in groupReference) {
11
+ if (Object.hasOwnProperty.call(groupReference, key)) {
12
+ const reference = groupReference[key]
13
+ const value = styleProperties[key]
14
+ const property = expression.normalizePropertyExpression(
15
+ value === undefined ? reference.default : value,
16
+ reference
17
+ )
18
+ this.props.set(key, property)
19
+ }
20
+ }
21
+ }
22
+
23
+ /**
24
+ * Replace tokens in a string template with values in an object
25
+ *
26
+ * @param properties - a key/value relationship between tokens and replacements
27
+ * @param text - the template string
28
+ * @returns the template with tokens replaced
29
+ */
30
+ resolveTokens(properties, text) {
31
+ return text.replace(/{([^{}]+)}/g, (match, key) => {
32
+ return properties && key in properties ? String(properties[key]) : ''
33
+ })
34
+ }
35
+ getDataConstValue(name, zoom) {
36
+ const expr = this.props.get(name)
37
+ return expr && expr.evaluate({ zoom })
38
+ }
39
+
40
+ getDataValue(name, zoom, feature) {
41
+ const expr = this.props.get(name)
42
+ return expr && expr.evaluate({ zoom }, feature)
43
+ }
44
+ }
@@ -1,2 +1,2 @@
1
- export { StyleLayerProperties } from "./StyleLayerProperties";
2
- export { StyleLayer } from "./StyleLayer";
1
+ export { StyleLayerProperties } from './StyleLayerProperties'
2
+ export { StyleLayer } from './StyleLayer'