Skip to content

Commit 6b1971a

Browse files
authored
feat: stac_wasm.stacJsonToParquet (#786)
1 parent ecb49ed commit 6b1971a

File tree

5 files changed

+99
-5
lines changed

5 files changed

+99
-5
lines changed

crates/wasm/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[package]
22
name = "stac-wasm"
3-
version = "0.0.2"
3+
version = "0.0.3"
4+
readme = "README.md"
45
description = "Converts Arrow arrays to STAC items, via WASM"
56
authors.workspace = true
67
edition.workspace = true
@@ -20,7 +21,7 @@ arrow-schema.workspace = true
2021
arrow-wasm = { git = "https://github.com/kylebarron/arrow-wasm", rev = "012447cab019efbb051cbd43ea5ad55a7549a4d8" }
2122
serde.workspace = true
2223
serde-wasm-bindgen = "0.6.5"
23-
stac = { workspace = true, features = ["geoarrow"] }
24+
stac = { workspace = true, features = ["geoparquet"] }
2425
thiserror.workspace = true
2526
wasm-bindgen = "0.2.84"
2627

crates/wasm/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ Converts [Arrow](https://arrow.apache.org/) arrays to [SpatioTemporal Asset Cata
1111
npm i stac-wasm
1212
```
1313

14-
We give you one function:
14+
We give you two functions:
1515

1616
```javascript
1717
import * as stac_wasm from "stac-wasm";
1818

1919
const table = loadArrowTable(); // e.g. from DuckDB
2020
const items = stac_wasm.arrowToStacJson(table);
21+
const bytes = stac_wasm.stacJsonToParquet(items);
2122
```
2223

2324
## Tests
@@ -36,3 +37,13 @@ This should open a page at <http://localhost:8080/> that you can use to test out
3637

3738
**stac-wasm** is part of [rustac](https://github.com/stac-utils/rustac), a monorepo that includes the Rust code used to build the WASM module.
3839
See [CONTRIBUTING.md](https://github.com/stac-utils/rustac/blob/main/CONTRIBUTING.md) for instructions on contributing to the monorepo.
40+
If your on MacOS, you might have to use **llvm** as described [in this comment](https://github.com/briansmith/ring/issues/1824#issuecomment-2059955073).
41+
42+
## Releasing
43+
44+
```shell
45+
wasm-pack build
46+
wasm-pack login
47+
cd pkg
48+
npm publish
49+
```

crates/wasm/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use arrow_schema::ArrowError;
33
use arrow_wasm::{Table, arrow_js::table::JSTable, error::WasmResult};
44
use serde::Serialize;
55
use serde_wasm_bindgen::Serializer;
6+
use stac::Item;
7+
use std::io::Cursor;
68
use thiserror::Error;
79
use wasm_bindgen::prelude::*;
810

@@ -24,3 +26,11 @@ pub fn arrow_to_stac_json(table: JSTable) -> WasmResult<JsValue> {
2426
let items = items.serialize(&serializer)?;
2527
Ok(items)
2628
}
29+
30+
#[wasm_bindgen(js_name = stacJsonToParquet)]
31+
pub fn stac_json_to_parquet(value: JsValue) -> Result<Vec<u8>, JsError> {
32+
let items: Vec<Item> = serde_wasm_bindgen::from_value(value)?;
33+
let mut cursor = Cursor::new(Vec::new());
34+
stac::geoparquet::into_writer(&mut cursor, items)?;
35+
Ok(cursor.into_inner())
36+
}

crates/wasm/www/index.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,75 @@ URL.revokeObjectURL(worker_url);
2222
const connection = await db.connect();
2323
const table = await connection.query("select 'an-id' as id");
2424
console.log(stac_wasm.arrowToStacJson(table));
25+
26+
const bytes = stac_wasm.stacJsonToParquet([
27+
{
28+
stac_version: "1.1.0",
29+
stac_extensions: [],
30+
type: "Feature",
31+
id: "20201211_223832_CS2",
32+
bbox: [
33+
172.91173669923782, 1.3438851951615003, 172.95469614953714,
34+
1.3690476620161975,
35+
],
36+
geometry: {
37+
type: "Polygon",
38+
coordinates: [
39+
[
40+
[172.91173669923782, 1.3438851951615003],
41+
[172.95469614953714, 1.3438851951615003],
42+
[172.95469614953714, 1.3690476620161975],
43+
[172.91173669923782, 1.3690476620161975],
44+
[172.91173669923782, 1.3438851951615003],
45+
],
46+
],
47+
},
48+
properties: {
49+
datetime: "2020-12-11T22:38:32.125000Z",
50+
},
51+
collection: "simple-collection",
52+
links: [
53+
{
54+
rel: "collection",
55+
href: "./collection.json",
56+
type: "application/json",
57+
title: "Simple Example Collection",
58+
},
59+
{
60+
rel: "root",
61+
href: "./collection.json",
62+
type: "application/json",
63+
title: "Simple Example Collection",
64+
},
65+
{
66+
rel: "parent",
67+
href: "./collection.json",
68+
type: "application/json",
69+
title: "Simple Example Collection",
70+
},
71+
],
72+
assets: {
73+
visual: {
74+
href: "https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2.tif",
75+
type: "image/tiff; application=geotiff; profile=cloud-optimized",
76+
title: "3-Band Visual",
77+
roles: ["visual"],
78+
},
79+
thumbnail: {
80+
href: "https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2.jpg",
81+
title: "Thumbnail",
82+
type: "image/jpeg",
83+
roles: ["thumbnail"],
84+
},
85+
},
86+
},
87+
]);
88+
console.log(bytes);
89+
const url = URL.createObjectURL(
90+
new Blob([bytes], { type: "application/vnd.apache.parquet" })
91+
);
92+
const a = document.createElement("a");
93+
a.href = url;
94+
a.download = "items.parquet";
95+
a.textContent = "download";
96+
document.body.appendChild(a);

crates/wasm/www/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "stac-wasm",
3-
"version": "0.1.0",
2+
"name": "stac-wasm-test",
3+
"version": "0.0.0",
44
"description": "STAC WASM test",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)