jiayuasu commented on code in PR #1259:
URL: https://github.com/apache/sedona/pull/1259#discussion_r1505375839


##########
common/src/main/java/org/apache/sedona/common/Predicates.java:
##########
@@ -13,9 +13,11 @@
  */

Review Comment:
   Please put this one in `Functions`. `Predicates` are for functions that take 
two geometries as input and return true/false.



##########
common/src/main/java/org/apache/sedona/common/Predicates.java:
##########
@@ -62,4 +64,82 @@ public static boolean dWithin(Geometry leftGeometry, 
Geometry rightGeometry, dou
             return leftGeometry.isWithinDistance(rightGeometry, distance);
         }
     }
+
+    /**
+     * Checks if a geometry crosses the International Date Line.
+     *
+     * @param geometry The geometry to check.
+     * @return True if the geometry crosses the Date Line, false otherwise.
+     */
+    public static boolean crossesDateLine(Geometry geometry) {
+        if (geometry == null || geometry.isEmpty()) {
+            return false;
+        }
+
+        AtomicBoolean crossesDateLine = new AtomicBoolean(false);
+
+        CoordinateSequenceFilter filter = new CoordinateSequenceFilter() {
+            private Coordinate previous = null;
+
+            @Override
+            public void filter(CoordinateSequence seq, int i) {
+                if (i == 0) {
+                    previous = seq.getCoordinateCopy(i);
+                    return;
+                }
+
+                Coordinate current = seq.getCoordinateCopy(i);
+                if (Math.abs(current.x - previous.x) > 180) {
+                    crossesDateLine.set(true);
+                }
+
+                previous = current;
+            }
+
+            @Override
+            public boolean isDone() {
+                return crossesDateLine.get();
+            }
+
+            @Override
+            public boolean isGeometryChanged() {
+                return false;
+            }
+        };
+
+        if (geometry instanceof GeometryCollection) {
+            GeometryCollection collection = (GeometryCollection) geometry;
+            for (int i = 0; i < collection.getNumGeometries(); i++) {
+                Geometry part = collection.getGeometryN(i);
+                part.apply(filter);
+                if (crossesDateLine.get()) {
+                    return true;
+                }
+            }
+        } else {
+            geometry.apply(filter);
+        }
+
+        return crossesDateLine.get();
+    }
+
+
+//    public static boolean crossesDateLine(Geometry geometry) {

Review Comment:
   Please remove the comments.



##########
common/src/main/java/org/apache/sedona/common/Predicates.java:
##########
@@ -62,4 +64,82 @@ public static boolean dWithin(Geometry leftGeometry, 
Geometry rightGeometry, dou
             return leftGeometry.isWithinDistance(rightGeometry, distance);
         }
     }
+
+    /**
+     * Checks if a geometry crosses the International Date Line.
+     *
+     * @param geometry The geometry to check.
+     * @return True if the geometry crosses the Date Line, false otherwise.
+     */
+    public static boolean crossesDateLine(Geometry geometry) {
+        if (geometry == null || geometry.isEmpty()) {
+            return false;
+        }
+
+        AtomicBoolean crossesDateLine = new AtomicBoolean(false);

Review Comment:
   Is there a reason why use `AtomicBoolean`? I believe the filter functions 
will be called in a sequential order?



##########
common/src/main/java/org/apache/sedona/common/Predicates.java:
##########
@@ -62,4 +64,82 @@ public static boolean dWithin(Geometry leftGeometry, 
Geometry rightGeometry, dou
             return leftGeometry.isWithinDistance(rightGeometry, distance);
         }
     }
+
+    /**
+     * Checks if a geometry crosses the International Date Line.
+     *
+     * @param geometry The geometry to check.
+     * @return True if the geometry crosses the Date Line, false otherwise.
+     */
+    public static boolean crossesDateLine(Geometry geometry) {
+        if (geometry == null || geometry.isEmpty()) {
+            return false;
+        }
+
+        AtomicBoolean crossesDateLine = new AtomicBoolean(false);
+
+        CoordinateSequenceFilter filter = new CoordinateSequenceFilter() {
+            private Coordinate previous = null;
+
+            @Override
+            public void filter(CoordinateSequence seq, int i) {
+                if (i == 0) {
+                    previous = seq.getCoordinateCopy(i);
+                    return;
+                }
+
+                Coordinate current = seq.getCoordinateCopy(i);
+                if (Math.abs(current.x - previous.x) > 180) {
+                    crossesDateLine.set(true);
+                }
+
+                previous = current;
+            }
+
+            @Override
+            public boolean isDone() {
+                return crossesDateLine.get();
+            }
+
+            @Override
+            public boolean isGeometryChanged() {
+                return false;
+            }
+        };
+
+        if (geometry instanceof GeometryCollection) {

Review Comment:
   Why explicitly do this for `GeometryCollection`? Note that: 
`GeometryCollection` can have nested `GeometryCollection` inside. We should 
avoid implementing our own logic to iterate GeometryCollection. If we really 
want to do it, this should be done via a recursive function.



-- 
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-unsubscr...@sedona.apache.org

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

Reply via email to