Awesome
geos-wasm
This is a WebAssembly build of GEOS using Emscripten. It can be used in the browser or in Node.js, bun, or deno.
GEOS is a C/C++ port of the Java Topology Suite, a library for performing operations on planar geometry. GEOS provides many of the algorithms used by PostGIS, the Shapely package for Python, the sf package for R, and others.
Installation
Browser
<script type="module">
import initGeosJs from './build/package/geos.esm.js';
const geos = await initGeosJs();
</script>
Node.js
import initGeosJs from './src/index.mjs';
const geos = await initGeosJs();
Example
import initGeosJs from '../../build/package/geos.esm.js'
// initGeosJs returns a promise that resolves to a GEOS object
const geos = await initGeosJs()
// use the GEOS object to call GEOS functions
// Example: get the area of a polygon
// create a WKT reader
const reader = geos.GEOSWKTReader_create()
const wkt = 'POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))'
// read the WKT string into a GEOS geometry -> returns a pointer
const geomPtr = geos.GEOSWKTReader_read(reader, wkt)
// create a pointer where the area will be written to
const areaPtr = geos.Module._malloc(8)
// calculate the area of the geometry
geos.GEOSArea(geomPtr, areaPtr)
// read the area from the pointer into a JS number
const area = geos.Module.getValue(areaPtr, 'double')
console.log(area) // area = 1
// free the WKT reader, the geometry, and the pointer
geos.GEOSWKTReader_destroy(reader)
geos.GEOSGeom_destroy(geomPtr)
geos.GEOSFree(areaPtr)
API
Please refer to the API documentation.
Development
Prerequisites
Compiling
The compiling process and overall project structure is largely copied from gdal3! Thanks for the great work!
- Clone the repository.
- Run
npm install
. - Run
npm run make
ormake
. Runmake type=debug
for a debug version.
Testing
npm test
Contribution
Contributions are welcome! Please open an issue in order to discuss contributions before submitting a pull request.