Kontinuation commented on code in PR #1992:
URL: https://github.com/apache/sedona/pull/1992#discussion_r2168025360
##########
common/pom.xml:
##########
@@ -81,9 +81,14 @@
<groupId>org.locationtech.spatial4j</groupId>
<artifactId>spatial4j</artifactId>
</dependency>
+<!-- <dependency>-->
+<!-- <groupId>com.google.geometry</groupId>-->
+<!-- <artifactId>s2-geometry</artifactId>-->
+<!-- </dependency>-->
Review Comment:
We can remove this commented dependency, but add some comment to clarify
that `org.datasyslab:s2-geometry-library` is a fork of
`com.google.geometry:s2-geometry-library`, and probably add a link to the issue
[GH-1996](https://github.com/apache/sedona/issues/1996).
##########
common/src/main/java/org/apache/sedona/common/S2Geography/PointGeography.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * 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.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
Review Comment:
```suggestion
out.writeByte(0); // flag
out.writeByte(1); // coveringSize
out.writeByte(0); // reserved
```
##########
common/pom.xml:
##########
@@ -109,6 +114,12 @@
<groupId>edu.ucar</groupId>
<artifactId>cdm-core</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-collections4</artifactId>
+ <version>4.4</version>
+ <scope>compile</scope>
+ </dependency>
Review Comment:
I think we don't need to add this dependency. The newly added code does not
depend on `commons-collections4`.
##########
common/pom.xml:
##########
@@ -177,6 +189,10 @@
<pattern>org.codehaus</pattern>
<shadedPattern>org.apache.sedona.shaded.codehaus</shadedPattern>
</relocation>
+ <relocation>
+
<pattern>com.google.common.geometry;</pattern>
Review Comment:
```suggestion
<pattern>com.google.common.geometry</pattern>
```
##########
common/src/main/java/org/apache/sedona/common/S2Geography/EncodeTag.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * 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 4 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)
+ */
+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.
+ */
Review Comment:
This comment needs to be updated as we have 2 supported flags now.
##########
common/src/main/java/org/apache/sedona/common/S2Geography/PolylineGeography.java:
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.UnsafeInput;
+import com.esotericsoftware.kryo.io.UnsafeOutput;
+import com.google.common.collect.ImmutableList;
+import com.google.common.geometry.*;
+import java.io.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.logging.Logger;
+
+/** A Geography representing zero or more polylines using S2Polyline. */
+public class PolylineGeography extends S2Geography {
+ private static final Logger logger =
Logger.getLogger(PolylineGeography.class.getName());
+
+ private final List<S2Polyline> polylines;
Review Comment:
We define `PolylineGeography` to contain a list of `S2Polyline`s, which is
different from the [C++
version](https://github.com/paleolimbot/s2geography/blob/0.2.0/src/s2geography/geography.h#L179-L203).
I think this is a reasonable change, as it makes distinguishing between
POLYGON and MULTIPOLYGON much easier as long as we ensure that each
`S2Polyline` only contains one polygon. WDYT @paleolimbot
--
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]