Skip to content

Commit

Permalink
Merge point and coord traits
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Oct 10, 2024
1 parent b2312e4 commit 74a99c8
Show file tree
Hide file tree
Showing 41 changed files with 182 additions and 671 deletions.
46 changes: 23 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 5 additions & 35 deletions src/algorithm/native/bounding_rect.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::ops::Add;

use crate::geo_traits::{
CoordTrait, GeometryCollectionTrait, GeometryTrait, GeometryType, LineStringTrait,
MultiLineStringTrait, MultiPointTrait, MultiPolygonTrait, PointTrait, PolygonTrait, RectTrait,
GeometryCollectionTrait, GeometryTrait, GeometryType, LineStringTrait, MultiLineStringTrait,
MultiPointTrait, MultiPolygonTrait, PointTrait, PolygonTrait, RectTrait,
};
use geo::{Coord, Rect};

Expand Down Expand Up @@ -61,36 +61,6 @@ impl BoundingRect {
}
}

pub fn add_coord(&mut self, coord: &impl CoordTrait<T = f64>) {
let x = coord.x();
let y = coord.y();
let z = coord.nth(2);

if x < self.minx {
self.minx = x;
}
if y < self.miny {
self.miny = y;
}
if let Some(z) = z {
if z < self.minz {
self.minz = z;
}
}

if x > self.maxx {
self.maxx = x;
}
if y > self.maxy {
self.maxy = y;
}
if let Some(z) = z {
if z > self.maxz {
self.maxz = z;
}
}
}

