-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathraster-tile-layer-specs.js
More file actions
326 lines (278 loc) · 11.2 KB
/
Copy pathraster-tile-layer-specs.js
File metadata and controls
326 lines (278 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
import test from 'tape';
import {KeplerGlLayers} from '@kepler.gl/layers';
import {testCreateCases} from 'test/helpers/layer-utils';
const {RasterTileLayer} = KeplerGlLayers;
// Mock data for raster tests
const MOCK_STAC_METADATA = {
type: 'Feature',
stac_version: '1.0.0',
stac_extensions: ['https://stac-extensions.github.io/eo/v1.0.0/schema.json'],
assets: {
red: {
href: 'https://example.com/red.tif',
type: 'image/tiff',
'eo:bands': [{name: 'red', common_name: 'red'}]
},
green: {
href: 'https://example.com/green.tif',
type: 'image/tiff',
'eo:bands': [{name: 'green', common_name: 'green'}]
},
blue: {
href: 'https://example.com/blue.tif',
type: 'image/tiff',
'eo:bands': [{name: 'blue', common_name: 'blue'}]
}
},
bounds: [-122.5, 37.5, -122.0, 38.0],
properties: {
datetime: '2023-01-01T00:00:00Z'
}
};
const MOCK_PMTILES_METADATA = {
metadataUrl: 'https://example.com/raster.pmtiles',
pmtilesType: 'raster',
minZoom: 0,
maxZoom: 18,
bounds: [-122.5, 37.5, -122.0, 38.0]
};
const MOCK_STAC_DATASET = {
type: 'raster-tile',
metadata: MOCK_STAC_METADATA,
label: 'Test STAC Dataset'
};
const MOCK_PMTILES_DATASET = {
type: 'raster-tile',
metadata: MOCK_PMTILES_METADATA,
label: 'Test PMTiles Dataset'
};
// Shared render options for tests
const createRenderOpts = (dataset, mapState = {dragRotate: false, bearing: 0, pitch: 0}) => ({
data: {
dataset,
tileSource: dataset.metadata.pmtilesType ? {getTileData: () => Promise.resolve(null)} : null
},
mapState,
interactionConfig: {tooltip: {enabled: true}}
});
test('#RasterTileLayer -> constructor and basic properties', t => {
const TEST_CASES = {
CREATE: [
{
props: {
dataId: 'raster-test-data',
isVisible: true,
label: 'test raster layer'
},
test: layer => {
t.equal(layer.config.dataId, 'raster-test-data', 'dataId should be correct');
t.equal(layer.type, 'rasterTile', 'type should be rasterTile');
t.equal(layer.isAggregated, false, 'should not be aggregated');
t.equal(layer.config.label, 'test raster layer', 'label should be correct');
t.equal(layer.config.isVisible, true, 'should be visible');
t.equal(layer.config.visConfig.opacity, 1, 'should have default opacity');
t.equal(
layer.config.visConfig.enableTerrain,
true,
'should have terrain enabled by default'
);
t.equal(
layer.config.visConfig.enableTerrainTopView,
false,
'should have terrain top view disabled'
);
t.equal(layer.requireData, true, 'should require data');
t.deepEqual(
layer.supportedDatasetTypes,
['raster-tile'],
'should support raster-tile dataset type'
);
t.equal(layer.name, 'Raster Tile', 'should have correct layer name');
t.ok(typeof layer.layerIcon === 'function', 'should have layerIcon function');
t.ok(layer.isValidToSave(), 'should be valid to save');
}
}
]
};
testCreateCases(t, RasterTileLayer, TEST_CASES.CREATE);
t.end();
});
test('#RasterTileLayer -> data formatting and rendering', t => {
const stacLayer = new RasterTileLayer({id: 'stac-layer', dataId: 'stac-data'});
const pmtilesLayer = new RasterTileLayer({id: 'pmtiles-layer', dataId: 'pmtiles-data'});
// Test STAC data formatting
const stacLayerData = stacLayer.formatLayerData({'stac-data': MOCK_STAC_DATASET});
t.ok(stacLayerData, 'should return STAC layer data');
t.equal(stacLayerData.dataset, MOCK_STAC_DATASET, 'should have correct STAC dataset');
t.equal(stacLayerData.tileSource, null, 'should have null tileSource for STAC');
// Test PMTiles data formatting
const pmtilesLayerData = pmtilesLayer.formatLayerData({'pmtiles-data': MOCK_PMTILES_DATASET});
t.ok(pmtilesLayerData, 'should return PMTiles layer data');
t.equal(pmtilesLayerData.dataset, MOCK_PMTILES_DATASET, 'should have correct PMTiles dataset');
t.ok(pmtilesLayerData.tileSource, 'should create tileSource for PMTiles');
// Test rendering
const stacLayers = stacLayer.renderLayer(createRenderOpts(MOCK_STAC_DATASET));
const pmtilesLayers = pmtilesLayer.renderLayer(createRenderOpts(MOCK_PMTILES_DATASET));
t.ok(Array.isArray(stacLayers), 'should return array for STAC');
t.ok(Array.isArray(pmtilesLayers), 'should return array for PMTiles');
t.equal(pmtilesLayers.length, 1, 'should render one deck layer for PMTiles');
t.end();
});
test('#RasterTileLayer -> configuration and visual settings', t => {
const layer = new RasterTileLayer({id: 'test-layer', dataId: 'stac-data'});
// Test layer config updates
layer.updateLayerConfig({isVisible: false, label: 'Updated Layer'});
t.equal(layer.config.isVisible, false, 'should update visibility');
t.equal(layer.config.label, 'Updated Layer', 'should update label');
// Test visual config updates
layer.updateLayerVisConfig({
opacity: 0.7,
enableTerrain: false,
enableTerrainTopView: true
});
t.equal(layer.config.visConfig.opacity, 0.7, 'should update opacity');
t.equal(layer.config.visConfig.enableTerrain, false, 'should update terrain');
t.equal(layer.config.visConfig.enableTerrainTopView, true, 'should update terrain top view');
// Test shouldRenderLayer
t.notOk(layer.shouldRenderLayer(), 'should not render when not visible');
layer.updateLayerConfig({isVisible: true});
t.ok(layer.shouldRenderLayer(), 'should render when visible');
t.end();
});
test('#RasterTileLayer -> metadata handling', t => {
const layer = new RasterTileLayer({id: 'test-layer', dataId: 'stac-data'});
// Test with non-raster dataset (should not throw)
t.doesNotThrow(() => {
layer.updateLayerMeta({type: 'csv'});
}, 'should handle non-raster dataset');
// Test with STAC and PMTiles datasets (should not throw)
t.doesNotThrow(() => {
layer.updateLayerMeta(MOCK_STAC_DATASET);
layer.updateLayerMeta(MOCK_PMTILES_DATASET);
}, 'should handle STAC and PMTiles datasets');
t.end();
});
test('#RasterTileLayer -> pixel value tracking', t => {
const layer = new RasterTileLayer({id: 'test-layer', dataId: 'stac-data'});
// Test initial state
t.equal(layer.minViewportPixelValue, Infinity, 'should have Infinity as initial min');
t.equal(layer.maxViewportPixelValue, -Infinity, 'should have -Infinity as initial max');
// Test with valid tiles
const mockTiles = [
{data: {minPixelValue: 25, maxPixelValue: 100}},
{data: {minPixelValue: 10, maxPixelValue: 150}}
];
layer.updateMinMaxPixelValue(mockTiles);
t.equal(layer.minViewportPixelValue, 10, 'should set correct min pixel value');
t.equal(layer.maxViewportPixelValue, 150, 'should set correct max pixel value');
// Test with empty tiles
layer.updateMinMaxPixelValue([]);
t.equal(layer.minViewportPixelValue, Infinity, 'should reset to Infinity for empty tiles');
t.equal(layer.maxViewportPixelValue, -Infinity, 'should reset to -Infinity for empty tiles');
// Test with null data
layer.updateMinMaxPixelValue([
{data: null},
{data: {minPixelValue: 30, maxPixelValue: 120}},
null
]);
t.equal(layer.minViewportPixelValue, 30, 'should handle null data gracefully');
t.equal(layer.maxViewportPixelValue, 120, 'should handle null data gracefully');
t.end();
});
test('#RasterTileLayer -> findDefaultLayerProps', t => {
// Test with raster dataset
const result = RasterTileLayer.findDefaultLayerProps(MOCK_STAC_DATASET);
t.ok(result.props, 'should return props object');
t.equal(result.props.length, 1, 'should return one layer prop');
t.equal(result.props[0].label, 'Test STAC Dataset', 'should use dataset label');
t.equal(result.props[0].isVisible, true, 'should be visible by default');
// Test with non-raster dataset
const nonRasterResult = RasterTileLayer.findDefaultLayerProps({type: 'csv'});
t.deepEqual(nonRasterResult.props, [], 'should return empty props for non-raster dataset');
t.end();
});
test('#RasterTileLayer -> edge cases and error handling', t => {
const layer = new RasterTileLayer({id: 'test-layer', dataId: 'stac-data'});
// Test formatLayerData with missing dataset
t.throws(() => {
layer.formatLayerData({});
}, 'should throw error when dataset missing');
// Test layer without dataId
const layerWithoutDataId = new RasterTileLayer({id: 'test-layer'});
layerWithoutDataId.config.dataId = undefined;
const layerData = layerWithoutDataId.formatLayerData({});
t.deepEqual(layerData, {}, 'should return empty object when no dataId');
// Test rendering with invalid/missing data
const invalidLayers = layer.renderLayer(createRenderOpts({metadata: {type: 'Feature'}}));
t.deepEqual(invalidLayers, [], 'should return empty array for invalid STAC data');
const noDataLayers = layer.renderLayer({...createRenderOpts(MOCK_STAC_DATASET), data: null});
t.deepEqual(noDataLayers, [], 'should return empty array when no data');
// Test malformed metadata (should not throw)
t.doesNotThrow(() => {
layer.updateLayerMeta({type: 'raster-tile', metadata: {type: 'Feature'}});
layer.updateLayerMeta({type: 'raster-tile', metadata: {pmtilesType: 'raster'}});
}, 'should handle malformed metadata gracefully');
t.end();
});
test('#RasterTileLayer -> data type variations', t => {
const layer = new RasterTileLayer({id: 'test-layer', dataId: 'stac-data'});
// Test multi-asset STAC
const multiAssetStac = {
type: 'raster-tile',
metadata: {
...MOCK_STAC_METADATA,
assets: {
...MOCK_STAC_METADATA.assets,
nir: {
href: 'https://example.com/nir.tif',
type: 'image/tiff',
'eo:bands': [{name: 'nir', common_name: 'nir'}]
}
}
}
};
const multiAssetData = layer.formatLayerData({'stac-data': multiAssetStac});
t.ok(multiAssetData, 'should handle multi-asset STAC data');
// Test single asset STAC
const singleAssetStac = {
type: 'raster-tile',
metadata: {
type: 'Feature',
stac_version: '1.0.0',
assets: {
visual: {href: 'https://example.com/visual.tif', type: 'image/tiff'}
},
bounds: [-122.5, 37.5, -122.0, 38.0]
}
};
const singleAssetData = layer.formatLayerData({'stac-data': singleAssetStac});
t.ok(singleAssetData, 'should handle single-asset STAC data');
t.end();
});
test('#RasterTileLayer -> layer serialization', t => {
const layer = new RasterTileLayer({
id: 'test-layer',
dataId: 'stac-data',
label: 'Test Raster Layer'
});
// Configure layer
layer.updateLayerVisConfig({
opacity: 0.8,
enableTerrain: false,
colorRange: {
colors: ['#FF0000', '#00FF00'],
colorLegends: {water: [0, 0, 255]}
}
});
// Test layer properties
const config = layer.config;
t.ok(layer.id, 'should have id on layer');
t.ok(config.dataId, 'should have dataId in config');
t.ok(config.label, 'should have label in config');
t.equal(config.visConfig.opacity, 0.8, 'should preserve opacity');
t.equal(config.visConfig.enableTerrain, false, 'should preserve terrain setting');
t.ok(layer.isValidToSave(), 'should be valid to save');
t.end();
});