You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@sedona.apache.org by "prantogg (via GitHub)" <gi...@apache.org> on 2025/11/10 02:43:48 UTC

Re: [PR] [WIP] Ensure geometry validity, CCW orientation and antimeridian handling [sedona-spatialbench]

prantogg commented on code in PR #60:
URL: https://github.com/apache/sedona-spatialbench/pull/60#discussion_r2508560982


##########
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 {

Review Comment:
   It's for cases where longitude wrapping is not applied beforehand.



##########
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 {

Review Comment:
   It's for cases where longitude wrapping is not applied beforehand.



-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org