Loam
Javascript wrapper for GDAL in the browser
Install / Use
/learn @azavea/LoamREADME
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:
loam-worker.js: This is the "backend" of the library; it initializes the Web Worker and translates between the Loam "frontend" and GDAL.gdal.js: This initializes the Emscripten runtime and loads the GDAL WebAssembly.gdal.wasm: The GDAL binary, compiled to WebAssembly.gdal.data: Contains configuration files that GDAL expects to find on the host filesystem.
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
pathPrefix(optional): The path or URL that Loam should use as a prefix when fetching its Web Worker. If left undefined, Loam will make a best guess based on the source path of its own<script>element. If Loam has no<script>element (e.g. because you are using dynamic imports), then autodetecting a prefix will fail and this parameter must be provided. URLs with domains may be used to enable Loam to be loaded from CDNs like unpkg, but the file name should be left off.gdalPrefix(optional): The path or URL that Loam should use as a prefix when fetching WebAssembly assets for GDAL. If left undefined, Loam will use the same value aspathPrefix. URLs with domains may be used to enable loading from CDNs like unpkg, but the file name should be left off. If Loam fails to work properly and you see requests resulting in 404s or other errors for thegdal.*assets listed above, you will need to setpathPrefix, or this parameter, or both, to the correct locations where Loam can find those assets.
Return value
A promise that resolves when Loam is initialized. All of the functions described in this document wait for this promise's resolution when executing, so paying attention to whether this promise has resolved or not is not required. However, it may be helpful to do so in some circumstances, for example, if you want to display a visual indicator that your app is ready.
<br />loam.open(file, sidecars)
Creates a new GDAL Dataset.
Parameters
file: A Blob or File object that should be opened with GDAL. GDAL is compiled with TIFF, PNG, and JPEG support. If you have a Blob, you may also control the name of the file that is shown to GDAL on the virtual filesystem by passing an object with the shape{name: string, data: Blob}. This can be useful if you are relying on GDAL behavior that uses file extensions to determine formats.sidecars: An array of additional files that will be made present in the virtual file system when openingfile. Some data formats are composed of multiple files (for example, Shapefiles have.shp,.shx, and.prjfiles, among others). If you need to include multiple files in order to open a dataset, pass the "main" file asfile, and pass the others tosidecars. For a Shapefile, this would mean passing the.shpfile asfileand the.shx,.prj, and friends tosidecars. Iffileis a File, thensidecarsmust be an Array<File>. Iffileis a Blob or Object (see above), thensidecarsmust be an Array<Object> where each element has the shape{name: string, data: Blob}.
Return value
A promise that resolves with an instance of GDALDataset.
loam.rasterize(geojson, args)
Burns vectors in GeoJSON format into rasters. This is the equivalent of the gdal_rasterize command.
Note: This returns a new GDALDataset object but does not perform any immediate calculation. Instead, calls to .rasterize() are evaluated lazily (as with convert() and warp(), below). The rasterization operation is only evaluated when necessary in order to access some property of the dataset, such as its size, bytes, or band count. Successive calls to .warp() and .convert() can be lazily chained onto datasets produced via loam.rasterize().
Parameters
geojson: A Javascript object (not a string) in GeoJSON format.args: An array of strings, each representing a single command-line argument accepted by thegdal_rasterizecommand. Thesrc_datasourceanddst_filenameparameters should be omitted; these are handled internally by Loam. Example (assuming you have a properly structured GeoJSON object):loam.rasterize(geojson, ['-burn', '1.0', '-of', 'GTiff', '-ts', '200', '200'])
Return value
A promise that resolves to a new GDALDataset.
loam.reproject(fromCRS, toCRS, coords)
Reproject coordinates from one coordinate system to another using PROJ.4.
Parameters
fromCRS: A WKT-formatted string representing the source CRS.toCRS: A WKT-formatted string representing the destination CRS.coords: An array of [x, y] coordinate pairs.
Return value
A promise that resolves with an array of transformed coordinate pairs.
<br />loam.reset()
Tear down Loam's internal Web Worker. This will cause initialize() to create a new Web Worker the next time it is called.
Note: This exists primarily to enable certain types of unit testing. It should not be necessary to call this function during normal usage of Loam. If you find that you are encountering a problem that loam.reset() solves, please open an issue
Parameters
- None
Return value
A promise that resolves when the Web Worker has been terminated. This function waits for initialize() to complete or fail before tearing down the worker.
<br />GDALDataset.close()
This used to be required in order to avoid memory leaks in earlier versions of Loam, but is currently a no-op. It has been maintained to preserve backwards compatibility, but has no effect other than to display a console warning.
Return value
A promise that resolves immediately with an empty list (for historical reasons).
<br />GDALDataset.count()
Get the number of raster bands in the dataset.
Return value
A promise which resolves to the number of raster bands in the dataset.
<br />GDALDataset.layerCount()
Get the number of vector layers in the dataset.
Return value
A promise which resolves to the number of vector layers in the dataset.
<br />GDALDataset.width()
Get the width of the dataset, in pixels.
Return value
A promise which resolves to the width of the dataset, in pixels.
<br />GDALDataset.height()
Get the height of the dataset, in pixels.
Return value
A promise which resolves to the height
Related Skills
node-connect
344.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
96.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
344.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
344.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
