Kontinuation commented on code in PR #1992:
URL: https://github.com/apache/sedona/pull/1992#discussion_r2165256446


##########
common/src/main/java/org/apache/sedona/common/S2Geography/PointGeography.java:
##########
@@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sedona.common.S2Geography;
+
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import com.esotericsoftware.kryo.io.UnsafeOutput;
+import com.google.common.geometry.*;
+import com.google.common.geometry.PrimitiveArrays.Bytes;
+import java.io.*;
+import java.util.*;
+import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PointGeography extends S2Geography {
+  private static final Logger logger = 
LoggerFactory.getLogger(PointGeography.class.getName());
+
+  private static final int BUFFER_SIZE = 4 * 1024;
+
+  private final List<S2Point> points = new ArrayList<>();
+
+  /** Constructs an empty PointGeography. */
+  public PointGeography() {
+    super(GeographyKind.POINT);
+  }
+
+  /** Constructs especially for CELL_CENTER */
+  private PointGeography(GeographyKind kind, S2Point point) {
+    super(kind); // can be POINT or CELL_CENTER
+    points.add(point);
+  }
+
+  /** Constructs a single-point geography. */
+  public PointGeography(S2Point point) {
+    this();
+    points.add(point);
+  }
+
+  /** Constructs from a list of points. */
+  public PointGeography(List<S2Point> pts) {
+    this();
+    points.addAll(pts);
+  }
+
+  @Override
+  public int dimension() {
+    return points.isEmpty() ? -1 : 0;
+  }
+
+  @Override
+  public int numShapes() {
+    return points.isEmpty() ? 0 : 1;
+  }
+
+  @Override
+  public S2Shape shape(int id) {
+    return S2Point.Shape.fromList(points);
+  }
+
+  @Override
+  public S2Region region() {
+    if (points.isEmpty()) {
+      return S2Cap.empty();
+    } else if (points.size() == 1) {
+      return new S2PointRegion(points.get(0));
+    } else {
+      // Union of all point regions
+      Collection<S2Region> pointRegionCollection = new ArrayList<>();
+      for (S2Point p : points) {
+        pointRegionCollection.add(new S2PointRegion(p));
+      }
+      return new S2RegionUnion(pointRegionCollection);
+    }
+  }
+
+  @Override
+  public void getCellUnionBound(List<S2CellId> cellIds) {
+    if (points.size() < 10) {
+      // For small point sets, cover each point individually
+      for (S2Point p : points) {
+        cellIds.add(S2CellId.fromPoint(p));
+      }
+    } else {
+      // Fallback to the default covering logic in S2Geography
+      super.getCellUnionBound(cellIds);
+    }
+  }
+
+  /** Returns an immutable view of the points. */
+  public List<S2Point> getPoints() {
+    // List.copyOf makes an unmodifiable copy under the hood
+    return List.copyOf(points);
+  }
+
+  // -------------------------------------------------------
+  // EncodeTagged / DecodeTagged
+  // -------------------------------------------------------
+
+  @Override
+  public void encodeTagged(OutputStream os, EncodeOptions opts) throws 
IOException {
+    UnsafeOutput out = new UnsafeOutput(os, BUFFER_SIZE);
+    if (points.size() == 1 && opts.getCodingHint() == 
EncodeOptions.CodingHint.COMPACT) {
+      // Optimized encoding which only uses covering to represent the point
+      S2CellId cid = S2CellId.fromPoint(points.get(0));
+      // Only encode this for very high levels: because the covering *is* the
+      // representation, we will have a very loose covering if the level is 
low.
+      // Level 23 has a cell size of ~1 meter
+      // (http://s2geometry.io/resources/s2cell_statistics)
+      if (cid.level() >= 23) {
+        out.writeByte(GeographyKind.CELL_CENTER.getKind());
+        out.writeByte(0); // POINT kind
+        out.writeByte(1); // flag
+        out.writeByte(0); // coveringSize
+        out.writeByte(2); // COMPACT encode type
+        out.writeLong(cid.id());
+        out.flush();
+        return;
+      }
+      super.encodeTagged(os, opts); // Not exactly encodable as a cell center
+    }
+    // In other cases, fallback to the default encodeTagged implementation:
+    super.encodeTagged(os, opts);

Review Comment:
   `super.encodeTagged` may be called twice. We can remove the 
super.encodeTagged call on line 136.



##########
common/src/main/java/org/apache/sedona/common/S2Geography/PointGeography.java:
##########
@@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sedona.common.S2Geography;
+
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import com.esotericsoftware.kryo.io.UnsafeOutput;
+import com.google.common.geometry.*;
+import com.google.common.geometry.PrimitiveArrays.Bytes;
+import java.io.*;
+import java.util.*;
+import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PointGeography extends S2Geography {
+  private static final Logger logger = 
LoggerFactory.getLogger(PointGeography.class.getName());
+
+  private static final int BUFFER_SIZE = 4 * 1024;
+
+  private final List<S2Point> points = new ArrayList<>();
+
+  /** Constructs an empty PointGeography. */
+  public PointGeography() {
+    super(GeographyKind.POINT);
+  }
+
+  /** Constructs especially for CELL_CENTER */
+  private PointGeography(GeographyKind kind, S2Point point) {
+    super(kind); // can be POINT or CELL_CENTER
+    points.add(point);
+  }
+
+  /** Constructs a single-point geography. */
+  public PointGeography(S2Point point) {
+    this();
+    points.add(point);
+  }
+
+  /** Constructs from a list of points. */
+  public PointGeography(List<S2Point> pts) {
+    this();
+    points.addAll(pts);
+  }
+
+  @Override
+  public int dimension() {
+    return points.isEmpty() ? -1 : 0;
+  }
+
+  @Override
+  public int numShapes() {
+    return points.isEmpty() ? 0 : 1;
+  }
+
+  @Override
+  public S2Shape shape(int id) {
+    return S2Point.Shape.fromList(points);
+  }
+
+  @Override
+  public S2Region region() {
+    if (points.isEmpty()) {
+      return S2Cap.empty();
+    } else if (points.size() == 1) {
+      return new S2PointRegion(points.get(0));
+    } else {
+      // Union of all point regions
+      Collection<S2Region> pointRegionCollection = new ArrayList<>();
+      for (S2Point p : points) {
+        pointRegionCollection.add(new S2PointRegion(p));
+      }
+      return new S2RegionUnion(pointRegionCollection);
+    }
+  }
+
+  @Override
+  public void getCellUnionBound(List<S2CellId> cellIds) {
+    if (points.size() < 10) {
+      // For small point sets, cover each point individually
+      for (S2Point p : points) {
+        cellIds.add(S2CellId.fromPoint(p));
+      }
+    } else {
+      // Fallback to the default covering logic in S2Geography
+      super.getCellUnionBound(cellIds);
+    }
+  }
+
+  /** Returns an immutable view of the points. */
+  public List<S2Point> getPoints() {
+    // List.copyOf makes an unmodifiable copy under the hood
+    return List.copyOf(points);
+  }
+
+  // -------------------------------------------------------
+  // EncodeTagged / DecodeTagged
+  // -------------------------------------------------------
+
+  @Override
+  public void encodeTagged(OutputStream os, EncodeOptions opts) throws 
IOException {
+    UnsafeOutput out = new UnsafeOutput(os, BUFFER_SIZE);
+    if (points.size() == 1 && opts.getCodingHint() == 
EncodeOptions.CodingHint.COMPACT) {
+      // Optimized encoding which only uses covering to represent the point
+      S2CellId cid = S2CellId.fromPoint(points.get(0));
+      // Only encode this for very high levels: because the covering *is* the
+      // representation, we will have a very loose covering if the level is 
low.
+      // Level 23 has a cell size of ~1 meter
+      // (http://s2geometry.io/resources/s2cell_statistics)
+      if (cid.level() >= 23) {
+        out.writeByte(GeographyKind.CELL_CENTER.getKind());
+        out.writeByte(0); // POINT kind
+        out.writeByte(1); // flag
+        out.writeByte(0); // coveringSize
+        out.writeByte(2); // COMPACT encode type
+        out.writeLong(cid.id());
+        out.flush();
+        return;
+      }
+      super.encodeTagged(os, opts); // Not exactly encodable as a cell center
+    }
+    // In other cases, fallback to the default encodeTagged implementation:
+    super.encodeTagged(os, opts);
+  }
+
+  @Override
+  protected void encode(Output out, EncodeOptions opts) throws IOException {
+    // Encode point payload using selected hint
+    S2Point.Shape shp = S2Point.Shape.fromList(points);
+    switch (opts.getCodingHint()) {
+      case FAST:
+        S2Point.Shape.FAST_CODER.encode(shp, out);
+        break;
+      case COMPACT:
+        S2Point.Shape.COMPACT_CODER.encode(shp, out);
+    }
+  }
+
+  public static PointGeography decode(Input in, EncodeTag tag) throws 
IOException {
+    PointGeography geo = new PointGeography();
+
+    // EMPTY
+    if ((tag.getFlags() & EncodeTag.FLAG_EMPTY) != 0) {
+      logger.warn("Decoded empty PointGeography.");
+      return geo;
+    }
+
+    // Optimized 1-point COMPACT situation
+    if (tag.getKind() == GeographyKind.CELL_CENTER) {
+      long id = in.readLong();
+      geo = new PointGeography(new S2CellId(id).toPoint());
+      logger.info("Decoded compact single-point geography via cell center.");
+      return geo;
+    }
+
+    // skip cover
+    tag.skipCovering(in);
+
+    // Grab Kryo’s backing buffer & bounds
+    Input kryoIn = (Input) in;
+    final byte[] backing = kryoIn.getBuffer();
+    final int start = kryoIn.position();
+    final int end = kryoIn.limit();
+    final long length = (long) end - start; // fits in an int normally
+
+    // Zero-copy Bytes view
+    Bytes bytes =
+        new Bytes() {
+          @Override
+          public long length() {
+            return length;
+          }
+
+          @Override
+          public byte get(long idx) {
+            if (idx < 0 || idx >= length) {
+              throw new IndexOutOfBoundsException(idx + " not in [0," + length 
+ ")");
+            }
+            // safe to cast to int because length <= backing.length
+            return backing[start + (int) idx];
+          }
+        };
+
+    PrimitiveArrays.Cursor cursor = bytes.cursor();
+    List<S2Point> points;
+    switch (tag.getEncodeType()) {
+      case 1:
+        points = S2Point.Shape.FAST_CODER.decode(bytes, cursor);
+        break;
+      case 2:
+        points = S2Point.Shape.COMPACT_CODER.decode(bytes, cursor);
+        break;
+      default:
+        throw new IllegalArgumentException("Unknown coding hint");
+    }

Review Comment:
   The internal buffer of UnsafeInput may not contain the entire encoded point 
list. This will be problematic when we have a PointGeography with lots of 
points.
   
   The S2 Coder interface of Java makes it hard to decode data using streams, 
we can write an integer indicating the total length of the encoded point before 
the actual payload in `encode`. We can read the length and read the entire 
payload into a byte array, then call the decode function of S2 Coder. This 
results in in-compatible encoding format with the C++ implementation, but we 
can do this for now until we need to exchange data with some native components.



##########
common/src/main/java/org/apache/sedona/common/S2Geography/PointGeography.java:
##########
@@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sedona.common.S2Geography;
+
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import com.esotericsoftware.kryo.io.UnsafeOutput;
+import com.google.common.geometry.*;
+import com.google.common.geometry.PrimitiveArrays.Bytes;
+import java.io.*;
+import java.util.*;
+import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PointGeography extends S2Geography {
+  private static final Logger logger = 
LoggerFactory.getLogger(PointGeography.class.getName());
+
+  private static final int BUFFER_SIZE = 4 * 1024;
+
+  private final List<S2Point> points = new ArrayList<>();
+
+  /** Constructs an empty PointGeography. */
+  public PointGeography() {
+    super(GeographyKind.POINT);
+  }
+
+  /** Constructs especially for CELL_CENTER */
+  private PointGeography(GeographyKind kind, S2Point point) {
+    super(kind); // can be POINT or CELL_CENTER
+    points.add(point);
+  }
+
+  /** Constructs a single-point geography. */
+  public PointGeography(S2Point point) {
+    this();
+    points.add(point);
+  }
+
+  /** Constructs from a list of points. */
+  public PointGeography(List<S2Point> pts) {
+    this();
+    points.addAll(pts);
+  }
+
+  @Override
+  public int dimension() {
+    return points.isEmpty() ? -1 : 0;
+  }
+
+  @Override
+  public int numShapes() {
+    return points.isEmpty() ? 0 : 1;
+  }
+
+  @Override
+  public S2Shape shape(int id) {
+    return S2Point.Shape.fromList(points);
+  }
+
+  @Override
+  public S2Region region() {
+    if (points.isEmpty()) {
+      return S2Cap.empty();
+    } else if (points.size() == 1) {
+      return new S2PointRegion(points.get(0));
+    } else {
+      // Union of all point regions
+      Collection<S2Region> pointRegionCollection = new ArrayList<>();
+      for (S2Point p : points) {
+        pointRegionCollection.add(new S2PointRegion(p));
+      }
+      return new S2RegionUnion(pointRegionCollection);
+    }
+  }
+
+  @Override
+  public void getCellUnionBound(List<S2CellId> cellIds) {
+    if (points.size() < 10) {
+      // For small point sets, cover each point individually
+      for (S2Point p : points) {
+        cellIds.add(S2CellId.fromPoint(p));
+      }
+    } else {
+      // Fallback to the default covering logic in S2Geography
+      super.getCellUnionBound(cellIds);
+    }
+  }
+
+  /** Returns an immutable view of the points. */
+  public List<S2Point> getPoints() {
+    // List.copyOf makes an unmodifiable copy under the hood
+    return List.copyOf(points);
+  }
+
+  // -------------------------------------------------------
+  // EncodeTagged / DecodeTagged
+  // -------------------------------------------------------
+
+  @Override
+  public void encodeTagged(OutputStream os, EncodeOptions opts) throws 
IOException {
+    UnsafeOutput out = new UnsafeOutput(os, BUFFER_SIZE);
+    if (points.size() == 1 && opts.getCodingHint() == 
EncodeOptions.CodingHint.COMPACT) {
+      // Optimized encoding which only uses covering to represent the point
+      S2CellId cid = S2CellId.fromPoint(points.get(0));
+      // Only encode this for very high levels: because the covering *is* the
+      // representation, we will have a very loose covering if the level is 
low.
+      // Level 23 has a cell size of ~1 meter
+      // (http://s2geometry.io/resources/s2cell_statistics)
+      if (cid.level() >= 23) {
+        out.writeByte(GeographyKind.CELL_CENTER.getKind());
+        out.writeByte(0); // POINT kind
+        out.writeByte(1); // flag
+        out.writeByte(0); // coveringSize
+        out.writeByte(2); // COMPACT encode type
+        out.writeLong(cid.id());
+        out.flush();
+        return;
+      }
+      super.encodeTagged(os, opts); // Not exactly encodable as a cell center
+    }
+    // In other cases, fallback to the default encodeTagged implementation:
+    super.encodeTagged(os, opts);
+  }
+
+  @Override
+  protected void encode(Output out, EncodeOptions opts) throws IOException {
+    // Encode point payload using selected hint
+    S2Point.Shape shp = S2Point.Shape.fromList(points);
+    switch (opts.getCodingHint()) {
+      case FAST:
+        S2Point.Shape.FAST_CODER.encode(shp, out);
+        break;
+      case COMPACT:
+        S2Point.Shape.COMPACT_CODER.encode(shp, out);
+    }
+  }
+
+  public static PointGeography decode(Input in, EncodeTag tag) throws 
IOException {
+    PointGeography geo = new PointGeography();
+
+    // EMPTY
+    if ((tag.getFlags() & EncodeTag.FLAG_EMPTY) != 0) {
+      logger.warn("Decoded empty PointGeography.");
+      return geo;
+    }
+
+    // Optimized 1-point COMPACT situation
+    if (tag.getKind() == GeographyKind.CELL_CENTER) {
+      long id = in.readLong();
+      geo = new PointGeography(new S2CellId(id).toPoint());
+      logger.info("Decoded compact single-point geography via cell center.");
+      return geo;
+    }
+
+    // skip cover
+    tag.skipCovering(in);
+
+    // Grab Kryo’s backing buffer & bounds
+    Input kryoIn = (Input) in;
+    final byte[] backing = kryoIn.getBuffer();
+    final int start = kryoIn.position();
+    final int end = kryoIn.limit();
+    final long length = (long) end - start; // fits in an int normally
+
+    // Zero-copy Bytes view
+    Bytes bytes =
+        new Bytes() {
+          @Override
+          public long length() {
+            return length;
+          }
+
+          @Override
+          public byte get(long idx) {
+            if (idx < 0 || idx >= length) {
+              throw new IndexOutOfBoundsException(idx + " not in [0," + length 
+ ")");
+            }
+            // safe to cast to int because length <= backing.length
+            return backing[start + (int) idx];
+          }
+        };
+
+    PrimitiveArrays.Cursor cursor = bytes.cursor();
+    List<S2Point> points;
+    switch (tag.getEncodeType()) {
+      case 1:
+        points = S2Point.Shape.FAST_CODER.decode(bytes, cursor);
+        break;
+      case 2:
+        points = S2Point.Shape.COMPACT_CODER.decode(bytes, cursor);
+        break;

Review Comment:
   Note:
   
   Actually we don't need to distinguish between FAST_CODER and COMPACT_CODER 
when decoding. The payload already has the encoding in the first byte, we can 
always use FAST_CODER and it will decode the payload correctly regardless of 
the coding type of the payload.
   
   We can omit the `encodeType` field in `EncodeTag` to make `EncodeTag` 
consistent with the C++ implementation, this will help a lot when we decide to 
exchange encoded data with some native components in the future.
   
   I'm not sure if we can do the same thing when decoding shape index, so we'd 
better leave it as is for now.



##########
common/src/main/java/org/apache/sedona/common/S2Geography/PointGeography.java:
##########
@@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sedona.common.S2Geography;
+
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import com.esotericsoftware.kryo.io.UnsafeOutput;
+import com.google.common.geometry.*;
+import com.google.common.geometry.PrimitiveArrays.Bytes;
+import java.io.*;
+import java.util.*;
+import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PointGeography extends S2Geography {
+  private static final Logger logger = 
LoggerFactory.getLogger(PointGeography.class.getName());
+
+  private static final int BUFFER_SIZE = 4 * 1024;
+
+  private final List<S2Point> points = new ArrayList<>();
+
+  /** Constructs an empty PointGeography. */
+  public PointGeography() {
+    super(GeographyKind.POINT);
+  }
+
+  /** Constructs especially for CELL_CENTER */
+  private PointGeography(GeographyKind kind, S2Point point) {
+    super(kind); // can be POINT or CELL_CENTER
+    points.add(point);
+  }
+
+  /** Constructs a single-point geography. */
+  public PointGeography(S2Point point) {
+    this();
+    points.add(point);
+  }
+
+  /** Constructs from a list of points. */
+  public PointGeography(List<S2Point> pts) {
+    this();
+    points.addAll(pts);
+  }
+
+  @Override
+  public int dimension() {
+    return points.isEmpty() ? -1 : 0;
+  }
+
+  @Override
+  public int numShapes() {
+    return points.isEmpty() ? 0 : 1;
+  }
+
+  @Override
+  public S2Shape shape(int id) {
+    return S2Point.Shape.fromList(points);
+  }
+
+  @Override
+  public S2Region region() {
+    if (points.isEmpty()) {
+      return S2Cap.empty();
+    } else if (points.size() == 1) {
+      return new S2PointRegion(points.get(0));
+    } else {
+      // Union of all point regions
+      Collection<S2Region> pointRegionCollection = new ArrayList<>();
+      for (S2Point p : points) {
+        pointRegionCollection.add(new S2PointRegion(p));
+      }
+      return new S2RegionUnion(pointRegionCollection);
+    }
+  }
+
+  @Override
+  public void getCellUnionBound(List<S2CellId> cellIds) {
+    if (points.size() < 10) {
+      // For small point sets, cover each point individually
+      for (S2Point p : points) {
+        cellIds.add(S2CellId.fromPoint(p));
+      }
+    } else {
+      // Fallback to the default covering logic in S2Geography
+      super.getCellUnionBound(cellIds);
+    }
+  }
+
+  /** Returns an immutable view of the points. */
+  public List<S2Point> getPoints() {
+    // List.copyOf makes an unmodifiable copy under the hood
+    return List.copyOf(points);
+  }
+
+  // -------------------------------------------------------
+  // EncodeTagged / DecodeTagged
+  // -------------------------------------------------------
+
+  @Override
+  public void encodeTagged(OutputStream os, EncodeOptions opts) throws 
IOException {
+    UnsafeOutput out = new UnsafeOutput(os, BUFFER_SIZE);
+    if (points.size() == 1 && opts.getCodingHint() == 
EncodeOptions.CodingHint.COMPACT) {
+      // Optimized encoding which only uses covering to represent the point
+      S2CellId cid = S2CellId.fromPoint(points.get(0));
+      // Only encode this for very high levels: because the covering *is* the
+      // representation, we will have a very loose covering if the level is 
low.
+      // Level 23 has a cell size of ~1 meter
+      // (http://s2geometry.io/resources/s2cell_statistics)
+      if (cid.level() >= 23) {
+        out.writeByte(GeographyKind.CELL_CENTER.getKind());
+        out.writeByte(0); // POINT kind
+        out.writeByte(1); // flag
+        out.writeByte(0); // coveringSize
+        out.writeByte(2); // COMPACT encode type
+        out.writeLong(cid.id());

Review Comment:
   This looks strange to me. The bytes written here does not conform to the 
format of `EncodeTag`. We should write an EncodeTag header with kind = 
CELL_CENTER, flag = 0, coveringSize = 1, reserved = 0.
   
   You can construct a new EncodeTag object and set the fields to correct 
values, then call `tag.encode(out)`. This will ensure that we always write the 
encode tag header in consistent format.



##########
common/src/main/java/org/apache/sedona/common/S2Geography/EncodeTag.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sedona.common.S2Geography;
+
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import com.esotericsoftware.kryo.io.UnsafeInput;
+import com.google.common.geometry.S2CellId;
+import java.io.*;
+import java.util.List;
+import org.apache.sedona.common.S2Geography.S2Geography.GeographyKind;
+
+/**
+ * A 5 byte prefix for encoded geographies. Builds a 5-byte header (EncodeTag) 
containing 1 byte:
+ * kind 1 byte: flags 1 byte: coveringSize 1 byte: reserved (must be 0) 1 
byte: encodeType (fast vs.
+ * compact)
+ */
+public class EncodeTag {
+  /**
+   * Subclass of S2Geography whose decode() method will be invoked. Encoded 
using a single unsigned
+   * byte (represented as an int in Java, range 0–255).
+   */
+  private GeographyKind kind = GeographyKind.UNINITIALIZED;
+  /**
+   * Flags for encoding metadata. Currently, only {@code kFlagEmpty} is 
supported, which is set if
+   * and only if the geography contains zero shapes.
+   */
+  private byte flags = 0;
+  /**
+   * Number of S2CellId entries that follow this tag. A value of zero (i.e., 
an empty covering)
+   * means no covering was written, but this does not imply that the geography 
itself is empty.
+   */
+  private byte coveringSize = 0;
+  /** Reserved byte for future use. Must be set to 0. */
+  private byte reserved = 0;
+
+  /** If set, geography has zero shapes. */
+  public static final byte FLAG_EMPTY = 1;
+
+  private byte encodeType = 1; // fast: 1 ; compact: 2
+
+  public EncodeTag() {}
+
+  public EncodeTag(EncodeOptions opts) {
+    this.encodeType = (byte) (opts.getCodingHint() == 
EncodeOptions.CodingHint.FAST ? 1 : 2);
+  }
+
+  // ——— Write the 4-byte tag header ——————————————————————————————————————
+
+  /** Write exactly 4 bytes: [kind|flags|coveringSize|reserved]. */
+  public void encode(Output out) throws IOException {

Review Comment:
   Now our header is 5 bytes. I suggest we remove the reserved byte to keep it 
aligning to 4-bytes boundary.



##########
common/src/main/java/org/apache/sedona/common/S2Geography/PointGeography.java:
##########
@@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sedona.common.S2Geography;
+
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import com.esotericsoftware.kryo.io.UnsafeOutput;
+import com.google.common.geometry.*;
+import com.google.common.geometry.PrimitiveArrays.Bytes;
+import java.io.*;
+import java.util.*;
+import java.util.List;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PointGeography extends S2Geography {
+  private static final Logger logger = 
LoggerFactory.getLogger(PointGeography.class.getName());
+
+  private static final int BUFFER_SIZE = 4 * 1024;
+
+  private final List<S2Point> points = new ArrayList<>();
+
+  /** Constructs an empty PointGeography. */
+  public PointGeography() {
+    super(GeographyKind.POINT);
+  }
+
+  /** Constructs especially for CELL_CENTER */
+  private PointGeography(GeographyKind kind, S2Point point) {
+    super(kind); // can be POINT or CELL_CENTER
+    points.add(point);
+  }
+
+  /** Constructs a single-point geography. */
+  public PointGeography(S2Point point) {
+    this();
+    points.add(point);
+  }
+
+  /** Constructs from a list of points. */
+  public PointGeography(List<S2Point> pts) {
+    this();
+    points.addAll(pts);
+  }
+
+  @Override
+  public int dimension() {
+    return points.isEmpty() ? -1 : 0;
+  }
+
+  @Override
+  public int numShapes() {
+    return points.isEmpty() ? 0 : 1;
+  }
+
+  @Override
+  public S2Shape shape(int id) {
+    return S2Point.Shape.fromList(points);
+  }
+
+  @Override
+  public S2Region region() {
+    if (points.isEmpty()) {
+      return S2Cap.empty();
+    } else if (points.size() == 1) {
+      return new S2PointRegion(points.get(0));
+    } else {
+      // Union of all point regions
+      Collection<S2Region> pointRegionCollection = new ArrayList<>();
+      for (S2Point p : points) {
+        pointRegionCollection.add(new S2PointRegion(p));
+      }
+      return new S2RegionUnion(pointRegionCollection);
+    }
+  }
+
+  @Override
+  public void getCellUnionBound(List<S2CellId> cellIds) {
+    if (points.size() < 10) {
+      // For small point sets, cover each point individually
+      for (S2Point p : points) {
+        cellIds.add(S2CellId.fromPoint(p));
+      }
+    } else {
+      // Fallback to the default covering logic in S2Geography
+      super.getCellUnionBound(cellIds);
+    }
+  }
+
+  /** Returns an immutable view of the points. */
+  public List<S2Point> getPoints() {
+    // List.copyOf makes an unmodifiable copy under the hood
+    return List.copyOf(points);
+  }
+
+  // -------------------------------------------------------
+  // EncodeTagged / DecodeTagged
+  // -------------------------------------------------------
+
+  @Override
+  public void encodeTagged(OutputStream os, EncodeOptions opts) throws 
IOException {
+    UnsafeOutput out = new UnsafeOutput(os, BUFFER_SIZE);
+    if (points.size() == 1 && opts.getCodingHint() == 
EncodeOptions.CodingHint.COMPACT) {
+      // Optimized encoding which only uses covering to represent the point
+      S2CellId cid = S2CellId.fromPoint(points.get(0));
+      // Only encode this for very high levels: because the covering *is* the
+      // representation, we will have a very loose covering if the level is 
low.
+      // Level 23 has a cell size of ~1 meter
+      // (http://s2geometry.io/resources/s2cell_statistics)
+      if (cid.level() >= 23) {
+        out.writeByte(GeographyKind.CELL_CENTER.getKind());
+        out.writeByte(0); // POINT kind
+        out.writeByte(1); // flag
+        out.writeByte(0); // coveringSize
+        out.writeByte(2); // COMPACT encode type
+        out.writeLong(cid.id());
+        out.flush();
+        return;
+      }
+      super.encodeTagged(os, opts); // Not exactly encodable as a cell center
+    }
+    // In other cases, fallback to the default encodeTagged implementation:
+    super.encodeTagged(os, opts);
+  }
+
+  @Override
+  protected void encode(Output out, EncodeOptions opts) throws IOException {
+    // Encode point payload using selected hint
+    S2Point.Shape shp = S2Point.Shape.fromList(points);
+    switch (opts.getCodingHint()) {
+      case FAST:
+        S2Point.Shape.FAST_CODER.encode(shp, out);
+        break;
+      case COMPACT:
+        S2Point.Shape.COMPACT_CODER.encode(shp, out);
+    }
+  }
+
+  public static PointGeography decode(Input in, EncodeTag tag) throws 
IOException {

Review Comment:
   I suggest that we change the input parameter type of `decode` methods from 
`Input` to `UnsafeInput`, and also change `skipCovering` and `decodeCovering` 
methods of `EncodeTag` to take `UnsafeInput` as argument. This is because 
UnsafeInput may over-consume the wrapped input stream, we'd better only use one 
UnsafeInput object throughout the decoding of an object.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to