Core API
Using loaders
Keep application code focused on the result: choose a loader explicitly, or let loaders.gl select one from the formats your app supports.
The loader boundary
Formats change. The application path can stay the same.
Choose a loader explicitly when the format is known, or let the selector choose from a compatible set. The result remains the shape your application is built to consume.
Loader selection
explicit · list · registry
One loader can be precise; a loader list can be flexible; the result contract is still yours.
The loader path
Choose how much the application wants to know.
Use one known loader when the format is part of the application contract, a list when the input may vary, or the shared registry when discovery belongs to the runtime.
Explicit
Pass one loader and retain precise result types
Multiple
Offer compatible loaders for automatic selection
Registered
Add application-wide defaults with registerLoaders()
Result
Use load(), parse(), or loadInBatches() for the chosen shape
loaders.gl has parser functions that use so-called loaders to convert raw file data into parsed objects. Each loader combines a parser for one format (or related group of formats) with metadata such as its name, supported extensions, and input type.
Detailed guide
Selection, options, and runtime behavior
The guide below covers installation, explicit and automatic selection, TypeScript inference, options, composite loaders, and error handling.
Installing loaders
loaders.gl provides a suite of pre-built loader objects packaged as scoped npm modules. The intention is that applications will install and import loaders only for the formats they need.
Using Loaders
Loaders are passed into utility functions in the loaders.gl core API to enable parsing of the chosen format.
import {load} from '@loaders.gl/core';
import {CSVLoader} from '@loaders.gl/tables/csv';
const data = await load(url, CSVLoader);
// Application code here
...
Specifying Loaders
As shown above, loaders can be specified directly in a call to load or any of the parse functions:
import {load} from '@loaders.gl/core';
import {PCDLoader} from '@loaders.gl/pointclouds/pcd';
import {LASLoader} from '@loaders.gl/pointclouds/las';
const pointCloud = await load(url, [PCDLoader, LASLoader]);
// Application code here
...
Loaders and TypeScript
Since v4.0, all loaders are typed, meaning that loaders.gl can infer types for returned data and loader options from the supplied loader.
Note that type inference only works when single loader is provided:
import {load} from '@loaders.gl/core';
import {PCDLoader} from '@loaders.gl/pointclouds/pcd';
import {LASLoader} from '@loaders.gl/pointclouds/las';
// Single loader infers type
const pcdPointCloud = await load(url, PCDLoader); // => type MeshArrowTable
const lasPointCloud = await load(url, LASLoader); // => type LASMesh
const pointCloud = await load(url, [PCDLoader, LASLoader]); // => type unknown
You can use selectLoader and a switch statement when the input format is unknown but the result should remain typed:
import {load, selectLoader} from '@loaders.gl/core';
import {PCDLoader} from '@loaders.gl/pointclouds/pcd';
import {LASLoader} from '@loaders.gl/pointclouds/las';
const loader = await selectLoader(url, [PCDLoader, LASLoader]);
switch (loader.id) {
case 'pcd': {
const pcdPointCloud = await load(url, PCDLoader); // => type MeshArrowTable
break;
}
case 'las': {
const lasPointCloud = await load(url, LASLoader); // => type LASMesh
break;
}
}
Registering Loaders
Loaders can also be registered globally. To register a loader, use registerLoaders().
Registered loaders will be included in loader selection if you call any form of
parse() or load() that does not specify a single loader.
import {registerLoaders, load} from '@loaders.gl/core';
import {CSVLoader} from '@loaders.gl/tables/csv';
registerLoaders([CSVLoader]);
const data = await load('url.csv'); // => CSVLoader selected from pre-registered loaders
Note that in this case the loader type is not known and the return type will be unknown.
Relying on global state (such as set by registerLoaders()) is not a
recommended application development practice.
It sometimes causes problems later, as it tends to create unexpected dependencies between distant parts of the code.
The mechanism is provided but the choice to use it is yours.
Selecting Loaders
The loader selection algorithm is exposed to applications via selectLoader:
import {selectLoader} from '@loaders.gl/core';
import {ArrowLoader} from '@loaders.gl/tables/arrow';
import {CSVLoader} from '@loaders.gl/tables/csv';
const loader = await selectLoader('filename.csv', [ArrowLoader, CSVLoader]); // => CSVLoader
Note: Selection works on urls and/or data
Loader Options
load, parse and other core functions accept loader options in the form of an options object.
parse(data, Loader, {...options});
Such loader options objects are organized into nested sub objects, with one sub-object per loader or loader category. This provides a structured way to pass options to multiple loaders.
load(url, {
json: {...},
csv: {...},
'3d-tiles': {...},
gltf: {...}
});
An advantage of this design is that since the core functions can select a loader from a list of multiple candidate loaders, or invoke sub-loaders, the nested options system allows separate specification of options to each loader in a single options object.
Loader options are merged with default options using a deep, two-level merge. Any object-valued key on the top level will be merged with the corresponding key value in the default options object.
Core options
Options that apply to every loader (for instance fetch, worker, nothrow, batching controls, etc.) now live under an explicit core sub-object:
load(url, Loader, {
core: {
fetch: customFetch,
worker: false,
nothrow: true,
shape: 'object-row-table'
},
gltf: {...}
});
Use options.core.shape when you want one shared default return shape across loaders that support shape selection. Loader-scoped options such as options.csv.shape or options.json.shape still take precedence for that loader.
Using Composite Loaders
loaders.gl enables the creation of composite loaders that call other loaders (referred to as "sub-loaders" in this section). This enables loaders for "composite formats" to be quickly composed out of loaders for the primitive parts.
Composite Loader usage is designed to be conceptually simple for applications (loaders.gl handles a number of subtleties under the hood).
A composite loader is called just like any other loader, however there are some additional
Parameter Passing between Loaders
Loaders and parameters are passed through to sub loaders and are merged so that applications can override them:
parse(data, [Tiles3DLoader, GLTFLoader, DracoLoader], {
'3d-tiles': {
...
},
gltf: {
...
}
});
In this example:
- the passed in loaders would override any loaders specified inside the sub-loaders as well as any globally registered loaders.
- The options will be passed through to the sub-loaders, so that the
GLTFLoaderwill receive thegltfoptions, merged with anygltfoptions set by theTiles3DLoader.
This override system makes it easy for applications to test alternate sub-loaders or parameter options without having to modify any existing loader code.
Named subloaders
A loader or source can declare the metadata loaders it delegates to using export names:
subloaders: {GLTFLoader, GLBLoader}
Core preload(loader, options) recursively prepares the declared dependencies concurrently.
It returns a new loader with parser-bearing subloaders; shared metadata is not mutated.
Parsers consume prepared dependencies from their own option namespace, for example
options['3d-tiles'].subloaders.GLTFLoader. A prepared source receives the same bindings in
its createDataSource() options. Calling a metadata object's own preload() hook directly
only loads that object's implementation; use the core API to prepare the dependency graph.
import {preload, parse} from '@loaders.gl/core';
import {Tiles3DLoader} from '@loaders.gl/tiles-3d/3d-tiles';
const loader = await preload(Tiles3DLoader, {
core: {subloaders: {GLTFLoader: CustomGLTFLoader}},
gltf: {loadImages: true}
});
const tile = await parse(tileBytes, loader);
Overrides replace declared loaders by name and must provide compatible parser methods and
results. The flat options.core.subloaders map applies throughout the graph: every
occurrence of a declared name uses the same replacement. For example, a nested Draco override
belongs in options.core.subloaders.DracoLoader, while Draco settings stay in
options.draco. The Draco example requires the glTF loader to declare that named dependency.
An overridden loader supplies its own declared dependencies. Dependency cycles reject preloading.
Unused override names are allowed, so applications can share a map across pipelines and retain
entries for dependencies discovered lazily. Prepared parser calls forward the same caller map
without replacing it with resolved parser bindings; those bindings remain local to each loader.
Preload options are retained as defaults by the prepared graph; parse-time options override ordinary settings. Dependency bindings and the preload override map are fixed at preparation time: preload again to change overrides. Branch-specific loader replacements are not supported. Two branches using the same loader ID share its ordinary option namespace. Default preparations share in-flight imports, and failures can be retried. Caller-specific preparations keep their bindings separate.
Prepared dependency graphs run locally because their function-valued bindings cannot be
transferred to a worker. core.worker is forced to false in nested calls as well,
even when parse-time options request workers. Use the original metadata loader for normal
worker dispatch.
createDataSourceSync() requires a loaded implementation. Preload a source first when its dependencies must
be available before source creation.