Skip to content

Commit

Permalink
Start moving js methods to per-folder algs (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Jul 12, 2023
1 parent f117113 commit abd1774
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 47 deletions.
31 changes: 31 additions & 0 deletions js/src/algorithm/geo/area.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::array::*;
use wasm_bindgen::prelude::*;

macro_rules! impl_area {
($struct_name:ident) => {
#[wasm_bindgen]
impl $struct_name {
/// Unsigned planar area of a geometry.
#[wasm_bindgen]
pub fn area(&self) -> FloatArray {
use geoarrow::algorithm::geo::Area;
FloatArray(Area::unsigned_area(&self.0))
}

/// Signed planar area of a geometry.
#[wasm_bindgen]
pub fn signed_area(&self) -> FloatArray {
use geoarrow::algorithm::geo::Area;
FloatArray(Area::signed_area(&self.0))
}
}
};
}

impl_area!(PointArray);
impl_area!(LineStringArray);
impl_area!(PolygonArray);
impl_area!(MultiPointArray);
impl_area!(MultiLineStringArray);
impl_area!(MultiPolygonArray);
impl_area!(GeometryArray);
27 changes: 27 additions & 0 deletions js/src/algorithm/geo/center.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::array::*;
use wasm_bindgen::prelude::*;

macro_rules! impl_center {
($struct_name:ident) => {
#[wasm_bindgen]
impl $struct_name {
/// Compute the center of geometries
///
/// This first computes the axis-aligned bounding rectangle, then takes the center of
/// that box
#[wasm_bindgen]
pub fn center(&self) -> PointArray {
use geoarrow::algorithm::geo::Center;
PointArray(Center::center(&self.0))
}
}
};
}

impl_center!(PointArray);
impl_center!(LineStringArray);
impl_center!(PolygonArray);
impl_center!(MultiPointArray);
impl_center!(MultiLineStringArray);
impl_center!(MultiPolygonArray);
impl_center!(GeometryArray);
29 changes: 29 additions & 0 deletions js/src/algorithm/geo/centroid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::array::*;
use wasm_bindgen::prelude::*;

macro_rules! impl_centroid {
($struct_name:ident) => {
#[wasm_bindgen]
impl $struct_name {
/// Calculation of the centroid.
/// The centroid is the arithmetic mean position of all points in the shape.
/// Informally, it is the point at which a cutout of the shape could be perfectly
/// balanced on the tip of a pin.
/// The geometric centroid of a convex object always lies in the object.
/// A non-convex object might have a centroid that _is outside the object itself_.
#[wasm_bindgen]
pub fn centroid(&self) -> PointArray {
use geoarrow::algorithm::geo::Centroid;
PointArray(Centroid::centroid(&self.0))
}
}
};
}

impl_centroid!(PointArray);
impl_centroid!(LineStringArray);
impl_centroid!(PolygonArray);
impl_centroid!(MultiPointArray);
impl_centroid!(MultiLineStringArray);
impl_centroid!(MultiPolygonArray);
impl_centroid!(GeometryArray);
35 changes: 35 additions & 0 deletions js/src/algorithm/geo/chamberlain_duquette_area.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::array::*;
use wasm_bindgen::prelude::*;

macro_rules! impl_alg {
($struct_name:ident) => {
#[wasm_bindgen]
impl $struct_name {
/// Calculate the unsigned approximate geodesic area of a `Geometry`.
#[wasm_bindgen]
pub fn chamberlain_duquette_unsigned_area(&self) -> FloatArray {
use geoarrow::algorithm::geo::ChamberlainDuquetteArea;
FloatArray(ChamberlainDuquetteArea::chamberlain_duquette_unsigned_area(
&self.0,
))
}

/// Calculate the signed approximate geodesic area of a `Geometry`.
#[wasm_bindgen]
pub fn chamberlain_duquette_signed_area(&self) -> FloatArray {
use geoarrow::algorithm::geo::ChamberlainDuquetteArea;
FloatArray(ChamberlainDuquetteArea::chamberlain_duquette_signed_area(
&self.0,
))
}
}
};
}

impl_alg!(PointArray);
impl_alg!(LineStringArray);
impl_alg!(PolygonArray);
impl_alg!(MultiPointArray);
impl_alg!(MultiLineStringArray);
impl_alg!(MultiPolygonArray);
impl_alg!(GeometryArray);
30 changes: 30 additions & 0 deletions js/src/algorithm/geo/convex_hull.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::array::*;
use wasm_bindgen::prelude::*;

macro_rules! impl_alg {
($struct_name:ident) => {
#[wasm_bindgen]
impl $struct_name {
/// Returns the convex hull of a Polygon. The hull is always oriented
/// counter-clockwise.
///
/// This implementation uses the QuickHull algorithm, based on [Barber, C. Bradford;
/// Dobkin, David P.; Huhdanpaa, Hannu (1 December
/// 1996)](https://dx.doi.org/10.1145%2F235815.235821) Original paper here:
/// <http://www.cs.princeton.edu/~dpd/Papers/BarberDobkinHuhdanpaa.pdf>
#[wasm_bindgen]
pub fn convex_hull(&self) -> PolygonArray {
use geoarrow::algorithm::geo::ConvexHull;
PolygonArray(ConvexHull::convex_hull(&self.0))
}
}
};
}

impl_alg!(PointArray);
impl_alg!(LineStringArray);
impl_alg!(PolygonArray);
impl_alg!(MultiPointArray);
impl_alg!(MultiLineStringArray);
impl_alg!(MultiPolygonArray);
impl_alg!(GeometryArray);
28 changes: 28 additions & 0 deletions js/src/algorithm/geo/dimensions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::array::*;
use wasm_bindgen::prelude::*;

macro_rules! impl_alg {
($struct_name:ident) => {
#[wasm_bindgen]
impl $struct_name {
/// Some geometries, like a `MultiPoint`, can have zero coordinates - we call these
/// `empty`.
///
/// Types like `Point`, which have at least one coordinate by construction, can never
/// be considered empty.
#[wasm_bindgen]
pub fn is_empty(&self) -> BooleanArray {
use geoarrow::algorithm::geo::HasDimensions;
BooleanArray(HasDimensions::is_empty(&self.0))
}
}
};
}

impl_alg!(PointArray);
impl_alg!(LineStringArray);
impl_alg!(PolygonArray);
impl_alg!(MultiPointArray);
impl_alg!(MultiLineStringArray);
impl_alg!(MultiPolygonArray);
impl_alg!(GeometryArray);
6 changes: 6 additions & 0 deletions js/src/algorithm/geo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
pub mod area;
pub mod center;
pub mod centroid;
pub mod chamberlain_duquette_area;
pub mod convex_hull;
pub mod dimensions;
pub mod euclidean_length;
pub mod geodesic_length;
pub mod haversine_length;
Expand Down
1 change: 0 additions & 1 deletion js/src/array/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::array::ffi::FFIArrowArray;
use crate::array::polygon::PolygonArray;
use crate::array::primitive::BooleanArray;
use crate::array::primitive::FloatArray;
use crate::array::{
LineStringArray, MultiLineStringArray, MultiPointArray, MultiPolygonArray, PointArray,
Expand Down
2 changes: 0 additions & 2 deletions js/src/array/linestring.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::array::ffi::FFIArrowArray;
use crate::array::point::PointArray;
use crate::array::polygon::PolygonArray;
use crate::array::primitive::BooleanArray;
use crate::array::primitive::FloatArray;
use crate::array::CoordBuffer;
Expand Down
36 changes: 0 additions & 36 deletions js/src/array/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,6 @@ macro_rules! impl_geometry_array {
Ok(GeometryArray(affine_transform(&self.into(), transform.0)?))
}

#[wasm_bindgen]
pub fn area(&self) -> FloatArray {
use geoarrow::algorithm::geo::Area;
FloatArray(Area::unsigned_area(&self.0))
}

#[wasm_bindgen]
pub fn center(&self) -> PointArray {
use geoarrow::algorithm::geo::Center;
PointArray(Center::center(&self.0))
}

#[wasm_bindgen]
pub fn centroid(&self) -> PointArray {
use geoarrow::algorithm::geo::Centroid;
PointArray(Centroid::centroid(&self.0))
}

#[wasm_bindgen]
pub fn convex_hull(&self) -> PolygonArray {
use geoarrow::algorithm::geo::ConvexHull;
PolygonArray(ConvexHull::convex_hull(&self.0))
}

#[wasm_bindgen]
pub fn geodesic_area(&self) -> WasmResult<FloatArray> {
use geoarrow::algorithm::geo::geodesic_area_unsigned;
Expand All @@ -50,12 +26,6 @@ macro_rules! impl_geometry_array {
Ok(FloatArray(geodesic_area_signed(&self.into())?))
}

#[wasm_bindgen]
pub fn is_empty(&self) -> BooleanArray {
use geoarrow::algorithm::geo::HasDimensions;
BooleanArray(HasDimensions::is_empty(&self.0))
}

#[cfg(feature = "geodesy")]
#[wasm_bindgen]
pub fn reproject_rs(
Expand Down Expand Up @@ -97,12 +67,6 @@ macro_rules! impl_geometry_array {
)?))
}

#[wasm_bindgen]
pub fn signed_area(&self) -> FloatArray {
use geoarrow::algorithm::geo::Area;
FloatArray(Area::signed_area(&self.0))
}

#[wasm_bindgen]
pub fn skew(
&self,
Expand Down
2 changes: 0 additions & 2 deletions js/src/array/multilinestring.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::array::ffi::FFIArrowArray;
use crate::array::point::PointArray;
use crate::array::polygon::PolygonArray;
use crate::array::primitive::BooleanArray;
use crate::array::primitive::FloatArray;
use crate::array::CoordBuffer;
Expand Down
2 changes: 0 additions & 2 deletions js/src/array/multipoint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::array::ffi::FFIArrowArray;
use crate::array::point::PointArray;
use crate::array::polygon::PolygonArray;
use crate::array::primitive::BooleanArray;
use crate::array::primitive::FloatArray;
use crate::array::CoordBuffer;
Expand Down
2 changes: 0 additions & 2 deletions js/src/array/multipolygon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::array::ffi::FFIArrowArray;
use crate::array::point::PointArray;
use crate::array::polygon::PolygonArray;
use crate::array::primitive::BooleanArray;
use crate::array::primitive::FloatArray;
use crate::array::CoordBuffer;
Expand Down
1 change: 0 additions & 1 deletion js/src/array/point.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::array::ffi::FFIArrowArray;
use crate::array::polygon::PolygonArray;
use crate::array::primitive::BooleanArray;
use crate::array::primitive::FloatArray;
use crate::array::CoordBuffer;
Expand Down
1 change: 0 additions & 1 deletion js/src/array/polygon.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::array::ffi::FFIArrowArray;
use crate::array::point::PointArray;
use crate::array::primitive::BooleanArray;
use crate::array::primitive::FloatArray;
use crate::array::CoordBuffer;
Expand Down

0 comments on commit abd1774

Please sign in to comment.