-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
h3-hexagon-layer-specs.js
418 lines (389 loc) · 11.7 KB
/
h3-hexagon-layer-specs.js
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright (c) 2023 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import test from 'tape';
import moment from 'moment';
import {
testCreateCases,
testFormatLayerDataCases,
testRenderLayerCases,
preparedDataset,
dataId,
testRows,
preparedFilterDomain0,
hexagonIdLayerMeta
} from 'test/helpers/layer-utils';
import {KeplerGlLayers, h3DefaultElevation as defaultElevation} from '@kepler.gl/layers';
import {getCentroid, idToPolygonGeo} from '@kepler.gl/utils';
import {copyTableAndUpdate} from '@kepler.gl/table';
const {H3Layer} = KeplerGlLayers;
const columns = {
lat: 'lat',
lng: 'lng',
hex_id: 'hex_id'
};
test('#H3Layer -> constructor', t => {
const TEST_CASES = {
CREATE: [
{
props: {
dataId: 'smoothie',
isVisible: true,
label: 'test h3 layer'
},
test: layer => {
t.ok(layer.config.dataId === 'smoothie', 'H3Layer dataId should be correct');
t.ok(layer.type === 'hexagonId', 'type should be h3');
t.ok(layer.isAggregated === false, 'H3Layer is not aggregated');
t.ok(layer.config.label === 'test h3 layer', 'label should be correct');
}
}
]
};
testCreateCases(t, H3Layer, TEST_CASES.CREATE);
t.end();
});
test('#H3Layer -> formatLayerData', t => {
const filteredIndex = [0, 2, 4];
const TEST_CASES = [
{
name: 'h3.1',
layer: {
config: {
dataId,
label: 'gps point h3',
columns,
color: [2, 3, 4]
},
type: 'hexagonId',
id: 'test_layer_1'
},
datasets: {
[dataId]: copyTableAndUpdate(preparedDataset, {filteredIndex})
},
assert: result => {
const {layerData, layer} = result;
const expectedLayerData = {
data: [
{
index: 0,
id: '89283082c2fffff',
centroid: getCentroid({id: '89283082c2fffff'})
},
{
index: 4,
id: '89283082c3bffff',
centroid: getCentroid({id: '89283082c3bffff'})
}
],
getElevation: () => {},
getFilterValue: () => {},
getFillColor: () => {},
getLineColor: () => {},
getHexId: () => {},
getCoverage: () => {},
getPosition: () => {},
textLabels: () => {}
};
t.deepEqual(
Object.keys(layerData).sort(),
Object.keys(expectedLayerData).sort(),
`layerData should have ${expectedLayerData.length} keys`
);
t.deepEqual(
layerData.data,
expectedLayerData.data,
'should format correct point layerData data'
);
// getFillColor
t.deepEqual(layerData.getFillColor, [2, 3, 4], 'getFillColor should be a constant');
// getLineColor
t.deepEqual(layerData.getLineColor, [2, 3, 4], 'getLineColor should be a constant');
// getElevation
t.deepEqual(layerData.getElevation, defaultElevation, 'getElevation should be a constant');
// getHexId
t.deepEqual(
layerData.data.map(layerData.getHexId),
['89283082c2fffff', '89283082c3bffff'],
'getHexId should return correct hex id'
);
// getFilterValue
t.deepEqual(
layerData.data.map(layerData.getFilterValue),
[
[Number.MIN_SAFE_INTEGER, 0, 0, 0],
[moment.utc(testRows[4][0]).valueOf() - preparedFilterDomain0, 0, 0, 0]
],
'getFilterValue should return [value, 0, 0, 0]'
);
// layerMeta
t.deepEqual(layer.meta, hexagonIdLayerMeta, 'should format correct point layer meta');
}
},
{
name: 'H3 layer format data. with colorField, strokeColorField, and sizeField',
layer: {
config: {
dataId,
label: 'h3.2',
columns,
color: [10, 10, 10],
// fill color by types(string)
colorField: {
type: 'string',
name: 'types'
},
// stroke color by types(string)
strokeColorField: {
type: 'string',
name: 'types'
},
// size by id(integer)
sizeField: {
type: 'real',
name: 'trip_distance'
},
visConfig: {
colorRange: {
colors: ['#010101', '#020202', '#030303']
},
strokeColorRange: {
colors: ['#010101', '#020202', '#030303']
},
elevationRange: [10, 20],
enable3d: true
}
},
type: 'hexagonId',
id: 'test_layer_2'
},
datasets: {
[dataId]: copyTableAndUpdate(preparedDataset, {filteredIndex})
},
assert: result => {
const {layerData} = result;
// getSourceColor
// domain: ['driver_analytics', 'driver_analytics_0', 'driver_gps']
// range ['#010101', '#020202', '#030303']
t.deepEqual(
layerData.data.map(layerData.getFillColor),
[
[2, 2, 2],
[1, 1, 1]
],
'getFillColor should be correct'
);
t.deepEqual(
layerData.data.map(layerData.getLineColor),
[
[2, 2, 2],
[1, 1, 1]
],
'getLineColor should be correct'
);
// getElevation
// domain: [1.59, 11]
// range: [0, 500]
// value [1.59, 2.37]
t.deepEqual(
layerData.data.map(layerData.getElevation),
[0, 41.445270988310305],
'getElevation should correct'
);
// getFilterValue
t.deepEqual(
layerData.data.map(layerData.getFilterValue),
[
[Number.MIN_SAFE_INTEGER, 0, 0, 0],
[moment.utc(testRows[4][0]).valueOf() - preparedFilterDomain0, 0, 0, 0]
],
'getFilterValue should return [value, 0, 0, 0]'
);
}
}
];
testFormatLayerDataCases(t, H3Layer, TEST_CASES);
t.end();
});
test('#H3Layer -> renderLayer', t => {
const filteredIndex = [0, 2, 4];
const TEST_CASES = [
{
name: 'Test render h3.2',
layer: {
id: 'test_layer_1',
type: 'hexagonId',
config: {
dataId,
label: 'h3 hex',
columns,
color: [1, 2, 3],
visConfig: {
worldUnitSize: 0.5,
elevationScale: 5
}
}
},
datasets: {
[dataId]: copyTableAndUpdate(preparedDataset, {filteredIndex})
},
assert: (deckLayers, layer) => {
t.equal(layer.type, 'hexagonId', 'should create 1 hexagonId layer');
t.equal(deckLayers.length, 2, 'Should create 2 deck.gl layers');
const expectedLayerIds = ['test_layer_1', 'test_layer_1-hexagon-cell'];
t.deepEqual(
deckLayers.map(l => l.id),
expectedLayerIds,
'should create 1 composite, 1 hexagon-cell layer'
);
const {props} = deckLayers[0];
const expectedProps = {
opacity: layer.config.visConfig.opacity,
filterRange: preparedDataset.gpuFilter.filterRange,
wrapLongitude: false,
coverage: 1,
autoHighlight: false,
highlightColor: [255, 255, 255, 60],
extruded: false,
elevationScale: 5,
filled: true,
stroked: false
};
Object.keys(expectedProps).forEach(key => {
t.deepEqual(props[key], expectedProps[key], `should have correct props.${key}`);
});
}
},
{
name: 'Test render h3.2 with text label',
layer: {
id: 'test_layer_2',
type: 'hexagonId',
config: {
dataId,
label: 'h3 hex',
columns,
color: [1, 2, 3],
visConfig: {
worldUnitSize: 0.5,
elevationScale: 5
},
textLabel: [
{
field: {
name: 'types',
format: ''
},
format: ''
}
]
}
},
datasets: {
[dataId]: preparedDataset
},
assert: (deckLayers, layer) => {
t.equal(layer.type, 'hexagonId', 'should create 1 hexagonId layer');
const expectedTextLabels = [
{
field: {
name: 'types',
id: 'types',
displayName: 'types',
format: '',
fieldIdx: 5,
type: 'string',
analyzerType: 'STRING',
valueAccessor: preparedDataset.fields[5].valueAccessor
},
color: [255, 255, 255],
size: 18,
offset: [0, 0],
anchor: 'middle',
alignment: 'center'
}
];
t.deepEqual(
layer.config.textLabel,
expectedTextLabels,
'should create textLabel using field "types"'
);
t.equal(deckLayers.length, 4, 'Should create 4 deck.gl layers');
const expectedLayerIds = [
'test_layer_2',
'test_layer_2-hexagon-cell',
'test_layer_2-label-types',
'test_layer_2-label-types-characters'
];
t.deepEqual(
deckLayers.map(l => l.id),
expectedLayerIds,
'should create 1 composite, 1 hexagon-cell layer, 1 text layer, 1 multi-icon layer'
);
}
}
];
testRenderLayerCases(t, H3Layer, TEST_CASES);
t.end();
});
test('#H3Layer -> idToPolygonGeo', t => {
const h3Object = {
index: 411,
id: '882a100d01fffff'
};
const hexagonOutline = idToPolygonGeo(h3Object);
const expectedCoordinates = [
[-73.9556604529423, 40.747207768842344],
[-73.96203986286912, 40.746155567377926],
[-73.96400718290255, 40.74169679631741],
[-73.95959600220597, 40.73829055843986],
[-73.95321761814058, 40.73934254051138],
[-73.9512493890226, 40.74380097982341],
[-73.9556604529423, 40.747207768842344]
];
const expectedHexagonOutline = {
type: 'Feature',
geometry: {
coordinates: expectedCoordinates,
type: 'LineString'
},
properties: undefined
};
t.deepEqual(
hexagonOutline,
expectedHexagonOutline,
'should generate geojson object of hexagon outline (LineString)'
);
const properties = {isClosed: true};
const hexagonPolygon = idToPolygonGeo(h3Object, properties);
const expectedHexagonPolygon = {
type: 'Feature',
geometry: {
coordinates: [expectedCoordinates],
type: 'Polygon'
},
properties: {isClosed: true}
};
t.deepEqual(
hexagonPolygon,
expectedHexagonPolygon,
'should generate geojson object of hexagon outline (LineString)'
);
t.end();
});