Kontinuation commented on code in PR #194: URL: https://github.com/apache/sedona-db/pull/194#discussion_r2413849966
########## rust/sedona-geo-traits-ext/src/rect.rs: ########## @@ -0,0 +1,331 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// Extend RectTrait traits for the `geo-traits` crate + +use geo_traits::{CoordTrait, GeometryTrait, RectTrait, UnimplementedRect}; +use geo_types::{coord, Coord, CoordFloat, CoordNum, Line, LineString, Polygon, Rect}; +use num_traits::One; + +use crate::{CoordTraitExt, GeoTraitExtWithTypeTag, RectTag}; + +static RECT_INVALID_BOUNDS_ERROR: &str = "Failed to create Rect: 'min' coordinate's x/y value must be smaller or equal to the 'max' x/y value"; + +/// Extension trait that augments [`geo_traits::RectTrait`] with additional +/// helpers for working with axis-aligned bounding boxes. +pub trait RectTraitExt: RectTrait + GeoTraitExtWithTypeTag<Tag = RectTag> +where + <Self as GeometryTrait>::T: CoordNum, +{ + /// Extension-aware coordinate type returned from accessors. + type CoordTypeExt<'a>: 'a + CoordTraitExt<T = <Self as GeometryTrait>::T> + where + Self: 'a; + + /// Returns the minimum corner using the extension trait wrapper. + fn min_ext(&self) -> Self::CoordTypeExt<'_>; + + /// Returns the maximum corner using the extension trait wrapper. + fn max_ext(&self) -> Self::CoordTypeExt<'_>; + + #[inline] + /// Returns the minimum corner as a `geo-types::Coord`. + fn min_coord(&self) -> Coord<<Self as GeometryTrait>::T> { + self.min_ext().geo_coord() + } + + #[inline] + /// Returns the maximum corner as a `geo-types::Coord`. + fn max_coord(&self) -> Coord<<Self as GeometryTrait>::T> { + self.max_ext().geo_coord() + } + + #[inline] + /// Constructs a [`geo_types::Rect`] from the extension trait accessors. + fn geo_rect(&self) -> Rect<<Self as GeometryTrait>::T> { + Rect::new(self.min_coord(), self.max_coord()) + } + + #[inline] + /// Returns the width of the rectangle. + fn width(&self) -> <Self as GeometryTrait>::T { + self.max().x() - self.min().x() + } + + #[inline] + /// Returns the height of the rectangle. + fn height(&self) -> <Self as GeometryTrait>::T { + self.max().y() - self.min().y() + } + + /// Converts the rectangle into a polygon with four corners. + fn to_polygon(&self) -> Polygon<<Self as GeometryTrait>::T> + where + <Self as GeometryTrait>::T: Clone, + { + let min_coord = self.min_coord(); + let max_coord = self.max_coord(); + + let min_x = min_coord.x; + let min_y = min_coord.y; + let max_x = max_coord.x; + let max_y = max_coord.y; + + let line_string = LineString::new(vec![ + Coord { x: min_x, y: min_y }, + Coord { x: min_x, y: max_y }, + Coord { x: max_x, y: max_y }, + Coord { x: max_x, y: min_y }, + Coord { x: min_x, y: min_y }, + ]); + + Polygon::new(line_string, vec![]) + } + + /// Returns the four outer edges as line segments. + fn to_lines(&self) -> [Line<<Self as GeometryTrait>::T>; 4] { + let min_coord = self.min_coord(); + let max_coord = self.max_coord(); + [ + Line::new( + coord! { + x: max_coord.x, + y: min_coord.y, + }, + coord! { + x: max_coord.x, + y: max_coord.y, + }, + ), + Line::new( + coord! { + x: max_coord.x, + y: min_coord.y, + }, + coord! { + x: min_coord.x, + y: max_coord.y, + }, + ), Review Comment: Fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
