azavea / loam

Javascript wrapper for GDAL in the browser
Apache License 2.0
219 stars 16 forks source link
bindings gdal javascript js wasm webassembly wrapper

A wrapper for running GDAL in the browser using gdal-js

Installation

npm install loam

Assuming you are using a build system, the main loam library should integrate into your build the same as any other library might. However, in order to correctly initialize the Emscripten environment for running GDAL, there are other assets that need to be accessible via HTTP request at runtime, but which should not be included in the main application bundle. Specifically, these are:

All of these files will be included in the node_modules folder after running npm install loam, but it is up to you to integrate them into your development environment and deployment processes. Unfortunately, support for WebAssembly and Web Workers is still relatively young, so many build tools do not yet have a straightforward out-of-the-box solution that will work. However, in general, treating the four files above similarly to static assets (e.g. images, videos, or PDFs) tends to work fairly well. An example for Create React App is given below.

Create React App

When integrating Loam with a React app that was initialized using Create React App, the simplest thing to do is probably to copy the assets above into the /public folder, like so:

cp node_modules/gdal-js/gdal.* node_modules/loam/lib/loam-worker.js public/

This will cause the CRA build system to copy these files into the build folder untouched, where they can then be accessed by URL (e.g. http://localhost:3000/gdal.wasm). However, this has the disadvantage that you will need to commit the copied files to source control, and they won't be updated if you update Loam. A way to work around this is to put symlinks in /public instead:

ln -s ../node_modules/loam/lib/loam-worker.js public/loam-worker.js
ln -s ../node_modules/gdal-js/gdal.wasm public/gdal.wasm
ln -s ../node_modules/gdal-js/gdal.data public/gdal.data
ln -s ../node_modules/gdal-js/gdal.js public/gdal.js

API Documentation

Basic usage

import loam from "loam";

// Load WebAssembly and data files asynchronously. Will be called automatically by loam.open()
// but it is often helpful for responsiveness to pre-initialize because these files are fairly large. Returns a promise.
loam.initialize();

// Assuming you have a `Blob` object from somewhere. `File` objects also work.
loam.open(blob).then((dataset) => {
  dataset.width()
    .then((width) => /* do stuff with width */);

Functions

loam.initialize(pathPrefix, gdalPrefix)

Manually set up web worker and initialize Emscripten runtime. This function is called automatically by other functions on loam. Returns a promise that is resolved when Loam is fully initialized.

Although this function is called automatically by other functions, such as loam.open(), it is often beneficial for user experience to manually call loam.initialize(), because it allows pre-fetching Loam's WebAssembly assets (which are several megabytes uncompressed) at a time when the latency required to download them will be least perceptible by the user. For example, loam.initialize() could be called when the user clicks a button to open a file-selection dialog, allowing the WebAssembly to load in the background while the user selects a file.

This function is safe to call multiple times.

Parameters


loam.open(file, sidecars)

Creates a new GDAL Dataset.

Parameters