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 ab4a2c810d [GH-2258] add MULTIPOLYGON Type in Geography (#2257)
ab4a2c810d is described below

commit ab4a2c810d1520d55865433f87e3b81de70b4eef
Author: Zhuocheng Shang <[email protected]>
AuthorDate: Sun Aug 10 16:16:20 2025 -0700

    [GH-2258] add MULTIPOLYGON Type in Geography (#2257)
    
    * add MULTIPOLYGON type support instead return as Geogcollection type
    
    * format clean
    
    * Apply suggestions from code review
    
    Co-authored-by: Copilot <[email protected]>
    
    * correct null polygon list check
    
    ---------
    
    Co-authored-by: Jia Yu <[email protected]>
    Co-authored-by: Copilot <[email protected]>
---
 .../org/apache/sedona/common/S2Geography/Geography.java |  4 +++-
 .../sedona/common/S2Geography/GeographyCollection.java  | 17 +++++++++++++++++
 .../common/S2Geography/MultiPolygonGeography.java       | 11 ++---------
 .../org/apache/sedona/common/S2Geography/WKBReader.java |  2 +-
 .../org/apache/sedona/common/S2Geography/WKTReader.java |  4 ++--
 .../apache/sedona/common/S2Geography/WKBReaderTest.java |  5 +++++
 6 files changed, 30 insertions(+), 13 deletions(-)

diff --git 
a/common/src/main/java/org/apache/sedona/common/S2Geography/Geography.java 
b/common/src/main/java/org/apache/sedona/common/S2Geography/Geography.java
index 37920aa90a..da2c0a5dbc 100644
--- a/common/src/main/java/org/apache/sedona/common/S2Geography/Geography.java
+++ b/common/src/main/java/org/apache/sedona/common/S2Geography/Geography.java
@@ -66,7 +66,8 @@ public abstract class Geography {
     ENCODED_SHAPE_INDEX(6),
     CELL_CENTER(7),
     SINGLEPOINT(8),
-    SINGLEPOLYLINE(9);
+    SINGLEPOLYLINE(9),
+    MULTIPOLYGON(10);
 
     private final int kind;
 
@@ -243,6 +244,7 @@ public abstract class Geography {
         geo = PolylineGeography.decode(in, tag);
         break;
       case POLYGON:
+      case MULTIPOLYGON:
         geo = PolygonGeography.decode(in, tag);
         break;
       case GEOGRAPHY_COLLECTION:
diff --git 
a/common/src/main/java/org/apache/sedona/common/S2Geography/GeographyCollection.java
 
b/common/src/main/java/org/apache/sedona/common/S2Geography/GeographyCollection.java
index db0aa398e2..d384101c29 100644
--- 
a/common/src/main/java/org/apache/sedona/common/S2Geography/GeographyCollection.java
+++ 
b/common/src/main/java/org/apache/sedona/common/S2Geography/GeographyCollection.java
@@ -25,8 +25,10 @@ import com.google.common.geometry.*;
 import java.io.*;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.logging.Logger;
+import java.util.stream.Collectors;
 
 /** A Geography wrapping zero or more Geography objects, representing a 
GEOMETRYCOLLECTION. */
 public class GeographyCollection extends Geography {
@@ -44,6 +46,21 @@ public class GeographyCollection extends Geography {
     this.totalShapes = 0;
   }
 
+  public GeographyCollection(GeographyKind kind, List<S2Polygon> polygons) {
+    super(kind); // can be MULTIPOLYGON
+    if (kind != GeographyKind.MULTIPOLYGON) {
+      throw new IllegalArgumentException(
+          "Invalid GeographyKind, only allow Multipolygon as in 
geographyCollection: " + kind);
+    }
+    List<S2Polygon> inputPolygons = (polygons == null) ? 
Collections.emptyList() : polygons;
+    // Create the list of features.
+    this.features =
+        
inputPolygons.stream().map(PolygonGeography::new).collect(Collectors.toUnmodifiableList());
+    this.numShapesList = new ArrayList<>();
+    this.totalShapes = 0;
+    countShapes();
+  }
+
   /** Wraps existing Geography features. */
   public GeographyCollection(List<Geography> features) {
     super(GeographyKind.GEOGRAPHY_COLLECTION);
diff --git 
a/common/src/main/java/org/apache/sedona/common/S2Geography/MultiPolygonGeography.java
 
b/common/src/main/java/org/apache/sedona/common/S2Geography/MultiPolygonGeography.java
index 5be5193680..ec71252874 100644
--- 
a/common/src/main/java/org/apache/sedona/common/S2Geography/MultiPolygonGeography.java
+++ 
b/common/src/main/java/org/apache/sedona/common/S2Geography/MultiPolygonGeography.java
@@ -21,21 +21,14 @@ package org.apache.sedona.common.S2Geography;
 import com.google.common.geometry.S2Polygon;
 import java.util.Collections;
 import java.util.List;
-import java.util.stream.Collectors;
 
 public class MultiPolygonGeography extends GeographyCollection {
   /**
    * Wrap each raw S2Polygon in a PolygonGeography, then hand it off to 
GeographyCollection to do
    * the rest (including serialization).
    */
-  public MultiPolygonGeography(List<S2Polygon> polygons) {
-    super(
-        polygons.stream()
-            .map(PolygonGeography::new) // wrap each S2Polygon
-            .collect(Collectors.toList())); // into a List<PolygonGeography>
-    if (polygons.isEmpty()) {
-      new MultiPolygonGeography();
-    }
+  public MultiPolygonGeography(GeographyKind kind, List<S2Polygon> polygons) {
+    super(kind, polygons);
   }
 
   public MultiPolygonGeography() {
diff --git 
a/common/src/main/java/org/apache/sedona/common/S2Geography/WKBReader.java 
b/common/src/main/java/org/apache/sedona/common/S2Geography/WKBReader.java
index 01e76538c8..c26109a1ab 100644
--- a/common/src/main/java/org/apache/sedona/common/S2Geography/WKBReader.java
+++ b/common/src/main/java/org/apache/sedona/common/S2Geography/WKBReader.java
@@ -393,7 +393,7 @@ public class WKBReader {
       org.apache.sedona.common.S2Geography.Geography geom = readGeometry(SRID);
       polygons.add(((PolygonGeography) geom).polygon);
     }
-    return new MultiPolygonGeography(polygons);
+    return new MultiPolygonGeography(Geography.GeographyKind.MULTIPOLYGON, 
polygons);
   }
 
   private GeographyCollection readGeographyCollection(int SRID) throws 
IOException, ParseException {
diff --git 
a/common/src/main/java/org/apache/sedona/common/S2Geography/WKTReader.java 
b/common/src/main/java/org/apache/sedona/common/S2Geography/WKTReader.java
index 63c8f03fde..598832514e 100644
--- a/common/src/main/java/org/apache/sedona/common/S2Geography/WKTReader.java
+++ b/common/src/main/java/org/apache/sedona/common/S2Geography/WKTReader.java
@@ -897,7 +897,7 @@ public class WKTReader {
       throws IOException, ParseException {
     String nextToken = getNextEmptyOrOpener(tokenizer);
     if (nextToken.equals(WKTConstants.EMPTY)) {
-      return new MultiPolygonGeography(new ArrayList<>());
+      return new MultiPolygonGeography(Geography.GeographyKind.MULTIPOLYGON, 
new ArrayList<>());
     }
     List<S2Polygon> polygons = new ArrayList<S2Polygon>();
     do {
@@ -905,7 +905,7 @@ public class WKTReader {
       polygons.add(polygon.polygon);
       nextToken = getNextCloserOrComma(tokenizer);
     } while (nextToken.equals(COMMA));
-    return new MultiPolygonGeography(polygons);
+    return new MultiPolygonGeography(Geography.GeographyKind.MULTIPOLYGON, 
polygons);
   }
 
   /**
diff --git 
a/common/src/test/java/org/apache/sedona/common/S2Geography/WKBReaderTest.java 
b/common/src/test/java/org/apache/sedona/common/S2Geography/WKBReaderTest.java
index 400e835f36..8a7f6166fd 100644
--- 
a/common/src/test/java/org/apache/sedona/common/S2Geography/WKBReaderTest.java
+++ 
b/common/src/test/java/org/apache/sedona/common/S2Geography/WKBReaderTest.java
@@ -364,6 +364,11 @@ public class WKBReaderTest {
         
"0106000020E6100000020000000103000020E61000000200000005000000000000000000000000000000000000000000000000000000000000000000244000000000000024400000000000002440000000000000244000000000000000000000000000000000000000000000000005000000000000000000F03F000000000000F03F000000000000F03F0000000000002240000000000000224000000000000022400000000000002240000000000000F03F000000000000F03F000000000000F03F0103000020E6100000010000000500000000000000000022C0000000000000000000000000000022C00000000000002
 [...]
     String multiPolygonWkt =
         "MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0),(1 1,1 9,9 9,9 1,1 1)),((-9 
0,-9 10,-1 10,-1 0,-9 0)))";
+    WKBReader reader = new WKBReader();
+    Geography geo = reader.read(WKBReader.hexToBytes(hexStr));
+    // Expect a MULTIPOLYGON geometry
+    Assert.assertEquals(Geography.GeographyKind.MULTIPOLYGON, geo.kind);
+
     TestHelper.checkWKBGeography(hexStr, multiPolygonWkt);
   }
 

Reply via email to