Loading Data
deck.gl uses loaders.gl, a framework-agnostic library to read data and resources.
deck.gl core always includes loaders for JSON and standard image formats (e.g, png, jpeg, svg). Certain layers include additional loaders supporting their own use cases. It is easy for applications to provide options to configure the behavior of the default loaders or to add loaders to support for additional formats.
Some examples of when loaders are used:
- JSON array or object from an URL passed to the
dataprop of a layer - Texture from an image, such as
imageinBitmapLayer,iconAtlasinIconLayer, andtextureinSimpleMeshLayer - Geometries from a binary tile, e.g.
MVTLayer,TerrainLayer, andTile3DLayer - Geometries from a standard 3D format, e.g.
scenegraphinScenegraphLayer, andmeshinSimpleMeshLayer
Customize Data Loading Behavior
All layers support a loadOptions prop that can be used to customize loading and parsing.
Example: Fetch data with credentials
In a production environment, deck.gl applications may need to load data from secure APIs that require special HTTP headers (such as Authorization) to be set.
In order to access a secure API, the loadOptions.fetch option passes through additional parameters to fetch, which deck.gl calls under the hood to load resources.
new ScatterplotLayer({
data: 'https://secure-server.com/userActivity',
loadOptions: {
fetch: {
method: 'POST',
body: JSON.stringify(requestBody),
headers: {
'Authorization': `Bearer ${accessToken}`,
}
}
}
});
Example: Override the default image loading options
deck.gl uses ImageLoader to read common image formats. The default loader options are:
{
image: {type: 'auto'},
imagebitmap: {premultiplyAlpha: 'none'}
}
The image is decoded into an ImageBitmap if the browser supports it (Firefox, Chrome, Edge) for better performance. You can override the default options for the createImageBitmap API as follows:
new IconLayer({
iconAtlas: '/path/to/image.png',
loadOptions: {
imagebitmap: {
// Flip the image vertically
imageOrientation: 'flipY'
}
}
})
If the image is a SVG that does not include width and height information, createImageBitmap will throw a DOMException:
The image element contains an SVG image without intrinsic dimensions, and no resize options or crop region are specified.
This can be fixed by explicitly setting its dimensions:
new IconLayer({
iconAtlas: '/path/to/image.svg',
loadOptions: {
imagebitmap: {
resizeWidth: 256,
resizeHeight: 256,
resizeQuality: 'high'
}
}
})