pub fn add_point(&mut self, point: &impl PointTrait<T = f64>) {
let x = point.x();
let y = point.y();
Expand Down Expand Up @@ -123,7 +93,7 @@ impl BoundingRect {

pub fn add_line_string(&mut self, line_string: &impl LineStringTrait<T = f64>) {
for coord in line_string.coords() {
self.add_coord(&coord);
self.add_point(&coord);
}
}

Expand Down Expand Up @@ -181,8 +151,8 @@ impl BoundingRect {
}

pub fn add_rect(&mut self, rect: &impl RectTrait<T = f64>) {
self.add_coord(&rect.lower());
self.add_coord(&rect.upper());
self.add_point(&rect.lower());
self.add_point(&rect.upper());
}

pub fn update(&mut self, other: &BoundingRect) {
Expand Down
64 changes: 5 additions & 59 deletions src/algorithm/native/eq.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,11 @@
use crate::geo_traits::{
CoordTrait, GeometryCollectionTrait, GeometryTrait, GeometryType, LineStringTrait,
MultiLineStringTrait, MultiPointTrait, MultiPolygonTrait, PointTrait, PolygonTrait, RectTrait,
GeometryCollectionTrait, GeometryTrait, GeometryType, LineStringTrait, MultiLineStringTrait,
MultiPointTrait, MultiPolygonTrait, PointTrait, PolygonTrait, RectTrait,
};
use arrow_array::OffsetSizeTrait;
use arrow_buffer::OffsetBuffer;
use geo::CoordFloat;

#[inline]
pub fn coord_eq_allow_nan<T: CoordFloat>(
left: &impl CoordTrait<T = T>,
right: &impl CoordTrait<T = T>,
) -> bool {
let left_dim = left.dim();
if left_dim != right.dim() {
return false;
}

// Specifically check for NaN because two points defined to be
// TODO: in the future add an `is_empty` to the PointTrait and then you shouldn't check for
// NaN manually
match left_dim {
2 => {
if left.x().is_nan() && right.x().is_nan() && left.y().is_nan() && right.y().is_nan() {
return true;
}
}
3 => {
if left.x().is_nan()
&& right.x().is_nan()
&& left.y().is_nan()
&& right.y().is_nan()
&& left.nth_unchecked(2).is_nan()
&& right.nth_unchecked(2).is_nan()
{
return true;
}
}
_ => panic!(),
}

for i in 0..left_dim {
if left.nth_unchecked(i) != right.nth_unchecked(i) {
return false;
}
}

true
}

#[inline]
pub fn coord_eq<T: CoordFloat>(
left: &impl CoordTrait<T = T>,
right: &impl CoordTrait<T = T>,
) -> bool {
if left.dim() != right.dim() {
return false;
}

left.x_y() == right.x_y()
}

#[inline]
pub fn point_eq<T: CoordFloat>(
left: &impl PointTrait<T = T>,
Expand Down Expand Up @@ -123,7 +69,7 @@ pub fn line_string_eq<T: CoordFloat>(
}

for (left_coord, right_coord) in left.coords().zip(right.coords()) {
if !coord_eq(&left_coord, &right_coord) {
if !point_eq(&left_coord, &right_coord, false) {
return false;
}
}
Expand Down Expand Up @@ -240,11 +186,11 @@ pub fn rect_eq<T: CoordFloat>(left: &impl RectTrait<T = T>, right: &impl RectTra
return false;
}

if !coord_eq(&left.lower(), &right.lower()) {
if !point_eq(&left.lower(), &right.lower(), false) {
return false;
}

if !coord_eq(&left.upper(), &right.upper()) {
if !point_eq(&left.upper(), &right.upper(), false) {
return false;
}

Expand Down
9 changes: 1 addition & 8 deletions src/array/coord/combined/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::f64;
use crate::array::{
CoordBuffer, CoordType, InterleavedCoordBufferBuilder, SeparatedCoordBufferBuilder,
};
use crate::geo_traits::{CoordTrait, PointTrait};
use crate::geo_traits::PointTrait;

/// The GeoArrow equivalent to `Vec<Coord>`: a mutable collection of coordinates.
///
Expand Down Expand Up @@ -93,13 +93,6 @@ impl<const D: usize> CoordBufferBuilder<D> {
CoordBufferBuilder::Separated(cb) => cb.push_point(point),
}
}

pub fn push_coord(&mut self, coord: &impl CoordTrait<T = f64>) {
match self {
CoordBufferBuilder::Interleaved(cb) => cb.push_coord(coord),
CoordBufferBuilder::Separated(cb) => cb.push_coord(coord),
}
}
}

impl CoordBufferBuilder<2> {
Expand Down
4 changes: 2 additions & 2 deletions src/array/coord/interleaved/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use crate::array::{CoordType, InterleavedCoordBufferBuilder};
use crate::datatypes::coord_type_to_data_type;
use crate::error::{GeoArrowError, Result};
use crate::geo_traits::CoordTrait;
use crate::geo_traits::PointTrait;
use crate::scalar::InterleavedCoord;
use crate::trait_::IntoArrow;
use arrow_array::{Array, FixedSizeListArray, Float64Array};
Expand Down Expand Up @@ -187,7 +187,7 @@ impl<const D: usize> From<&[f64]> for InterleavedCoordBuffer<D> {
}
}

impl<G: CoordTrait<T = f64>> From<&[G]> for InterleavedCoordBuffer<2> {
impl<G: PointTrait<T = f64>> From<&[G]> for InterleavedCoordBuffer<2> {
fn from(other: &[G]) -> Self {
let mut_arr: InterleavedCoordBufferBuilder<2> = other.into();
mut_arr.into()
Expand Down
11 changes: 3 additions & 8 deletions src/array/coord/interleaved/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::array::InterleavedCoordBuffer;
use crate::geo_traits::{CoordTrait, PointTrait};
use crate::geo_traits::PointTrait;

/// The GeoArrow equivalent to `Vec<Coord>`: a mutable collection of coordinates.
///
Expand Down Expand Up @@ -75,11 +75,6 @@ impl<const D: usize> InterleavedCoordBufferBuilder<D> {
let buf: [f64; D] = core::array::from_fn(|i| point.nth(i).unwrap_or(f64::NAN));
self.coords.extend_from_slice(&buf);
}

pub fn push_coord(&mut self, coord: &impl CoordTrait<T = f64>) {
let buf: [f64; D] = core::array::from_fn(|i| coord.nth(i).unwrap_or(f64::NAN));
self.coords.extend_from_slice(&buf);
}
}

impl InterleavedCoordBufferBuilder<2> {
Expand Down Expand Up @@ -111,11 +106,11 @@ impl<const D: usize> From<InterleavedCoordBufferBuilder<D>> for InterleavedCoord
}
}

impl<G: CoordTrait<T = f64>, const D: usize> From<&[G]> for InterleavedCoordBufferBuilder<D> {
impl<G: PointTrait<T = f64>, const D: usize> From<&[G]> for InterleavedCoordBufferBuilder<D> {
fn from(value: &[G]) -> Self {
let mut buffer = InterleavedCoordBufferBuilder::with_capacity(value.len());
for coord in value {
buffer.push_coord(coord);
buffer.push_point(coord);
}
buffer
}
Expand Down
4 changes: 2 additions & 2 deletions src/array/coord/separated/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use arrow_schema::{DataType, Field};

use crate::array::{CoordType, SeparatedCoordBufferBuilder};
use crate::error::{GeoArrowError, Result};
use crate::geo_traits::CoordTrait;
use crate::geo_traits::PointTrait;
use crate::scalar::SeparatedCoord;
use crate::trait_::IntoArrow;

Expand Down Expand Up @@ -201,7 +201,7 @@ impl TryFrom<(Vec<f64>, Vec<f64>)> for SeparatedCoordBuffer<2> {
}
}

impl<G: CoordTrait<T = f64>> From<&[G]> for SeparatedCoordBuffer<2> {
impl<G: PointTrait<T = f64>> From<&[G]> for SeparatedCoordBuffer<2> {
fn from(other: &[G]) -> Self {
let mut_arr: SeparatedCoordBufferBuilder<2> = other.into();
mut_arr.into()
Expand Down
Loading

0 comments on commit 74a99c8

Please sign in to comment.