The read sequence
Inspect the package before selecting rows.
A package can hold more than one feature table. Select the table and output shape explicitly; the loader keeps geometry columns and spatial metadata attached to the result.
Open
Load the SQLite-backed package
Discover
List tables and geometry metadata
Select
Choose one table and output shape
Return
GeoJSON, table, or feature data
The GeoPackageLoader parses GeoPackage vector tables into loaders.gl geometry tables.
Detailed guide
GeoPackage loader usage
The sections below cover package selection, output shapes, SQL.js setup, and the current loader constraints.
The GeoPackageLoader requires applications to install the optional peer dependency sql.js (npm install sql.js). It uses the sql.js npm module which has caused issues with certain JavaScript bundlers. It is recommended that you do your own tests before using the GeoPackageLoader in your project.
| Property | Value |
|---|---|
| Data source | GeoPackageDataSource |
Installation
Install sql.js to use this API. It is an optional peer of @loaders.gl/geospatial.
npm install @loaders.gl/core @loaders.gl/geospatial sql.js@^1.14.1
For grouped imports, install @loaders.gl/geospatial in place of @loaders.gl/geospatial/geopackage and keep sql.js installed.
Usage
The loader returns one selected vector table. With no table name supplied it uses the single table, the metadata-marked default, or the first vector table as a fallback:
import {GeoPackageLoader} from '@loaders.gl/geospatial/geopackage';
import {load} from '@loaders.gl/core';
import {isArrowTable} from '@loaders.gl/core/schema-utils';
const table = await load(url, GeoPackageLoader);
if (!isArrowTable(table)) throw new Error('Expected an Arrow table');
To load a specific table named feature_table in a GeoPackage file as GeoJSON:
import {GeoPackageLoader, GeoPackageLoaderOptions} from '@loaders.gl/geospatial/geopackage';
import {load} from '@loaders.gl/core';
import type {GeoJSONTable} from '@loaders.gl/core/schema';
const optionsAsGeoJson: GeoPackageLoaderOptions = {
geopackage: {
shape: 'geojson-table',
table: 'feature_table',
sqlJsCDN: 'https://cdn.jsdelivr.net/npm/sql.js@1.14.1/dist/'
}
};
const geoJsonData: GeoJSONTable = await load(url, GeoPackageLoader, optionsAsGeoJson);
The default output is an Arrow table. Use geopackage.shape: 'geojson-table' when a GeoJSON
feature table is required.
To inspect available tables first and then fetch a specific table, use GeoPackageSource.
Shapes
GeoPackageLoader returns one selected vector table. Set geopackage.shape to choose the
table representation and geopackage.table to select a table by name. To discover all tables
and query them through a source API, use GeoPackageSource.
| Shape | Output |
|---|---|
geojson-table | loaders.gl GeoJSONTable for one table |
arrow | Apache Arrow Table with WKB geometry |
Options
| Option | Type | Default | Description |
|---|---|---|---|
geopackage.shape | String | 'arrow' | Output shape: 'arrow' or 'geojson-table'. |
geopackage.table | String | metadata-selected table | Name of the vector table to load. |
geopackage.sqlJsCDN | String | 'https://cdn.jsdelivr.net/npm/sql.js@1.14.1/dist/' | CDN from which to load the SQL.js bundle. This is loaded asynchronously when the GeoPackage loader is called on a file. |
gis.reproject | boolean | false | Opt-in coordinate transformation; missing source CRS or unsupported transformations throw CRSReprojectionError. |
gis.targetCrs | CRSDefinition | 'WGS84' when reprojection is enabled | Output CRS for transformed coordinates. |
Output
The GeoPackageLoader loads one GeoPackage vector table.
-
If
geopackage.shapeis'geojson-table':Returns a
GeoJSONTablefor the selected table. -
If
geopackage.shapeis'arrow':Returns an Apache Arrow
Tablefor the selected table with a WKBgeometrycolumn.
Use GeoPackageSource when the
application needs table discovery, metadata for multiple layers, or repeated table queries.
Remarks
options.geopackage.sqlJsCDN: As of March 2022, SQL.js versions 1.6.0, 1.6.1, and 1.6.2 were tested as not working.
Notes
- GeoPackage does not define a standard preferred-table field. When a single table is not explicitly requested, loaders.gl uses best-effort metadata heuristics and otherwise falls back to the first vector table in
gpkg_contents.