Skip to main content
1

Install the package

npm install @ngageoint/geopackage
2

Import and configure the canvas adapter

In Node.js, the library uses CanvasKit (WebAssembly) for tile rendering. You must tell it where the .wasm file lives before opening any GeoPackage.
const {
  setCanvasKitWasmLocateFile,
  GeoPackageManager,
  GeoPackageTileRetriever,
  Canvas,
  TileUtils,
} = require('@ngageoint/geopackage');

// Point to the canvaskit WASM file bundled with the package.
setCanvasKitWasmLocateFile(
  file => `${__dirname}/node_modules/@ngageoint/geopackage/dist/canvaskit/` + file
);
If you bundle your app with webpack or esbuild, copy the dist/canvaskit/ directory to your output folder and adjust the path accordingly.
3

Open a GeoPackage file

Pass the path to a .gpkg file. GeoPackageManager.open returns a Promise.
GeoPackageManager.open('/path/to/file.gpkg').then(async (geoPackage) => {
  // work with geoPackage here
});
Or using async/await:
const geoPackage = await GeoPackageManager.open('/path/to/file.gpkg');
4

List tables

const tileTables    = geoPackage.getTileTables();
const featureTables = geoPackage.getFeatureTables();

console.log('Tile tables:',    tileTables);
console.log('Feature tables:', featureTables);
5

Query features as GeoJSON

Iterate over every feature in a table as a GeoJSON Feature object. Always call close() on the result set when you are done.
const table = featureTables[0];
const geoJSONResultSet = geoPackage.queryForGeoJSONFeatures(table);

for (const feature of geoJSONResultSet) {
  console.log(feature); // GeoJSON Feature
}
geoJSONResultSet.close();
6

Get a tile

Retrieve a web-mercator tile at a given x/y/zoom coordinate and convert it to a base-64 PNG string.
const tileDao = geoPackage.getTileDao(tileTables[0]);
const gpr     = new GeoPackageTileRetriever(tileDao);

const x = 0, y = 0, zoom = 0;
const geoPackageTile = await gpr.getTile(x, y, zoom);

// Decode the tile into an image and draw it onto a canvas.
const canvas        = Canvas.create(TileUtils.TILE_PIXELS_DEFAULT, TileUtils.TILE_PIXELS_DEFAULT);
const context       = canvas.getContext('2d');
const geoPackageImage = await geoPackageTile.getGeoPackageImage();

context.drawImage(geoPackageImage.getImage(), 0, 0);
const base64String = canvas.toDataURL('image/png');

// Free CanvasKit memory — required in Node.js.
Canvas.disposeImage(geoPackageImage);
Canvas.disposeCanvas(canvas);
7

Close the GeoPackage

geoPackage.close();
In Node.js you must call Canvas.disposeImage(image) and Canvas.disposeCanvas(canvas) after you are finished with each image or canvas object. Failing to do so will leak CanvasKit WebAssembly memory.

Memory management (Node.js)

CanvasKit allocates off-heap WebAssembly memory for every image and canvas it creates. The JavaScript garbage collector cannot free this memory automatically, so you must dispose of these objects manually.
// After drawing is complete, release CanvasKit memory.
Canvas.disposeImage(geoPackageImage);  // free a GeoPackageImage
Canvas.disposeCanvas(canvas);          // free a canvas created with Canvas.create()
Forget these calls inside a loop and your Node.js process will grow without bound.

Next steps

Installation

Full setup instructions for Node.js, browser, and Web Workers.

Feature queries

Spatial bounding-box queries, pagination, and attribute filtering.

Tile rendering

Render map tiles, draw feature tiles, and export images.

Web Worker

Run GeoPackage operations off the main thread.

Build docs developers (and LLMs) love