Skip to main content

GeoPackage loader

GeoPackageLoader

GeoPackageLoader opens the SQLite container, discovers its feature tables, and returns the selected table in a loaders.gl geometry shape.

GeoPackage loader example

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

From-v3.0

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.

caution

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.

PropertyValue
Data sourceGeoPackageDataSource

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.

ShapeOutput
geojson-tableloaders.gl GeoJSONTable for one table
arrowApache Arrow Table with WKB geometry

Options​

OptionTypeDefaultDescription
geopackage.shapeString'arrow'Output shape: 'arrow' or 'geojson-table'.
geopackage.tableStringmetadata-selected tableName of the vector table to load.
geopackage.sqlJsCDNString'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.reprojectbooleanfalseOpt-in coordinate transformation; missing source CRS or unsupported transformations throw CRSReprojectionError.
gis.targetCrsCRSDefinition'WGS84' when reprojection is enabledOutput CRS for transformed coordinates.

Output​

The GeoPackageLoader loads one GeoPackage vector table.

  • If geopackage.shape is 'geojson-table':

    Returns a GeoJSONTable for the selected table.

  • If geopackage.shape is 'arrow':

    Returns an Apache Arrow Table for the selected table with a WKB geometry column.

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.