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

jiayu pushed a commit to branch fix/2403-setsrid-polygon-empty
in repository https://gitbox.apache.org/repos/asf/sedona.git

commit 2f4e7e534cfea39f0c7ac255ebcdd5a7fa4f2905
Author: Jia Yu <[email protected]>
AuthorDate: Sat Feb 7 01:16:08 2026 -0800

    fix(SetSRID): handle POLYGON EMPTY and other empty geometry types
    
    JTS 1.20.0 GeometryEditor.editPolygon() returns the original empty
    polygon without copying it to the new GeometryFactory, so the SRID
    is not updated. Work around this by explicitly calling setSRID()
    after createGeometry().
    
    This is a no-op for non-empty geometries (SRID is already correct).
    
    Fixes #2403
---
 .../java/org/apache/sedona/common/Functions.java     |  7 ++++++-
 .../java/org/apache/sedona/common/FunctionsTest.java | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

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 0be88a9b1b..e7a0138448 100644
--- a/common/src/main/java/org/apache/sedona/common/Functions.java
+++ b/common/src/main/java/org/apache/sedona/common/Functions.java
@@ -920,7 +920,12 @@ public class Functions {
             geometry.getPrecisionModel(),
             srid,
             geometry.getFactory().getCoordinateSequenceFactory());
-    return factory.createGeometry(geometry);
+    Geometry newGeom = factory.createGeometry(geometry);
+    // Workaround for JTS bug: GeometryEditor.editPolygon returns the original
+    // empty polygon without copying it to the new factory, so the SRID is not
+    // updated for POLYGON EMPTY (and similar empty geometry types).
+    newGeom.setSRID(srid);
+    return newGeom;
   }
 
   public static int getSRID(Geometry geometry) {
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 e379e67d6d..cb098c8dc7 100644
--- a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java
+++ b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java
@@ -3952,6 +3952,26 @@ public class FunctionsTest extends TestBase {
     assertEquals(expected4, actual4);
   }
 
+  @Test
+  public void setSRIDEmptyGeometries() throws ParseException {
+    // Regression test for GH-2403: SetSRID doesn't work on POLYGON EMPTY
+    String[] emptyWkts = {
+      "POINT EMPTY",
+      "LINESTRING EMPTY",
+      "POLYGON EMPTY",
+      "MULTIPOINT EMPTY",
+      "MULTILINESTRING EMPTY",
+      "MULTIPOLYGON EMPTY",
+      "GEOMETRYCOLLECTION EMPTY"
+    };
+    for (String wkt : emptyWkts) {
+      Geometry geom = Constructors.geomFromWKT(wkt, 0);
+      Geometry result = Functions.setSRID(geom, 4236);
+      assertEquals("SRID should be set for " + wkt, 4236, result.getSRID());
+      assertTrue("Result should be empty for " + wkt, result.isEmpty());
+    }
+  }
+
   @Test
   public void closestPoint() {
     Point point1 = GEOMETRY_FACTORY.createPoint(new Coordinate(1, 1));

Reply via email to