The GridLayer renders a grid heatmap based on an array of inputs.
It takes the constant cell size and aggregates input objects into cells. The color
and height of a cell are determined based on the objects it contains.
This layer renders either a GPUGridLayer or a CPUGridLayer, depending on its props and whether GPU aggregation is supported. For more details check the GPU Aggregation section below.
GridLayer is a CompositeLayer.
import DeckGL from '@deck.gl/react';
import {GridLayer} from '@deck.gl/aggregation-layers';
function App({data, viewState}) {
/**
* Data format:
* [
* {COORDINATES: [-122.42177834, 37.78346622]},
* ...
* ]
*/
const layer = new GridLayer({
id: 'new-grid-layer',
data,
pickable: true,
extruded: true,
cellSize: 200,
elevationScale: 4,
getPosition: d => d.COORDINATES
});
return <DeckGL viewState={viewState}
layers={[layer]}
getTooltip={({object}) => object && `${object.position.join(', ')}\nCount: ${object.count}`} />;
}Note: The GridLayer at the moment only works with COORDINATE_SYSTEM.LNGLAT.
To install the dependencies from NPM:
npm install deck.gl
# or
npm install @deck.gl/core @deck.gl/layers @deck.gl/aggregation-layersimport {GridLayer} from '@deck.gl/aggregation-layers';
new GridLayer({});To use pre-bundled scripts:
<script src="https://unpkg.com/deck.gl@^8.0.0/dist.min.js"></script>
<!-- or -->
<script src="https://unpkg.com/@deck.gl/core@^8.0.0/dist.min.js"></script>
<script src="https://unpkg.com/@deck.gl/layers@^8.0.0/dist.min.js"></script>
<script src="https://unpkg.com/@deck.gl/aggregation-layers@^8.0.0/dist.min.js"></script>new deck.GridLayer({});Inherits from all Base Layer and CompositeLayer properties.
cellSize (Number, optional)1000Size of each cell in meters
colorDomain (Array, optional)[min(colorWeight), max(colorWeight)]Color scale domain, default is set to the extent of aggregated weights in each cell. You can control how the colors of cells are mapped to weights by passing in an arbitrary color domain. This is useful when you want to render different data input with the same color mapping for comparison.
colorRange (Array, optional)6-class YlOrRd 
Specified as an array of colors [color1, color2, ...]. Each color is an array of 3 or 4 values [R, G, B] or [R, G, B, A], representing intensities of Red, Green, Blue and Alpha channels. Each intensity is a value between 0 and 255. When Alpha not provided a value of 255 is used.
colorDomain is divided into colorRange.length equal segments, each mapped to one color in colorRange.
coverage (Number, optional) 1Cell size multiplier, clamped between 0 - 1. The displayed size of cell is calculated by coverage * cellSize.
Note: coverage does not affect how objects are binned.
elevationDomain (Array, optional)[0, max(elevationWeight)]Elevation scale input domain, default is set to between 0 and the max of aggregated weights in each cell. You can control how the elevations of cells are mapped to weights by passing in an arbitrary elevation domain. This is useful when you want to render different data input with the same elevation scale for comparison.
elevationRange (Array, optional)[0, 1000]Elevation scale output range
elevationScale (Number, optional) 1Cell elevation multiplier. This is a handy property to scale all cells without updating the data.
extruded (Boolean, optional)trueWhether to enable cell elevation.If set to false, all cell will be flat.
upperPercentile (Number, optional) 100Filter cells and re-calculate color by upperPercentile. Cells with value
larger than the upperPercentile will be hidden.
lowerPercentile (Number, optional) 0Filter cells and re-calculate color by lowerPercentile. Cells with value
smaller than the lowerPercentile will be hidden.
elevationUpperPercentile (Number, optional) 100Filter cells and re-calculate elevation by elevationUpperPercentile. Cells with elevation value
larger than the elevationUpperPercentile will be hidden.
elevationLowerPercentile (Number, optional) 100Filter cells and re-calculate elevation by elevationLowerPercentile. Cells with elevation value
smaller than the elevationLowerPercentile will be hidden.
colorScaleType (String, optional)Scaling function used to determine the color of the grid cell, default value is 'quantize'. Supported Values are 'quantize', 'linear', 'quantile' and 'ordinal'.
fp64 (Boolean, optional)falseWhether the aggregation should be performed in high-precision 64-bit mode. Note that since deck.gl v6.1, the default 32-bit projection uses a hybrid mode that matches 64-bit precision with significantly better performance.
gpuAggregation (bool, optional)falseWhen set to true, aggregation is performed on GPU, provided other conditions are met, for more details check the GPU Aggregation section below. GPU aggregation can be a lot faster than CPU depending upon the number of objects and number of cells.
Note: GPU Aggregation is faster only when using large data sets. For smaller data sets GPU Aggregation could be potentially slower than CPU Aggregation.
material (Object, optional)trueThis is an object that contains material props for lighting effect applied on extruded polygons. Check the lighting guide for configurable settings.
colorAggregation (String, optional)Defines the operation used to aggregate all data object weights to calculate a cell's color value. Valid values are 'SUM', 'MEAN', 'MIN' and 'MAX'. 'SUM' is used when an invalid value is provided.
getColorWeight and colorAggregation together determine the elevation value of each cell. If the getColorValue prop is supplied, they will be ignored. Note that supplying getColorValue disables GPU aggregation.
getColorValue
...
const layer = new CPUGridLayer({
id: 'my-grid-layer',
...
getColorValue: points => points.length,
...
});getColorWeight and colorAggregation
...
const layer = new CPUGridLayer({
id: 'my-grid-layer',
...
getColorWeight: point => 1,
colorAggregation: 'SUM'
...
});getColorValuefunction getMean(points) {
return points.reduce((sum, p) => sum += p.SPACES, 0) / points.length;
}
...
const layer = new CPUGridLayer({
id: 'my-grid-layer',
...
getColorValue: getMean,
...
});getColorWeight and colorAggregation...
const layer = new CPUGridLayer({
id: 'my-grid-layer',
...
getColorWeight: point => point.SPACES,
colorAggregation: 'SUM'
...
});If your use case requires aggregating using an operation that is not one of 'SUM', 'MEAN', 'MAX' and 'MIN', getColorValue should be used to define such custom aggregation function. In those cases GPU aggregation is not supported.
elevationAggregation (String, optional)Defines the operation used to aggregate all data object weights to calculate a cell's elevation value. Valid values are 'SUM', 'MEAN', 'MIN' and 'MAX'. 'SUM' is used when an invalid value is provided.
getElevationWeight and elevationAggregation together determine the elevation value of each cell. If the getElevationValue prop is supplied, they will be ignored. Note that supplying getElevationValue disables GPU aggregation.
getElevationValue...
const layer = new CPUGridLayer({
id: 'my-grid-layer',
...
getElevationValue: points => points.length
...
});getElevationWeight and elevationAggregation...
const layer = new CPUGridLayer({
id: 'my-grid-layer',
...
getElevationWeight: point => 1,
elevationAggregation: 'SUM'
...
});getElevationValuefunction getMax(points) {
return points.reduce((max, p) => p.SPACES > max ? p.SPACES : max, -Infinity);
}
...
const layer = new CPUGridLayer({
id: 'my-grid-layer',
...
getElevationValue: getMax,
...
});getElevationWeight and elevationAggregation...
const layer = new CPUGridLayer({
id: 'my-grid-layer',
...
getElevationWeight: point => point.SPACES,
elevationAggregation: 'MAX'
...
});If your use case requires aggregating using an operation that is not one of 'SUM', 'MEAN', 'MAX' and 'MIN', getElevationValue should be used to define such custom aggregation function. In those cases GPU aggregation is not supported.
getElevationValue (Function, optional) nullAfter data objects are aggregated into cells, this accessor is called on each cell to get the value that its elevation is based on. If supplied, this will override the effect of getElevationWeight and elevationAggregation props. Note that supplying this prop disables GPU aggregation.
Arguments:
objects (Array) - a list of objects whose positions fall inside this cell.objectInfo (Object) - contains the following fields:indices (Array) - the indices of objects in the original datadata - the value of the data prop.getPosition (Function, optional)object => object.positionMethod called to retrieve the position of each object.
getColorWeight (Function, optional) 1The weight of a data object used to calculate the color value for a cell.
getColorValue (Function, optional) nullAfter data objects are aggregated into cells, this accessor is called on each cell to get the value that its color is based on. If supplied, this will override the effect of getColorWeight and colorAggregation props. Note that supplying this prop disables GPU aggregation.
Arguments:
objects (Array) - a list of objects whose positions fall inside this cell.objectInfo (Object) - contains the following fields:indices (Array) - the indices of objects in the original datadata - the value of the data prop.getElevationWeight (Function, optional) 1The weight of a data object used to calculate the elevation value for a cell.
onSetColorDomain (Function, optional)([min, max]) => {}This callback will be called when cell color domain has been calculated.
onSetElevationDomain (Function, optional)([min, max]) => {}This callback will be called when cell elevation domain has been calculated.
The following table compares the performance between CPU and GPU aggregations using random data:
| #objects | CPU #iterations/sec | GPU #iterations/sec | Notes |
|---|---|---|---|
| 25K | 535 | 359 | GPU is 33% slower |
| 100K | 119 | 437 | GPU is 267% faster |
| 1M | 12.7 | 158 | GPU is 1144% faster |
Numbers are collected on a 2016 15-inch Macbook Pro (CPU: 2.8 GHz Intel Core i7 and GPU: AMD Radeon R9 M370X 2 GB)
This layer performs aggregation on GPU when the browser is using WebGL2 and the gpuAggregation prop is set to true, but will fallback to CPU in the following cases:
When following percentile props are set, it requires sorting of aggregated values, which cannot be supported when aggregating on GPU.
lowerPercentile, upperPercentile, elevationLowerPercentile and elevationUpperPercentile.When colorScaleType props is set to a 'quantile' or 'ordinal', aggregation will fallback to CPU. For GPU Aggregation, use 'quantize', 'linear'.
When following percentile props are set, it requires sorting of aggregated values, which cannot be supported when aggregating on GPU.
lowerPercentile, upperPercentile, elevationLowerPercentile and elevationUpperPercentile.When using GPU Aggregation, onSetColorDomain and onSetElevationDomain are not fired.
The GridLayer renders the following sublayers:
CPU - a CPUGridLayer when using CPU aggregation.
GPU - a GPUGridLayer when using GPU aggregation.