The Layer, Data, and Accessor Model
🎯 Introduction
By the end of this article, you will be able to map application data to point positions, colors, and sizes, and update deck.gl layers on the map through MapboxOverlay.
🧱 Layer, data, and id
A deck.gl layer declares how to draw data. data supplies the application records, id identifies the layer, and the remaining options determine how each record becomes a graphic.
const layer = new ScatterplotLayer({
id: 'stations',
data: stations,
getPosition: (station) => [station.lng, station.lat],
});
Keep each layer's id stable across updates. Typically, you create new layer configurations when data changes and pass the complete layer array to overlay.setProps. deck.gl compares layers by id and reuses GPU resources where possible.
⚡ How accessors map data
An accessor is a function that derives a graphical attribute from a data record. Common accessors include:
getPosition: returns[longitude, latitude]for a point's position.getFillColor: returns an RGB or RGBA array for the fill color.getRadius: returns the point's size, with units determined byradiusUnits.
A constant accessor applies the same value to every record:
getRadius: 120,
getFillColor: [37, 99, 235, 220],
A function accessor can calculate a value from the current record:
getRadius: (station) => station.value * 10,
getFillColor: (station) => station.value > 50
? [220, 38, 38, 220]
: [37, 99, 235, 220],
Mapbox does not read data or accessors. It provides the map space and camera; the deck.gl layer converts data into GPU attributes.
🛠 Use with MapboxOverlay
const layer = new ScatterplotLayer({
id: 'stations',
data,
getPosition: (item) => [item.lng, item.lat],
getRadius: (item) => item.value * 10,
getFillColor: (item) => item.value > 50
? [255, 0, 0, 220]
: [0, 120, 255, 220],
});
overlay.setProps({
layers: [layer],
});
Here, both point size and color are calculated from the level field, while Mapbox still provides the basemap:
🪤 Common mistakes
getPosition must return coordinates in [longitude, latitude] order. Reversing them to [latitude, longitude] usually places the graphics somewhere entirely different.
The meaning of getRadius also depends on radiusUnits. The default geographic units suit spatial radii, while pixels is better for keeping points at a fixed screen size. Choose based on how the points should behave as the map zooms.
🧾 Summary
- A layer describes rendering, data supplies application records, and id identifies the layer.
- Accessors convert fields into GPU attributes such as position, color, and size.
- Constant accessors provide uniform styling; function accessors calculate values per record.
overlay.setProps({ layers })updates deck.gl layers when using Mapbox integration.
❓ Questions and answers
Q1: Does Mapbox process deck.gl's data?
No. Mapbox manages the map and camera, while deck.gl layers read data and generate GPU attributes.
Q2: Why does a layer need a stable id?
deck.gl uses it to recognize that old and new configurations represent the same layer, allowing it to reuse resources and update changed attributes.
Q3: Can one field control both color and size?
Yes. Both getFillColor and getRadius can read the same field, as long as each accessor returns the type it requires.
🧪 Exercises
- Change
getRadiustoitem.level * 12and observe the point sizes. - Add a
valuefield and use it to switch colors between low and high values.
🎉 You now understand deck.gl's core data mapping model.
