geoarrow_wasm/array/
macro.rs

1// TODO: better to not export at the top level?
2// https://stackoverflow.com/a/31749071
3#[macro_export]
4macro_rules! impl_geometry_array {
5    ($struct_name:ident) => {
6        #[wasm_bindgen]
7        impl $struct_name {
8            #[wasm_bindgen]
9            pub fn affine_transform(
10                &self,
11                transform: BroadcastableAffine,
12            ) -> WasmResult<GeometryArray> {
13                use geoarrow::algorithm::geo::affine_transform;
14                Ok(GeometryArray(affine_transform(&self.into(), transform.0)?))
15            }
16
17            #[wasm_bindgen]
18            pub fn area(&self) -> WasmResult<FloatArray> {
19                use geoarrow::algorithm::geo::area;
20                Ok(FloatArray(area(self.into())?))
21            }
22
23            #[wasm_bindgen]
24            pub fn center(&self) -> WasmResult<PointArray> {
25                use geoarrow::algorithm::geo::center;
26                Ok(PointArray(center(&self.into())?))
27            }
28
29            #[wasm_bindgen]
30            pub fn centroid(&self) -> WasmResult<PointArray> {
31                use geoarrow::algorithm::geo::centroid;
32                Ok(PointArray(centroid(&self.into())?))
33            }
34
35            #[wasm_bindgen]
36            pub fn convex_hull(&self) -> WasmResult<PolygonArray> {
37                use geoarrow::algorithm::geo::convex_hull;
38                Ok(PolygonArray(convex_hull(&self.into())?))
39            }
40
41            #[wasm_bindgen]
42            pub fn euclidean_length(&self) -> WasmResult<FloatArray> {
43                use geoarrow::algorithm::geo::euclidean_length;
44                Ok(FloatArray(euclidean_length(&self.into())?))
45            }
46
47            #[wasm_bindgen]
48            pub fn geodesic_area(&self) -> WasmResult<FloatArray> {
49                use geoarrow::algorithm::geo::geodesic_area_unsigned;
50                Ok(FloatArray(geodesic_area_unsigned(&self.into())?))
51            }
52
53            #[wasm_bindgen]
54            pub fn geodesic_area_signed(&self) -> WasmResult<FloatArray> {
55                use geoarrow::algorithm::geo::geodesic_area_signed;
56                Ok(FloatArray(geodesic_area_signed(&self.into())?))
57            }
58
59            #[wasm_bindgen]
60            pub fn geodesic_length(&self) -> WasmResult<FloatArray> {
61                use geoarrow::algorithm::geo::geodesic_length;
62                Ok(FloatArray(geodesic_length(&self.into())?))
63            }
64
65            #[wasm_bindgen]
66            pub fn haversine_length(&self) -> WasmResult<FloatArray> {
67                use geoarrow::algorithm::geo::haversine_length;
68                Ok(FloatArray(haversine_length(&self.into())?))
69            }
70
71            #[wasm_bindgen]
72            pub fn is_empty(&self) -> WasmResult<BooleanArray> {
73                use geoarrow::algorithm::geo::is_empty;
74                Ok(BooleanArray(is_empty(&self.into())?))
75            }
76
77            #[cfg(feature = "geodesy")]
78            #[wasm_bindgen]
79            pub fn reproject_rs(
80                &self,
81                definition: &str,
82                direction: ReprojectDirection,
83            ) -> WasmResult<GeometryArray> {
84                use geoarrow::algorithm::geodesy::reproject;
85                Ok(GeometryArray(reproject(
86                    &self.into(),
87                    definition,
88                    direction.into(),
89                )?))
90            }
91
92            #[wasm_bindgen]
93            pub fn rotate(
94                &self,
95                angle: BroadcastableFloat,
96                origin: TransformOrigin,
97            ) -> WasmResult<GeometryArray> {
98                use geoarrow::algorithm::geo::rotate;
99                Ok(GeometryArray(rotate(&self.into(), angle.0, origin.0)?))
100            }
101
102            #[wasm_bindgen]
103            pub fn scale(
104                &self,
105                xfact: BroadcastableFloat,
106                yfact: BroadcastableFloat,
107                origin: TransformOrigin,
108            ) -> WasmResult<GeometryArray> {
109                use geoarrow::algorithm::geo::scale;
110                Ok(GeometryArray(scale(
111                    &self.into(),
112                    xfact.0,
113                    yfact.0,
114                    origin.0,
115                )?))
116            }
117
118            #[wasm_bindgen]
119            pub fn signed_area(&self) -> WasmResult<FloatArray> {
120                use geoarrow::algorithm::geo::signed_area;
121                Ok(FloatArray(signed_area(self.into())?))
122            }
123
124            #[wasm_bindgen]
125            pub fn skew(
126                &self,
127                x_degrees: BroadcastableFloat,
128                y_degrees: BroadcastableFloat,
129                origin: TransformOrigin,
130            ) -> WasmResult<GeometryArray> {
131                use geoarrow::algorithm::geo::skew;
132                Ok(GeometryArray(skew(
133                    &self.into(),
134                    x_degrees.0,
135                    y_degrees.0,
136                    origin.0,
137                )?))
138            }
139
140            #[wasm_bindgen]
141            pub fn to_ffi(&self) -> FFIArrowArray {
142                let arrow_array = self.0.clone().into_boxed_arrow();
143                let field = Field::new("", arrow_array.data_type().clone(), true);
144                FFIArrowArray::new(&field, arrow_array)
145            }
146
147            #[wasm_bindgen]
148            pub fn translate(
149                &self,
150                x_offset: BroadcastableFloat,
151                y_offset: BroadcastableFloat,
152            ) -> WasmResult<GeometryArray> {
153                use geoarrow::algorithm::geo::translate;
154                log!("{:?}", &self.0);
155                Ok(GeometryArray(translate(
156                    &self.into(),
157                    x_offset.0,
158                    y_offset.0,
159                )?))
160            }
161
162            #[wasm_bindgen]
163            pub fn vincenty_length(&self) -> WasmResult<FloatArray> {
164                use geoarrow::algorithm::geo::vincenty_length;
165                Ok(FloatArray(vincenty_length(&self.into())?))
166            }
167        }
168    };
169}