-
Notifications
You must be signed in to change notification settings - Fork 9
feat(python): add geolibre-wasm Python wrapper #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
27ce5a5
feat(python): add geolibre-wasm Python wrapper
giswqs 38fdc83
ci: test the Python wrapper and publish it to PyPI on release
giswqs 2ce99c9
docs: add Python install instructions and PyPI badge
giswqs 12175b2
Address CodeRabbit review feedback
giswqs 876d0f6
docs: clarify the /work sandbox in the Python examples
giswqs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # geolibre-wasm (Python) | ||
|
|
||
| Run the [`geolibre-rust`](https://github.com/opengeos/geolibre-rust) geospatial | ||
| tool suite (the `whitebox_next_gen` tools plus GeoLibre's own) from Python. The | ||
| tools are a single WebAssembly (WASI) module executed in-process via | ||
| [`wasmtime`](https://github.com/bytecodealliance/wasmtime-py), so there is **no | ||
| native install, no GDAL, and no server** — just `pip install`. | ||
|
|
||
| This mirrors the JavaScript `geolibre-wasm/tools` API (`list_tools`, | ||
| `list_manifests`, `run_tool`), so the two stay in sync. | ||
|
|
||
| > The import package is `geolibre_wasm` (the distribution is `geolibre-wasm`), | ||
| > matching the npm package name and avoiding a clash with the separate | ||
| > `geolibre` application package. | ||
|
|
||
| ## Install | ||
|
|
||
| ```bash | ||
| pip install geolibre-wasm | ||
| ``` | ||
|
|
||
| On first use the runtime (`geolibre-cli.wasm`, ~20 MB) is downloaded from the | ||
| matching GitHub release and cached under `~/.cache/geolibre/`. To use a local | ||
| copy instead, set `GEOLIBRE_WASM=/path/to/geolibre-cli.wasm` or pass | ||
| `wasm_path=` to any call. | ||
|
|
||
| ## Usage | ||
|
|
||
| Inputs are passed as `bytes` under `/work`; the tool reads/writes there and any | ||
| new files come back as `bytes`. | ||
|
|
||
| ```python | ||
| import geolibre_wasm as gl | ||
|
|
||
| # Discover tools (each manifest carries a "source": "geolibre" | "whitebox") | ||
| tools = gl.list_tools() | ||
| manifests = gl.list_manifests() | ||
|
|
||
| # Raster: compute slope from a DEM | ||
| dem = open("dem.tif", "rb").read() | ||
| res = gl.run_tool( | ||
| "slope", | ||
| args=["--input=/work/dem.tif", "--output=/work/slope.tif", "--units=degrees"], | ||
| input={"dem.tif": dem}, | ||
| ) | ||
| assert res.exit_code == 0 | ||
| open("slope.tif", "wb").write(res.files["slope.tif"]) | ||
|
|
||
| # Reproject (warp) to a target EPSG | ||
| res = gl.run_tool( | ||
| "reproject_raster", | ||
| args=["--input=/work/dem.tif", "--output=/work/wgs84.tif", "--epsg=4326"], | ||
| input={"dem.tif": dem}, | ||
| ) | ||
|
|
||
| # Vector: GeoJSON -> GeoParquet (Hilbert-sorted, bbox covering, ZSTD by default) | ||
| gj = open("cities.geojson", "rb").read() | ||
| res = gl.run_tool( | ||
| "write_geoparquet", | ||
| args=["--input=/work/in.geojson", "--output=/work/out.parquet"], | ||
| input={"in.geojson": gj}, | ||
| ) | ||
| open("cities.parquet", "wb").write(res.files["out.parquet"]) | ||
| ``` | ||
|
|
||
| Tools that write a directory tree (e.g. `raster_to_tiles`) return nested keys: | ||
|
|
||
| ```python | ||
| res = gl.run_tool( | ||
| "raster_to_tiles", | ||
| args=["--input=/work/dem.tif", "--output_dir=/work/tiles", "--min_zoom=16", "--max_zoom=18"], | ||
| input={"dem.tif": dem}, | ||
| ) | ||
| for path, data in res.files.items(): | ||
| # e.g. "tiles/16/9559/32767.png" | ||
| ... | ||
| ``` | ||
|
|
||
| ## API | ||
|
|
||
| - `list_tools(wasm_path=None) -> list[str]` | ||
| - `list_manifests(wasm_path=None) -> list[dict]` | ||
| - `run_tool(tool, args=None, input=None, wasm_path=None) -> ToolResult` | ||
| - `ToolResult(exit_code: int, stdout: list[str], files: dict[str, bytes])` | ||
| - `runtime_path(wasm_path=None) -> str` — resolve the runtime (explicit > `GEOLIBRE_WASM` > cached download) | ||
| - `download_runtime(dest=None) -> str` — fetch the runtime ahead of time | ||
|
|
||
| ## License | ||
|
|
||
| MIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| [build-system] | ||
| requires = ["hatchling"] | ||
| build-backend = "hatchling.build" | ||
|
|
||
| [project] | ||
| name = "geolibre-wasm" | ||
| version = "0.4.0" | ||
| description = "Run the GeoLibre / whitebox_next_gen geospatial tool suite (WASM/WASI) from Python." | ||
| readme = "README.md" | ||
| requires-python = ">=3.9" | ||
| license = "MIT" | ||
| authors = [{ name = "Qiusheng Wu", email = "giswqs@gmail.com" }] | ||
| keywords = ["geospatial", "gis", "wasm", "wasi", "raster", "vector", "geoparquet", "whitebox", "geolibre"] | ||
| classifiers = [ | ||
| "Programming Language :: Python :: 3", | ||
| "License :: OSI Approved :: MIT License", | ||
| "Operating System :: OS Independent", | ||
| "Topic :: Scientific/Engineering :: GIS", | ||
| ] | ||
| dependencies = ["wasmtime>=20,<50"] | ||
|
|
||
| [project.urls] | ||
| Homepage = "https://github.com/opengeos/geolibre-rust" | ||
| Repository = "https://github.com/opengeos/geolibre-rust" | ||
| Issues = "https://github.com/opengeos/geolibre-rust/issues" | ||
|
|
||
| [tool.hatch.build.targets.wheel] | ||
| packages = ["src/geolibre_wasm"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """GeoLibre: run the whitebox_next_gen + GeoLibre geospatial tool suite from Python. | ||
|
|
||
| The tools are compiled to a single WebAssembly (WASI) module and executed | ||
| in-process via wasmtime, so there is no native install, GDAL, or server. The API | ||
| mirrors the JavaScript ``geolibre-wasm/tools`` package. | ||
|
|
||
| Example: | ||
| >>> import geolibre_wasm as gl | ||
| >>> dem = open("dem.tif", "rb").read() | ||
| >>> result = gl.run_tool( | ||
| ... "slope", | ||
| ... args=["--input=/work/dem.tif", "--output=/work/slope.tif", "--units=degrees"], | ||
| ... input={"dem.tif": dem}, | ||
| ... ) | ||
| >>> result.exit_code | ||
| 0 | ||
| >>> open("slope.tif", "wb").write(result.files["slope.tif"]) | ||
| """ | ||
|
|
||
| from ._core import ( | ||
| RUNTIME_VERSION, | ||
| ToolResult, | ||
| download_runtime, | ||
| list_manifests, | ||
| list_tools, | ||
| run_tool, | ||
| runtime_path, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "RUNTIME_VERSION", | ||
| "ToolResult", | ||
| "download_runtime", | ||
| "list_manifests", | ||
| "list_tools", | ||
| "run_tool", | ||
| "runtime_path", | ||
| ] | ||
|
|
||
| __version__ = "0.4.0" | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.