This is an automated email from the ASF dual-hosted git repository.

jiayu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sedona.git


The following commit(s) were added to refs/heads/master by this push:
     new 195359511 [SEDONA-608] Fix ST_IsPolygonCW, ST_IsPolygonCCW, 
ST_ForcePolygonCW and ST_ForcePolygonCCW (#1476)
195359511 is described below

commit 195359511914f17d5ac5e165c8c02a735d1cbc48
Author: Furqaan Khan <[email protected]>
AuthorDate: Thu Jun 13 00:52:24 2024 -0400

    [SEDONA-608] Fix ST_IsPolygonCW, ST_IsPolygonCCW, ST_ForcePolygonCW and 
ST_ForcePolygonCCW (#1476)
    
    * fix: interior ring bug
    
    * fix: spotless error
---
 .../main/java/org/apache/sedona/common/Functions.java    |  8 ++++++++
 .../java/org/apache/sedona/common/FunctionsTest.java     | 16 ++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/common/src/main/java/org/apache/sedona/common/Functions.java 
b/common/src/main/java/org/apache/sedona/common/Functions.java
index 18a8403bf..552c37c8e 100644
--- a/common/src/main/java/org/apache/sedona/common/Functions.java
+++ b/common/src/main/java/org/apache/sedona/common/Functions.java
@@ -1032,6 +1032,10 @@ public class Functions {
     LinearRing exteriorRing = geom.getExteriorRing();
     boolean isExteriorRingCW = 
!Orientation.isCCW(exteriorRing.getCoordinateSequence());
 
+    if (geom.getNumInteriorRing() == 0) {
+      return isExteriorRingCW;
+    }
+
     boolean isInteriorRingCW = 
Orientation.isCCW(geom.getInteriorRingN(0).getCoordinateSequence());
     for (int i = 1; i < geom.getNumInteriorRing(); i++) {
       isInteriorRingCW =
@@ -1141,6 +1145,10 @@ public class Functions {
     LinearRing exteriorRing = geom.getExteriorRing();
     boolean isExteriorRingCCW = 
Orientation.isCCW(exteriorRing.getCoordinateSequence());
 
+    if (geom.getNumInteriorRing() == 0) {
+      return isExteriorRingCCW;
+    }
+
     boolean isInteriorRingCCW =
         !Orientation.isCCW(geom.getInteriorRingN(0).getCoordinateSequence());
     for (int i = 1; i < geom.getNumInteriorRing(); i++) {
diff --git a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java 
b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java
index ba71b3ee6..eac55be8b 100644
--- a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java
+++ b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java
@@ -1020,6 +1020,11 @@ public class FunctionsTest extends TestBase {
         "POLYGON ((20 35, 45 20, 30 5, 10 10, 10 30, 20 35), (30 20, 20 25, 20 
15, 30 20))";
     assertEquals(expected, actual);
 
+    polyCCW = Constructors.geomFromWKT("POLYGON ((20 35, 10 30, 10 10, 30 5, 
45 20, 20 35))", 0);
+    actual = Functions.asWKT(Functions.forcePolygonCCW(polyCCW));
+    expected = "POLYGON ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35))";
+    assertEquals(expected, actual);
+
     // both exterior ring and interior ring are counter-clockwise
     polyCCW =
         Constructors.geomFromWKT(
@@ -1062,6 +1067,11 @@ public class FunctionsTest extends TestBase {
         "POLYGON ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35), (30 20, 20 15, 20 
25, 30 20))";
     assertEquals(expected, actual);
 
+    polyCW = Constructors.geomFromWKT("POLYGON ((20 35, 45 20, 30 5, 10 10, 10 
30, 20 35))", 0);
+    actual = Functions.asWKT(Functions.forcePolygonCCW(polyCW));
+    expected = "POLYGON ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35))";
+    assertEquals(expected, actual);
+
     // both exterior ring and interior ring are counter-clockwise
     polyCW =
         Constructors.geomFromWKT(
@@ -1101,6 +1111,9 @@ public class FunctionsTest extends TestBase {
             "POLYGON ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 
20 25, 30 20))", 0);
     assertFalse(Functions.isPolygonCW(polyCCW));
 
+    polyCCW = Constructors.geomFromWKT("POLYGON ((20 35, 10 30, 10 10, 30 5, 
45 20, 20 35))", 0);
+    assertFalse(Functions.isPolygonCW(polyCCW));
+
     Geometry polyCW =
         Constructors.geomFromWKT(
             "POLYGON ((20 35, 45 20, 30 5, 10 10, 10 30, 20 35), (30 20, 20 
25, 20 15, 30 20))", 0);
@@ -1126,6 +1139,9 @@ public class FunctionsTest extends TestBase {
             "POLYGON ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 
20 25, 30 20))", 0);
     assertTrue(Functions.isPolygonCCW(polyCCW));
 
+    polyCCW = Constructors.geomFromWKT("POLYGON ((20 35, 10 30, 10 10, 30 5, 
45 20, 20 35))", 0);
+    assertTrue(Functions.isPolygonCCW(polyCCW));
+
     Geometry polyCW =
         Constructors.geomFromWKT(
             "POLYGON ((20 35, 45 20, 30 5, 10 10, 10 30, 20 35), (30 20, 20 
25, 20 15, 30 20))", 0);

Reply via email to