@deck.gl/aggregation-layers 8.8.7 → 8.9.0-alpha.2

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 (50) hide show
  1. package/dist/dist.dev.js +246 -246
  2. package/dist.min.js +1 -1
  3. package/package.json +2 -2
  4. package/typed/aggregation-layer.js +151 -0
  5. package/typed/contour-layer/contour-layer.js +234 -0
  6. package/typed/contour-layer/contour-utils.js +64 -0
  7. package/typed/contour-layer/marching-squares-codes.js +337 -0
  8. package/typed/contour-layer/marching-squares.js +147 -0
  9. package/typed/cpu-grid-layer/cpu-grid-layer.js +129 -0
  10. package/typed/cpu-grid-layer/grid-aggregator.js +141 -0
  11. package/typed/gpu-grid-layer/gpu-grid-cell-layer-fragment.glsl.js +34 -0
  12. package/typed/gpu-grid-layer/gpu-grid-cell-layer-vertex.glsl.js +141 -0
  13. package/typed/gpu-grid-layer/gpu-grid-cell-layer.js +143 -0
  14. package/typed/gpu-grid-layer/gpu-grid-layer.js +289 -0
  15. package/typed/grid-aggregation-layer.js +172 -0
  16. package/typed/grid-layer/grid-layer.js +60 -0
  17. package/typed/heatmap-layer/heatmap-layer-utils.js +78 -0
  18. package/typed/heatmap-layer/heatmap-layer.js +478 -0
  19. package/typed/heatmap-layer/max-fs.glsl.js +7 -0
  20. package/typed/heatmap-layer/max-vs.glsl.js +12 -0
  21. package/typed/heatmap-layer/triangle-layer-fragment.glsl.js +58 -0
  22. package/typed/heatmap-layer/triangle-layer-vertex.glsl.js +51 -0
  23. package/typed/heatmap-layer/triangle-layer.js +67 -0
  24. package/typed/heatmap-layer/weights-fs.glsl.js +19 -0
  25. package/typed/heatmap-layer/weights-vs.glsl.js +23 -0
  26. package/typed/hexagon-layer/hexagon-aggregator.js +112 -0
  27. package/typed/hexagon-layer/hexagon-layer.js +171 -0
  28. package/typed/index.js +33 -0
  29. package/typed/screen-grid-layer/screen-grid-cell-layer.js +159 -0
  30. package/typed/screen-grid-layer/screen-grid-layer-fragment.glsl.js +37 -0
  31. package/typed/screen-grid-layer/screen-grid-layer-vertex.glsl.js +83 -0
  32. package/typed/screen-grid-layer/screen-grid-layer.js +216 -0
  33. package/typed/types.js +1 -0
  34. package/typed/utils/aggregation-operation-utils.js +94 -0
  35. package/typed/utils/bin-sorter.d.ts +1 -1
  36. package/typed/utils/bin-sorter.js +167 -0
  37. package/typed/utils/color-utils.js +53 -0
  38. package/typed/utils/cpu-aggregator.js +417 -0
  39. package/typed/utils/gpu-grid-aggregation/aggregate-all-fs.glsl.js +39 -0
  40. package/typed/utils/gpu-grid-aggregation/aggregate-all-vs.glsl.js +53 -0
  41. package/typed/utils/gpu-grid-aggregation/aggregate-to-grid-fs.glsl.js +31 -0
  42. package/typed/utils/gpu-grid-aggregation/aggregate-to-grid-vs.glsl.js +77 -0
  43. package/typed/utils/gpu-grid-aggregation/gpu-grid-aggregator-constants.js +41 -0
  44. package/typed/utils/gpu-grid-aggregation/gpu-grid-aggregator-utils.js +1 -0
  45. package/typed/utils/gpu-grid-aggregation/gpu-grid-aggregator.js +562 -0
  46. package/typed/utils/gpu-grid-aggregation/transform-mean-vs.glsl.js +36 -0
  47. package/typed/utils/grid-aggregation-utils.js +114 -0
  48. package/typed/utils/prop-utils.js +28 -0
  49. package/typed/utils/resource-utils.js +40 -0
  50. package/typed/utils/scale-utils.js +184 -0
