@deck.gl-community/experimental 9.0.3

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.
@@ -0,0 +1,260 @@
1
+ // deck.gl-community
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {Tile3DLayer, Tile3DLayerProps} from '@deck.gl/geo-layers';
6
+ import {UpdateParameters, Viewport, DefaultProps} from '@deck.gl/core';
7
+ import {TILE_TYPE, Tile3D, Tileset3D} from '@loaders.gl/tiles';
8
+ import {load} from '@loaders.gl/core';
9
+
10
+ const defaultProps: DefaultProps<DataDrivenTile3DLayerProps> = {
11
+ colorsByAttribute: null,
12
+ filtersByAttribute: null
13
+ };
14
+
15
+ type DataDrivenTile3DLayerProps<DataT = any> = _DataDrivenTile3DLayerProps &
16
+ Tile3DLayerProps<DataT>;
17
+
18
+ type _DataDrivenTile3DLayerProps = {
19
+ onTraversalComplete?: (selectedTiles: Tile3D[]) => Tile3D[];
20
+ colorsByAttribute?: ColorsByAttribute | null;
21
+ customizeColors?: (
22
+ tile: Tile3D,
23
+ colorsByAttribute: ColorsByAttribute | null
24
+ ) => Promise<{isColored: boolean; id: string}>;
25
+ filtersByAttribute?: FiltersByAttribute | null;
26
+ filterTile?: (
27
+ tile: Tile3D,
28
+ filtersByAttribute: FiltersByAttribute | null
29
+ ) => Promise<{isFiltered: boolean; id: string}>;
30
+ };
31
+
32
+ export type ColorsByAttribute = {
33
+ /** Feature attribute name */
34
+ attributeName: string;
35
+ /** Minimum attribute value */
36
+ minValue: number;
37
+ /** Maximum attribute value */
38
+ maxValue: number;
39
+ /** Minimum color. 3DObject will be colorized with gradient from `minColor to `maxColor` */
40
+ minColor: [number, number, number, number];
41
+ /** Maximum color. 3DObject will be colorized with gradient from `minColor to `maxColor` */
42
+ maxColor: [number, number, number, number];
43
+ /** Colorization mode. `replace` - replace vertex colors with a new colors, `multiply` - multiply vertex colors with new colors */
44
+ mode: string;
45
+ };
46
+
47
+ export type FiltersByAttribute = {
48
+ /** Feature attribute name */
49
+ attributeName: string;
50
+ /** Filter value */
51
+ value: number;
52
+ };
53
+
54
+ // @ts-expect-error call of private method of the base class
55
+ export class DataDrivenTile3DLayer<
56
+ DataT = any,
57
+ ExtraProps extends Record<string, unknown> = Record<string, unknown>
58
+ > extends Tile3DLayer<DataT, Required<_DataDrivenTile3DLayerProps> & ExtraProps> {
59
+ static layerName = 'DataDrivenTile3DLayer';
60
+ static defaultProps = defaultProps as any;
61
+
62
+ state: {
63
+ activeViewports: any;
64
+ frameNumber?: number;
65
+ lastUpdatedViewports: {[viewportId: string]: Viewport} | null;
66
+ layerMap: {[layerId: string]: any};
67
+ tileset3d: Tileset3D | null;
68
+
69
+ colorsByAttribute: ColorsByAttribute | null;
70
+ filtersByAttribute: FiltersByAttribute | null;
71
+ loadingCounter: number;
72
+ } = undefined!;
73
+
74
+ initializeState() {
75
+ super.initializeState();
76
+
77
+ this.setState({
78
+ colorsByAttribute: this.props.colorsByAttribute,
79
+ filtersByAttribute: this.props.filtersByAttribute,
80
+ loadingCounter: 0
81
+ });
82
+ }
83
+
84
+ updateState(params: UpdateParameters<this>): void {
85
+ const {props, oldProps, changeFlags} = params;
86
+
87
+ if (props.data && props.data !== oldProps.data) {
88
+ this._loadTileset(props.data);
89
+ } else if (props.colorsByAttribute !== oldProps.colorsByAttribute) {
90
+ this.setState({
91
+ colorsByAttribute: props.colorsByAttribute
92
+ });
93
+ this._colorizeTileset();
94
+ } else if (props.filtersByAttribute !== oldProps.filtersByAttribute) {
95
+ this.setState({
96
+ filtersByAttribute: props.filtersByAttribute
97
+ });
98
+ this._filterTileset();
99
+ } else if (changeFlags.viewportChanged) {
100
+ const {activeViewports} = this.state;
101
+ const viewportsNumber = Object.keys(activeViewports).length;
102
+ if (viewportsNumber) {
103
+ if (!this.state.loadingCounter) {
104
+ // @ts-expect-error call of private method of the base class
105
+ super._updateTileset(activeViewports);
106
+ }
107
+ this.state.lastUpdatedViewports = activeViewports;
108
+ this.state.activeViewports = {};
109
+ }
110
+ } else {
111
+ super.updateState(params);
112
+ }
113
+ }
114
+
115
+ private override async _loadTileset(tilesetUrl) {
116
+ const {loadOptions = {}} = this.props;
117
+
118
+ // TODO: deprecate `loader` in v9.0
119
+ let loader: any = this.props.loader || this.props.loaders;
120
+ if (Array.isArray(loader)) {
121
+ loader = loader[0];
122
+ }
123
+
124
+ const options = {loadOptions: {...loadOptions}};
125
+ if (loader.preload) {
126
+ const preloadOptions = await loader.preload(tilesetUrl, loadOptions);
127
+
128
+ if (preloadOptions.headers) {
129
+ options.loadOptions.fetch = {
130
+ ...options.loadOptions.fetch,
131
+ headers: preloadOptions.headers
132
+ };
133
+ }
134
+ Object.assign(options, preloadOptions);
135
+ }
136
+ const tilesetJson = await load(tilesetUrl, loader, options.loadOptions);
137
+
138
+ const tileset3d = new Tileset3D(tilesetJson, {
139
+ onTileLoad: this._onTileLoad.bind(this),
140
+ // @ts-expect-error call of private method of the base class
141
+ onTileUnload: super._onTileUnload.bind(this),
142
+ onTileError: this.props.onTileError,
143
+ // New code ------------------
144
+ onTraversalComplete: this._onTraversalComplete.bind(this),
145
+ // ---------------------------
146
+ ...options
147
+ });
148
+
149
+ this.setState({
150
+ tileset3d,
151
+ layerMap: {}
152
+ });
153
+
154
+ // @ts-expect-error call of private method of the base class
155
+ super._updateTileset(this.state.activeViewports);
156
+ this.props.onTilesetLoad(tileset3d);
157
+ }
158
+
159
+ private override _onTileLoad(tileHeader: Tile3D): void {
160
+ const {lastUpdatedViewports} = this.state;
161
+ // New code ------------------
162
+ this._colorizeTiles([tileHeader]);
163
+ this._filterTiles([tileHeader]);
164
+ // ---------------------------
165
+ this.props.onTileLoad(tileHeader);
166
+ // New code ------------------ condition is added
167
+ if (!this.state.colorsByAttribute && !this.state.filtersByAttribute) {
168
+ // ---------------------------
169
+ // @ts-expect-error call of private method of the base class
170
+ super._updateTileset(lastUpdatedViewports);
171
+ this.setNeedsUpdate();
172
+ // New code ------------------
173
+ }
174
+ // ------------------
175
+ }
176
+
177
+ private _onTraversalComplete(selectedTiles: Tile3D[]): Tile3D[] {
178
+ this._colorizeTiles(selectedTiles);
179
+ this._filterTiles(selectedTiles);
180
+ return this.props.onTraversalComplete
181
+ ? this.props.onTraversalComplete(selectedTiles)
182
+ : selectedTiles;
183
+ }
184
+
185
+ private _colorizeTiles(tiles: Tile3D[]): void {
186
+ if (this.props.customizeColors && tiles[0]?.type === TILE_TYPE.MESH) {
187
+ const {layerMap, colorsByAttribute} = this.state;
188
+ const promises: Promise<{isColored: boolean; id: string}>[] = [];
189
+ for (const tile of tiles) {
190
+ promises.push(this.props.customizeColors(tile, colorsByAttribute));
191
+ }
192
+ this.setState({
193
+ loadingCounter: this.state.loadingCounter + 1
194
+ });
195
+ Promise.allSettled(promises).then((result) => {
196
+ this.setState({
197
+ loadingCounter: this.state.loadingCounter - 1
198
+ });
199
+ let isTileChanged = false;
200
+ for (const item of result) {
201
+ if (item.status === 'fulfilled' && item.value.isColored) {
202
+ isTileChanged = true;
203
+ delete layerMap[item.value.id];
204
+ }
205
+ }
206
+ if (isTileChanged && !this.state.loadingCounter) {
207
+ // @ts-expect-error call of private method of the base class
208
+ super._updateTileset(this.state.activeViewports);
209
+ this.setNeedsUpdate();
210
+ }
211
+ });
212
+ }
213
+ }
214
+
215
+ private _colorizeTileset(): void {
216
+ const {tileset3d} = this.state;
217
+
218
+ if (tileset3d) {
219
+ this._colorizeTiles(tileset3d.selectedTiles);
220
+ }
221
+ }
222
+
223
+ private _filterTiles(tiles: Tile3D[]): void {
224
+ if (this.props.filterTile && tiles[0]?.type === TILE_TYPE.MESH) {
225
+ const {layerMap, filtersByAttribute} = this.state;
226
+ const promises: Promise<{isFiltered: boolean; id: string}>[] = [];
227
+ for (const tile of tiles) {
228
+ promises.push(this.props.filterTile(tile, filtersByAttribute));
229
+ }
230
+ this.setState({
231
+ loadingCounter: this.state.loadingCounter + 1
232
+ });
233
+ Promise.allSettled(promises).then((result) => {
234
+ this.setState({
235
+ loadingCounter: this.state.loadingCounter - 1
236
+ });
237
+ let isTileChanged = false;
238
+ for (const item of result) {
239
+ if (item.status === 'fulfilled' && item.value.isFiltered) {
240
+ isTileChanged = true;
241
+ delete layerMap[item.value.id];
242
+ }
243
+ }
244
+ if (isTileChanged && !this.state.loadingCounter) {
245
+ // @ts-expect-error call of private method of the base class
246
+ super._updateTileset(this.state.activeViewports);
247
+ this.setNeedsUpdate();
248
+ }
249
+ });
250
+ }
251
+ }
252
+
253
+ private _filterTileset(): void {
254
+ const {tileset3d} = this.state;
255
+
256
+ if (tileset3d) {
257
+ this._filterTiles(tileset3d.selectedTiles);
258
+ }
259
+ }
260
+ }
@@ -0,0 +1,53 @@
1
+ // deck.gl-community
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {customizeColors} from '@loaders.gl/i3s';
6
+ import {Tile3D} from '@loaders.gl/tiles';
7
+ import {ColorsByAttribute} from '../data-driven-tile-3d-layer';
8
+
9
+ /**
10
+ * Update tile colors with the custom colors assigned to the I3S Loader
11
+ * @returns {Promise<{isColored: boolean; id: string}>} Result of the tile colorization - isColored: true/false and tile id
12
+ */
13
+ export const colorizeTile = async (
14
+ tile: Tile3D,
15
+ colorsByAttribute: ColorsByAttribute | null
16
+ ): Promise<{isColored: boolean; id: string}> => {
17
+ const result = {isColored: false, id: tile.id};
18
+
19
+ if (tile.content.customColors !== colorsByAttribute) {
20
+ if (tile.content && colorsByAttribute) {
21
+ if (!tile.content.originalColorsAttributes) {
22
+ tile.content.originalColorsAttributes = {
23
+ ...tile.content.attributes.colors,
24
+ value: new Uint8Array(tile.content.attributes.colors.value)
25
+ };
26
+ } else if (colorsByAttribute.mode === 'multiply') {
27
+ tile.content.attributes.colors.value.set(tile.content.originalColorsAttributes.value);
28
+ }
29
+
30
+ tile.content.customColors = colorsByAttribute;
31
+
32
+ const newColors = await customizeColors(
33
+ tile.content.attributes.colors,
34
+ tile.content.featureIds,
35
+ tile.header.attributeUrls,
36
+ tile.tileset.tileset.fields,
37
+ tile.tileset.tileset.attributeStorageInfo,
38
+ colorsByAttribute,
39
+ (tile.tileset.loadOptions as any).i3s.token
40
+ );
41
+ // Make sure custom colors is not changed during async customizeColors execution
42
+ if (tile.content.customColors === colorsByAttribute) {
43
+ tile.content.attributes.colors = newColors;
44
+ result.isColored = true;
45
+ }
46
+ } else if (tile.content && tile.content.originalColorsAttributes) {
47
+ tile.content.attributes.colors.value = tile.content.originalColorsAttributes.value;
48
+ tile.content.customColors = null;
49
+ result.isColored = true;
50
+ }
51
+ }
52
+ return result;
53
+ };
@@ -0,0 +1,177 @@
1
+ // deck.gl-community
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {Tile3D} from '@loaders.gl/tiles';
6
+ import {FiltersByAttribute} from '../data-driven-tile-3d-layer';
7
+ import {AttributeStorageInfo, I3SAttributeLoader} from '@loaders.gl/i3s';
8
+ import {load} from '@loaders.gl/core';
9
+ import {TypedArray} from '@loaders.gl/schema';
10
+
11
+ type I3STileAttributes = Record<string, string[] | TypedArray | null>;
12
+
13
+ /**
14
+ * Filter tile indices by attribute value
15
+ * @param tile - tile to be filtered
16
+ * @param filtersByAttribute - custom filters patameters
17
+ * @returns {Promise<{isFiltered: boolean; id: string}>} Result of the tile filtering - isFiltered: true/false and tile id
18
+ */
19
+ export const filterTile = async (
20
+ tile: Tile3D,
21
+ filtersByAttribute: FiltersByAttribute | null
22
+ ): Promise<{isFiltered: boolean; id: string}> => {
23
+ const result = {isFiltered: false, id: tile.id};
24
+
25
+ if (tile.content.userData?.customFilters !== filtersByAttribute) {
26
+ if (tile.content && filtersByAttribute) {
27
+ if (tile.content.userData?.originalIndices === undefined) {
28
+ tile.content.userData = {};
29
+ // save original indices for filtring cancellation
30
+ tile.content.userData.originalIndices = tile.content.indices;
31
+ }
32
+ tile.content.indices = tile.content.userData?.originalIndices;
33
+ tile.content.userData.customFilters = filtersByAttribute;
34
+
35
+ const {indices} = await filterTileIndices(
36
+ tile,
37
+ filtersByAttribute,
38
+ (tile.tileset.loadOptions as any).i3s.token
39
+ );
40
+ // Make sure custom filters is not changed during async filtring execution
41
+ if (indices && tile.content.userData.customFilters === filtersByAttribute) {
42
+ tile.content.indices = indices;
43
+ result.isFiltered = true;
44
+ }
45
+ } else if (tile.content && tile.content.userData?.originalIndices !== undefined) {
46
+ tile.content.indices = tile.content.userData.originalIndices;
47
+ tile.content.userData.customFilters = null;
48
+ result.isFiltered = true;
49
+ }
50
+ }
51
+ return result;
52
+ };
53
+
54
+ // eslint-disable-next-line max-statements, complexity
55
+ async function filterTileIndices(
56
+ tile: Tile3D,
57
+ filtersByAttribute: FiltersByAttribute,
58
+ token: string
59
+ ): Promise<{success: boolean; indices?: Uint32Array}> {
60
+ if (!filtersByAttribute.attributeName.length) {
61
+ return {success: false};
62
+ }
63
+
64
+ const filterAttributeField = tile.tileset.tileset.fields.find(
65
+ ({name}) => name === filtersByAttribute?.attributeName
66
+ );
67
+
68
+ if (
69
+ !filterAttributeField ||
70
+ !['esriFieldTypeDouble', 'esriFieldTypeInteger', 'esriFieldTypeSmallInteger'].includes(
71
+ filterAttributeField.type
72
+ )
73
+ ) {
74
+ return {success: false};
75
+ }
76
+
77
+ const tileFilterAttributeData = await loadFeatureAttributeData(
78
+ filterAttributeField.name,
79
+ tile.header.attributeUrls,
80
+ tile.tileset.tileset.attributeStorageInfo,
81
+ token
82
+ );
83
+ if (!tileFilterAttributeData) {
84
+ return {success: false};
85
+ }
86
+
87
+ const objectIdField = tile.tileset.tileset.fields.find(({type}) => type === 'esriFieldTypeOID');
88
+ if (!objectIdField) {
89
+ return {success: false};
90
+ }
91
+
92
+ const objectIdAttributeData = await loadFeatureAttributeData(
93
+ objectIdField.name,
94
+ tile.header.attributeUrls,
95
+ tile.tileset.tileset.attributeStorageInfo,
96
+ token
97
+ );
98
+ if (!objectIdAttributeData) {
99
+ return {success: false};
100
+ }
101
+
102
+ const attributeValuesMap = {};
103
+ objectIdAttributeData[objectIdField.name]?.forEach((elem, index) => {
104
+ attributeValuesMap[elem] = tileFilterAttributeData[filterAttributeField.name][index];
105
+ });
106
+
107
+ if (!tile.content.indices) {
108
+ const triangles: number[] = [];
109
+ for (let i = 0; i < tile.content.featureIds.length; i += 3) {
110
+ if (attributeValuesMap[tile.content.featureIds[i]] === filtersByAttribute.value) {
111
+ triangles.push(i);
112
+ }
113
+ }
114
+
115
+ const indices = new Uint32Array(3 * triangles.length);
116
+
117
+ triangles.forEach((vertex, index) => {
118
+ indices[index * 3] = vertex;
119
+ indices[index * 3 + 1] = vertex + 1;
120
+ indices[index * 3 + 2] = vertex + 2;
121
+ });
122
+ return {success: true, indices};
123
+ }
124
+ const triangles: number[] = [];
125
+ for (let i = 0; i < tile.content.indices.length; i += 3) {
126
+ if (
127
+ attributeValuesMap[tile.content.featureIds[tile.content.indices[i]]] ===
128
+ filtersByAttribute.value
129
+ ) {
130
+ triangles.push(i);
131
+ }
132
+ }
133
+
134
+ const indices = new Uint32Array(3 * triangles.length);
135
+
136
+ triangles.forEach((vertex, index) => {
137
+ indices[index * 3] = tile.content.indices[vertex];
138
+ indices[index * 3 + 1] = tile.content.indices[vertex + 1];
139
+ indices[index * 3 + 2] = tile.content.indices[vertex + 2];
140
+ });
141
+ return {success: true, indices};
142
+ }
143
+
144
+ async function loadFeatureAttributeData(
145
+ attributeName: string,
146
+ attributeUrls: string[],
147
+ attributesStorageInfo: AttributeStorageInfo[],
148
+ token?: string
149
+ ): Promise<I3STileAttributes | null> {
150
+ const attributeIndex = attributesStorageInfo.findIndex(({name}) => attributeName === name);
151
+ if (attributeIndex === -1) {
152
+ return null;
153
+ }
154
+ const objectIdAttributeUrl = getUrlWithToken(attributeUrls[attributeIndex], token);
155
+ const attributeType = getAttributeValueType(attributesStorageInfo[attributeIndex]);
156
+ const objectIdAttributeData = await load(objectIdAttributeUrl, I3SAttributeLoader, {
157
+ attributeName,
158
+ attributeType
159
+ });
160
+
161
+ return objectIdAttributeData;
162
+ }
163
+
164
+ function getUrlWithToken(url: string, token: string | null = null): string {
165
+ return token ? `${url}?token=${token}` : url;
166
+ }
167
+
168
+ function getAttributeValueType(attribute: AttributeStorageInfo) {
169
+ // eslint-disable-next-line no-prototype-builtins
170
+ if (attribute.hasOwnProperty('objectIds')) {
171
+ return 'Oid32';
172
+ // eslint-disable-next-line no-prototype-builtins
173
+ } else if (attribute.hasOwnProperty('attributeValues')) {
174
+ return attribute.attributeValues?.valueType;
175
+ }
176
+ return '';
177
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ // deck.gl-community
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ export {DataDrivenTile3DLayer} from './data-driven-tile-3d-layer/data-driven-tile-3d-layer';
6
+
7
+ export {colorizeTile} from './data-driven-tile-3d-layer/utils/colorize-tile';
8
+ export {filterTile} from './data-driven-tile-3d-layer/utils/filter-tile';