james-willis commented on code in PR #60:
URL: 
https://github.com/apache/sedona-spatialbench/pull/60#discussion_r2511600039


##########
spatialbench/src/spatial/utils/antimeridian.rs:
##########
@@ -0,0 +1,258 @@
+use geo::{Centroid, LineString, Polygon};
+
+/// Wraps a longitude value to ensure it stays within the valid range of 
[-180, 180] degrees.
+///
+/// Longitude is a circular coordinate:
+/// - If longitude exceeds 180°, it wraps around from the eastern hemisphere 
back to the western hemisphere.
+/// - If longitude is below -180°, it wraps around from the western hemisphere 
back to the eastern hemisphere.
+pub fn wrap_around_longitude(mut lon: f64) -> f64 {
+    while lon > 180.0 {
+        lon -= 360.0;
+    }
+    while lon < -180.0 {
+        lon += 360.0;
+    }
+    lon
+}
+
+/// Checks if a polygon crosses the dateline (antimeridian at ±180°)
+pub fn crosses_dateline(polygon: &Polygon) -> bool {
+    let coords = polygon.exterior().coords();
+    let mut has_east = false;
+    let mut has_west = false;
+
+    for coord in coords {
+        if (coord.x > 90.0 && coord.x <= 180.0) || coord.x < -180.0 {
+            has_east = true;
+        }
+        if coord.x > 180.0 || (coord.x >= -180.0 && coord.x < -90.0) {
+            has_west = true;
+        }
+        if has_east && has_west {
+            return true;
+        }
+    }
+    false
+}
+
+/// Clamps a polygon's longitude coordinates to one side of the antimeridian 
(±180°).
+///
+/// When a polygon crosses the dateline, this function constrains its 
coordinates to remain
+/// on either the eastern (0° to 180°) or western (-180° to 0°) hemisphere 
based on where
+/// the polygon's centroid is located.
+///
+/// # Behavior
+/// - If the centroid is in the eastern hemisphere (≥ 0°), coordinates are 
clamped to [0°, 180°]
+///   or kept at their original values if already within [-180°, 0°]
+/// - If the centroid is in the western hemisphere (< 0°), coordinates are 
clamped to [-180°, 0°]
+///   or kept at their original values if already within [0°, 180°]
+/// - Latitude values (y-coordinates) remain unchanged
+///
+pub fn clamp_polygon_to_dateline(polygon: &Polygon) -> Polygon {
+    let centroid = polygon.centroid().expect("Polygon should have centroid");
+    let east_bound = centroid.x() >= 0.0;
+    let keep_east = (centroid.x() >= 0.0 && centroid.x() <= 180.0) || 
(centroid.x() < -180.0);
+
+    let exterior_coords: Vec<_> = polygon
+        .exterior()
+        .coords()
+        .map(|coord| {

Review Comment:
   Is there some risk this creates an invalid polygon?



-- 
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]

Reply via email to