@@ -0,0 +1,289 @@
1
+ // Copyright (c) 2015 - 2017 Uber Technologies, Inc.
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ // THE SOFTWARE.
20
+ import { Buffer } from '@luma.gl/core';
21
+ import GL from '@luma.gl/constants';
22
+ import { log } from '@deck.gl/core';
23
+ import GPUGridAggregator from '../utils/gpu-grid-aggregation/gpu-grid-aggregator';
24
+ import { AGGREGATION_OPERATION } from '../utils/aggregation-operation-utils';
25
+ import { defaultColorRange, colorRangeToFlatArray } from '../utils/color-utils';
26
+ import GPUGridCellLayer from './gpu-grid-cell-layer';
27
+ import { pointToDensityGridDataCPU } from './../cpu-grid-layer/grid-aggregator';
28
+ import GridAggregationLayer from '../grid-aggregation-layer';
29
+ import { getBoundingBox, getGridParams } from '../utils/grid-aggregation-utils';
30
+ const defaultProps = {
31
+ // color
32
+ colorDomain: null,
33
+ colorRange: defaultColorRange,
34
+ getColorWeight: { type: 'accessor', value: 1 },
35
+ colorAggregation: 'SUM',
36
+ // elevation
37
+ elevationDomain: null,
38
+ elevationRange: [0, 1000],
39
+ getElevationWeight: { type: 'accessor', value: 1 },
40
+ elevationAggregation: 'SUM',
41
+ elevationScale: { type: 'number', min: 0, value: 1 },
42
+ // grid
43
+ cellSize: { type: 'number', min: 1, max: 1000, value: 1000 },
44
+ coverage: { type: 'number', min: 0, max: 1, value: 1 },
45
+ getPosition: { type: 'accessor', value: x => x.position },
46
+ extruded: false,
47
+ // Optional material for 'lighting' shader module
48
+ material: true
49
+ };
50
+ // This layer only perform GPU aggregation, no need to seperate data and weight props
51
+ // aggregation will be dirty when any of the props are changed.
52
+ const DIMENSIONS = {
53
+ data: {
54
+ props: ['cellSize', 'colorAggregation', 'elevationAggregation']
55
+ }
56
+ // rest of the changes are detected by `state.attributesChanged`
57
+ };
58
+ const POSITION_ATTRIBUTE_NAME = 'positions';
59
+ /**
60
+ * The `GPUGridLayer` renders a grid heatmap based on an array of inputs.
61
+ */
62
+ export default class GPUGridLayer extends GridAggregationLayer {
63
+ initializeState({ gl }) {
64
+ const isSupported = GPUGridAggregator.isSupported(gl);
65
+ if (!isSupported) {
66
+ log.error('GPUGridLayer is not supported on this browser, use GridLayer instead')();
67
+ }
68
+ super.initializeAggregationLayer({
69
+ dimensions: DIMENSIONS
70
+ });
71
+ this.setState({
72
+ gpuAggregation: true,
73
+ projectPoints: false,
74
+ isSupported,
75
+ weights: {
76
+ color: {
77
+ needMin: true,
78
+ needMax: true,
79
+ combineMaxMin: true,
80
+ maxMinBuffer: new Buffer(gl, {
81
+ byteLength: 4 * 4,
82
+ accessor: { size: 4, type: GL.FLOAT, divisor: 1 }
83
+ })
84
+ },
85
+ elevation: {
86
+ needMin: true,
87
+ needMax: true,
88
+ combineMaxMin: true,
89
+ maxMinBuffer: new Buffer(gl, {
90
+ byteLength: 4 * 4,
91
+ accessor: { size: 4, type: GL.FLOAT, divisor: 1 }
92
+ })
93
+ }
94
+ },
95
+ positionAttributeName: 'positions'
96
+ });
97
+ const attributeManager = this.getAttributeManager();
98
+ attributeManager.add({
99
+ [POSITION_ATTRIBUTE_NAME]: {
100
+ size: 3,
101
+ accessor: 'getPosition',
102
+ type: GL.DOUBLE,
103
+ fp64: this.use64bitPositions()
104
+ },
105
+ color: { size: 3, accessor: 'getColorWeight' },
106
+ elevation: { size: 3, accessor: 'getElevationWeight' }
107
+ });
108
+ }
109
+ updateState(opts) {
110
+ if (this.state.isSupported === false) {
111
+ // Skip update, layer not supported
112
+ return;
113
+ }
114
+ super.updateState(opts);
115
+ const { aggregationDirty } = this.state;
116
+ if (aggregationDirty) {
117
+ // reset cached CPU Aggregation results (used for picking)
118
+ this.setState({
119
+ gridHash: null
120
+ });
121
+ }
122
+ }
123
+ getHashKeyForIndex(index) {
124
+ const { numRow, numCol, boundingBox, gridOffset } = this.state;
125
+ const gridSize = [numCol, numRow];
126
+ const gridOrigin = [boundingBox.xMin, boundingBox.yMin];
127
+ const cellSize = [gridOffset.xOffset, gridOffset.yOffset];
128
+ const yIndex = Math.floor(index / gridSize[0]);
129
+ const xIndex = index - yIndex * gridSize[0];
130
+ // This will match the index to the hash-key to access aggregation data from CPU aggregation results.
131
+ const latIdx = Math.floor((yIndex * cellSize[1] + gridOrigin[1] + 90 + cellSize[1] / 2) / cellSize[1]);
132
+ const lonIdx = Math.floor((xIndex * cellSize[0] + gridOrigin[0] + 180 + cellSize[0] / 2) / cellSize[0]);
133
+ return `${latIdx}-${lonIdx}`;
134
+ }
135
+ getPositionForIndex(index) {
136
+ const { numRow, numCol, boundingBox, gridOffset } = this.state;
137
+ const gridSize = [numCol, numRow];
138
+ const gridOrigin = [boundingBox.xMin, boundingBox.yMin];
139
+ const cellSize = [gridOffset.xOffset, gridOffset.yOffset];
140
+ const yIndex = Math.floor(index / gridSize[0]);
141
+ const xIndex = index - yIndex * gridSize[0];
142
+ const yPos = yIndex * cellSize[1] + gridOrigin[1];
143
+ const xPos = xIndex * cellSize[0] + gridOrigin[0];
144
+ return [xPos, yPos];
145
+ }
146
+ getPickingInfo({ info, mode }) {
147
+ const { index } = info;
148
+ let object = null;
149
+ if (index >= 0) {
150
+ const { gpuGridAggregator } = this.state;
151
+ const position = this.getPositionForIndex(index);
152
+ const colorInfo = GPUGridAggregator.getAggregationData({
153
+ pixelIndex: index,
154
+ ...gpuGridAggregator.getData('color')
155
+ });
156
+ const elevationInfo = GPUGridAggregator.getAggregationData({
157
+ pixelIndex: index,
158
+ ...gpuGridAggregator.getData('elevation')
159
+ });
160
+ object = {
161
+ colorValue: colorInfo.cellWeight,
162
+ elevationValue: elevationInfo.cellWeight,
163
+ count: colorInfo.cellCount || elevationInfo.cellCount,
164
+ position,
165
+ totalCount: colorInfo.totalCount || elevationInfo.totalCount
166
+ };
167
+ if (mode !== 'hover') {
168
+ // perform CPU aggregation for full list of points for each cell
169
+ const { props } = this;
170
+ let { gridHash } = this.state;
171
+ if (!gridHash) {
172
+ const { gridOffset, translation, boundingBox } = this.state;
173
+ const { viewport } = this.context;
174
+ const attributes = this.getAttributes();
175
+ const cpuAggregation = pointToDensityGridDataCPU(props, {
176
+ gridOffset,
177
+ attributes,
178
+ viewport,
179
+ translation,
180
+ boundingBox
181
+ });
182
+ gridHash = cpuAggregation.gridHash;
183
+ this.setState({ gridHash });
184
+ }
185
+ const key = this.getHashKeyForIndex(index);
186
+ const cpuAggregationData = gridHash[key];
187
+ Object.assign(object, cpuAggregationData);
188
+ }
189
+ }
190
+ // override object with picked cell
191
+ info.picked = Boolean(object);
192
+ info.object = object;
193
+ return info;
194
+ }
195
+ renderLayers() {
196
+ if (!this.state.isSupported) {
197
+ return null;
198
+ }
199
+ const { elevationScale, extruded, cellSize: cellSizeMeters, coverage, material, elevationRange, colorDomain, elevationDomain } = this.props;
200
+ const { weights, numRow, numCol, gridOrigin, gridOffset } = this.state;
201
+ const { color, elevation } = weights;
202
+ const colorRange = colorRangeToFlatArray(this.props.colorRange);
203
+ const SubLayerClass = this.getSubLayerClass('gpu-grid-cell', GPUGridCellLayer);
204
+ return new SubLayerClass({
205
+ gridSize: [numCol, numRow],
206
+ gridOrigin,
207
+ gridOffset: [gridOffset.xOffset, gridOffset.yOffset],
208
+ colorRange,
209
+ elevationRange,
210
+ colorDomain,
211
+ elevationDomain,
212
+ cellSize: cellSizeMeters,
213
+ coverage,
214
+ material,
215
+ elevationScale,
216
+ extruded
217
+ }, this.getSubLayerProps({
218
+ id: 'gpu-grid-cell'
219
+ }), {
220
+ data: {
221
+ attributes: {
222
+ colors: color.aggregationBuffer,
223
+ elevations: elevation.aggregationBuffer
224
+ }
225
+ },
226
+ colorMaxMinBuffer: color.maxMinBuffer,
227
+ elevationMaxMinBuffer: elevation.maxMinBuffer,
228
+ numInstances: numCol * numRow
229
+ });
230
+ }
231
+ finalizeState(context) {
232
+ const { color, elevation } = this.state.weights;
233
+ [color, elevation].forEach(weight => {
234
+ const { aggregationBuffer, maxMinBuffer } = weight;
235
+ maxMinBuffer.delete();
236
+ aggregationBuffer?.delete();
237
+ });
238
+ super.finalizeState(context);
239
+ }
240
+ // Aggregation Overrides
241
+ updateAggregationState(opts) {
242
+ const { props, oldProps } = opts;
243
+ const { cellSize, coordinateSystem } = props;
244
+ const { viewport } = this.context;
245
+ const cellSizeChanged = oldProps.cellSize !== cellSize;
246
+ const { dimensions } = this.state;
247
+ const positionsChanged = this.isAttributeChanged(POSITION_ATTRIBUTE_NAME);
248
+ // any attribute changed
249
+ const attributesChanged = positionsChanged || this.isAttributeChanged();
250
+ let { boundingBox } = this.state;
251
+ if (positionsChanged) {
252
+ boundingBox = getBoundingBox(this.getAttributes(), this.getNumInstances());
253
+ this.setState({ boundingBox });
254
+ }
255
+ if (positionsChanged || cellSizeChanged) {
256
+ const { gridOffset, translation, width, height, numCol, numRow } = getGridParams(boundingBox, cellSize, viewport, coordinateSystem);
257
+ this.allocateResources(numRow, numCol);
258
+ this.setState({
259
+ gridOffset,
260
+ translation,
261
+ gridOrigin: [-1 * translation[0], -1 * translation[1]],
262
+ width,
263
+ height,
264
+ numCol,
265
+ numRow
266
+ });
267
+ }
268
+ const aggregationDataDirty = attributesChanged ||
269
+ this.isAggregationDirty(opts, {
270
+ dimension: dimensions.data,
271
+ compareAll: true
272
+ });
273
+ if (aggregationDataDirty) {
274
+ this._updateAccessors(opts);
275
+ }
276
+ this.setState({
277
+ aggregationDataDirty
278
+ });
279
+ }
280
+ // Private
281
+ _updateAccessors(opts) {
282
+ const { colorAggregation, elevationAggregation } = opts.props;
283
+ const { color, elevation } = this.state.weights;
284
+ color.operation = AGGREGATION_OPERATION[colorAggregation];
285
+ elevation.operation = AGGREGATION_OPERATION[elevationAggregation];
286
+ }
287
+ }
288
+ GPUGridLayer.layerName = 'GPUGridLayer';
289
+ GPUGridLayer.defaultProps = defaultProps;
@@ -0,0 +1,172 @@
1
+ // Copyright (c) 2015 - 2017 Uber Technologies, Inc.
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ // THE SOFTWARE.
20
+ import AggregationLayer from './aggregation-layer';
21
+ import GPUGridAggregator from './utils/gpu-grid-aggregation/gpu-grid-aggregator';
22
+ import { Buffer } from '@luma.gl/core';
23
+ import { log } from '@deck.gl/core';
24
+ import GL from '@luma.gl/constants';
25
+ import BinSorter from './utils/bin-sorter';
26
+ import { pointToDensityGridDataCPU } from './cpu-grid-layer/grid-aggregator';
27
+ export default class GridAggregationLayer extends AggregationLayer {
28
+ initializeAggregationLayer({ dimensions }) {
29
+ const { gl } = this.context;
30
+ super.initializeAggregationLayer(dimensions);
31
+ this.setState({
32
+ // CPU aggregation results
33
+ layerData: {},
34
+ gpuGridAggregator: new GPUGridAggregator(gl, { id: `${this.id}-gpu-aggregator` }),
35
+ cpuGridAggregator: pointToDensityGridDataCPU
36
+ });
37
+ }
38
+ updateState(opts) {
39
+ // get current attributes
40
+ super.updateState(opts);
41
+ this.updateAggregationState(opts);
42
+ const { aggregationDataDirty, aggregationWeightsDirty, gpuAggregation } = this.state;
43
+ if (this.getNumInstances() <= 0) {
44
+ return;
45
+ }
46
+ let aggregationDirty = false;
47
+ // CPU aggregation is two steps
48
+ // 1. Create bins (based on cellSize and position) 2. Aggregate weights for each bin
49
+ // For GPU aggregation both above steps are combined into one step
50
+ // step-1
51
+ if (aggregationDataDirty || (gpuAggregation && aggregationWeightsDirty)) {
52
+ this._updateAggregation(opts);
53
+ aggregationDirty = true;
54
+ }
55
+ // step-2 (Applicalbe for CPU aggregation only)
56
+ if (!gpuAggregation && (aggregationDataDirty || aggregationWeightsDirty)) {
57
+ this._updateWeightBins();
58
+ this._uploadAggregationResults();
59
+ aggregationDirty = true;
60
+ }
61
+ this.setState({ aggregationDirty });
62
+ }
63
+ finalizeState(context) {
64
+ const { count } = this.state.weights;
65
+ if (count && count.aggregationBuffer) {
66
+ count.aggregationBuffer.delete();
67
+ }
68
+ this.state.gpuGridAggregator?.delete();
69
+ super.finalizeState(context);
70
+ }
71
+ updateShaders(shaders) {
72
+ if (this.state.gpuAggregation) {
73
+ this.state.gpuGridAggregator.updateShaders(shaders);
74
+ }
75
+ }
76
+ // Methods that can be overriden by subclasses for customizations
77
+ updateAggregationState(opts) {
78
+ // Sublayers should implement this method.
79
+ log.assert(false);
80
+ }
81
+ allocateResources(numRow, numCol) {
82
+ if (this.state.numRow !== numRow || this.state.numCol !== numCol) {
83
+ const dataBytes = numCol * numRow * 4 * 4;
84
+ const gl = this.context.gl;
85
+ const { weights } = this.state;
86
+ for (const name in weights) {
87
+ const weight = weights[name];
88
+ if (weight.aggregationBuffer) {
89
+ weight.aggregationBuffer.delete();
90
+ }
91
+ weight.aggregationBuffer = new Buffer(gl, {
92
+ byteLength: dataBytes,
93
+ accessor: {
94
+ size: 4,
95
+ type: GL.FLOAT,
96
+ divisor: 1
97
+ }
98
+ });
99
+ }
100
+ }
101
+ }
102
+ updateResults({ aggregationData, maxMinData, maxData, minData }) {
103
+ const { count } = this.state.weights;
104
+ if (count) {
105
+ count.aggregationData = aggregationData;
106
+ count.maxMinData = maxMinData;
107
+ count.maxData = maxData;
108
+ count.minData = minData;
109
+ }
110
+ }
111
+ // Private
112
+ _updateAggregation(opts) {
113
+ const { cpuGridAggregator, gpuGridAggregator, gridOffset, posOffset, translation = [0, 0], scaling = [0, 0, 0], boundingBox, projectPoints, gpuAggregation, numCol, numRow } = this.state;
114
+ const { props } = opts;
115
+ const { viewport } = this.context;
116
+ const attributes = this.getAttributes();
117
+ const vertexCount = this.getNumInstances();
118
+ if (!gpuAggregation) {
119
+ const result = cpuGridAggregator(props, {
120
+ gridOffset,
121
+ projectPoints,
122
+ attributes,
123
+ viewport,
124
+ posOffset,
125
+ boundingBox
126
+ });
127
+ this.setState({
128
+ layerData: result
129
+ });
130
+ }
131
+ else {
132
+ const { weights } = this.state;
133
+ gpuGridAggregator.run({
134
+ weights,
135
+ cellSize: [gridOffset.xOffset, gridOffset.yOffset],
136
+ numCol,
137
+ numRow,
138
+ translation,
139
+ scaling,
140
+ vertexCount,
141
+ projectPoints,
142
+ attributes,
143
+ moduleSettings: this.getModuleSettings()
144
+ });
145
+ }
146
+ }
147
+ _updateWeightBins() {
148
+ const { getValue } = this.state;
149
+ const sortedBins = new BinSorter(this.state.layerData.data || [], { getValue });
150
+ this.setState({ sortedBins });
151
+ }
152
+ _uploadAggregationResults() {
153
+ const { numCol, numRow } = this.state;
154
+ const { data } = this.state.layerData;
155
+ const { aggregatedBins, minValue, maxValue, totalCount } = this.state.sortedBins;
156
+ const ELEMENTCOUNT = 4;
157
+ const aggregationSize = numCol * numRow * ELEMENTCOUNT;
158
+ const aggregationData = new Float32Array(aggregationSize).fill(0);
159
+ for (const bin of aggregatedBins) {
160
+ const { lonIdx, latIdx } = data[bin.i];
161
+ const { value, counts } = bin;
162
+ const cellIndex = (lonIdx + latIdx * numCol) * ELEMENTCOUNT;
163
+ aggregationData[cellIndex] = value;
164
+ aggregationData[cellIndex + ELEMENTCOUNT - 1] = counts;
165
+ }
166
+ const maxMinData = new Float32Array([maxValue, 0, 0, minValue]);
167
+ const maxData = new Float32Array([maxValue, 0, 0, totalCount]);
168
+ const minData = new Float32Array([minValue, 0, 0, totalCount]);
169
+ this.updateResults({ aggregationData, maxMinData, maxData, minData });
170
+ }
171
+ }
172
+ GridAggregationLayer.layerName = 'GridAggregationLayer';
@@ -0,0 +1,60 @@
1
+ import { CompositeLayer } from '@deck.gl/core';
2
+ import GPUGridAggregator from '../utils/gpu-grid-aggregation/gpu-grid-aggregator';
3
+ import GPUGridLayer from '../gpu-grid-layer/gpu-grid-layer';
4
+ import CPUGridLayer from '../cpu-grid-layer/cpu-grid-layer';
5
+ const defaultProps = {
6
+ ...GPUGridLayer.defaultProps,
7
+ ...CPUGridLayer.defaultProps,
8
+ gpuAggregation: false
9
+ };
10
+ export default class GridLayer extends CompositeLayer {
11
+ initializeState() {
12
+ this.state = {
13
+ useGPUAggregation: true
14
+ };
15
+ }
16
+ updateState({ props }) {
17
+ this.setState({
18
+ useGPUAggregation: this.canUseGPUAggregation(props)
19
+ });
20
+ }
21
+ renderLayers() {
22
+ const { data, updateTriggers } = this.props;
23
+ const id = this.state.useGPUAggregation ? 'GPU' : 'CPU';
24
+ const LayerType = this.state.useGPUAggregation
25
+ ? this.getSubLayerClass('GPU', GPUGridLayer)
26
+ : this.getSubLayerClass('CPU', CPUGridLayer);
27
+ return new LayerType(this.props, this.getSubLayerProps({
28
+ id,
29
+ updateTriggers
30
+ }), {
31
+ data
32
+ });
33
+ }
34
+ // Private methods
35
+ canUseGPUAggregation(props) {
36
+ const { gpuAggregation, lowerPercentile, upperPercentile, getColorValue, getElevationValue, colorScaleType } = props;
37
+ if (!gpuAggregation) {
38
+ // cpu aggregation is requested
39
+ return false;
40
+ }
41
+ if (!GPUGridAggregator.isSupported(this.context.gl)) {
42
+ return false;
43
+ }
44
+ if (lowerPercentile !== 0 || upperPercentile !== 100) {
45
+ // percentile calculations requires sorting not supported on GPU
46
+ return false;
47
+ }
48
+ if (getColorValue !== null || getElevationValue !== null) {
49
+ // accessor for custom color or elevation calculation is specified
50
+ return false;
51
+ }
52
+ if (colorScaleType === 'quantile' || colorScaleType === 'ordinal') {
53
+ // quantile and ordinal scales are not supported on GPU
54
+ return false;
55
+ }
56
+ return true;
57
+ }
58
+ }
59
+ GridLayer.layerName = 'GridLayer';
60
+ GridLayer.defaultProps = defaultProps;
@@ -0,0 +1,78 @@
1
+ import GL from '@luma.gl/constants';
2
+ import { isWebGL2 } from '@luma.gl/core';
3
+ export function getBounds(points) {
4
+ // Now build bounding box in world space (aligned to world coordiante system)
5
+ const x = points.map(p => p[0]);
6
+ const y = points.map(p => p[1]);
7
+ const xMin = Math.min.apply(null, x);
8
+ const xMax = Math.max.apply(null, x);
9
+ const yMin = Math.min.apply(null, y);
10
+ const yMax = Math.max.apply(null, y);
11
+ return [xMin, yMin, xMax, yMax];
12
+ }
13
+ // true if currentBounds contains targetBounds, false otherwise
14
+ export function boundsContain(currentBounds, targetBounds) {
15
+ if (targetBounds[0] >= currentBounds[0] &&
16
+ targetBounds[2] <= currentBounds[2] &&
17
+ targetBounds[1] >= currentBounds[1] &&
18
+ targetBounds[3] <= currentBounds[3]) {
19
+ return true;
20
+ }
21
+ return false;
22
+ }
23
+ const scratchArray = new Float32Array(12);
24
+ // For given rectangle bounds generates two triangles vertices that coverit completely
25
+ export function packVertices(points, dimensions = 2) {
26
+ let index = 0;
27
+ for (const point of points) {
28
+ for (let i = 0; i < dimensions; i++) {
29
+ scratchArray[index++] = point[i] || 0;
30
+ }
31
+ }
32
+ return scratchArray;
33
+ }
34
+ // Expands boundingBox:[xMin, yMin, xMax, yMax] to match aspect ratio of given width and height
35
+ export function scaleToAspectRatio(boundingBox, width, height) {
36
+ const [xMin, yMin, xMax, yMax] = boundingBox;
37
+ const currentWidth = xMax - xMin;
38
+ const currentHeight = yMax - yMin;
39
+ let newWidth = currentWidth;
40
+ let newHeight = currentHeight;
41
+ if (currentWidth / currentHeight < width / height) {
42
+ // expand bounding box width
43
+ newWidth = (width / height) * currentHeight;
44
+ }
45
+ else {
46
+ newHeight = (height / width) * currentWidth;
47
+ }
48
+ if (newWidth < width) {
49
+ newWidth = width;
50
+ newHeight = height;
51
+ }
52
+ const xCenter = (xMax + xMin) / 2;
53
+ const yCenter = (yMax + yMin) / 2;
54
+ return [
55
+ xCenter - newWidth / 2,
56
+ yCenter - newHeight / 2,
57
+ xCenter + newWidth / 2,
58
+ yCenter + newHeight / 2
59
+ ];
60
+ }
61
+ // Get texture coordiante of point inside a bounding box
62
+ export function getTextureCoordinates(point, bounds) {
63
+ const [xMin, yMin, xMax, yMax] = bounds;
64
+ return [(point[0] - xMin) / (xMax - xMin), (point[1] - yMin) / (yMax - yMin)];
65
+ }
66
+ // Returns format and type for creating texture objects
67
+ export function getTextureParams({ gl, floatTargetSupport }) {
68
+ return floatTargetSupport
69
+ ? {
70
+ // format: should be RGBA32F on WebGL2 (float textures), RGBA in WebGL1 for float or non float textures
71
+ format: isWebGL2(gl) ? GL.RGBA32F : GL.RGBA,
72
+ type: GL.FLOAT
73
+ }
74
+ : {
75
+ format: GL.RGBA,
76
+ type: GL.UNSIGNED_BYTE
77
+ };
78
+ }