zongsizhang commented on code in PR #764:
URL: https://github.com/apache/sedona/pull/764#discussion_r1109851123


##########
common/src/test/java/org/apache/sedona/common/FunctionsTest.java:
##########
@@ -238,4 +235,153 @@ public void splitHeterogeneousGeometryCollection() {
 
         assertNull(actualResult);
     }
+
+    private static boolean intersects(Set<?> s1, Set<?> s2) {
+        Set<?> copy = new HashSet<>(s1);
+        copy.retainAll(s2);
+        return !copy.isEmpty();
+    }
+
+    @Test
+    public void getGoogleS2CellIDsPoint() {
+        Point point = GEOMETRY_FACTORY.createPoint(new Coordinate(1, 2));
+        Long[] cid = Functions.s2CellIDs(point, 30);
+        Polygon reversedPolygon = S2Utils.toJTSPolygon(new S2CellId(cid[0]));
+        // cast the cell to a rectangle, it must be able to cover the points
+        assert(reversedPolygon.contains(point));
+    }
+
+    @Test
+    public void getGoogleS2CellIDsPolygon() {
+        // polygon with holes
+        Polygon target = GEOMETRY_FACTORY.createPolygon(
+                GEOMETRY_FACTORY.createLinearRing(coordArray(0.1, 0.1, 0.5, 
0.1, 1.0, 0.3, 1.0, 1.0, 0.1, 1.0, 0.1, 0.1)),
+                new LinearRing[] {
+                        GEOMETRY_FACTORY.createLinearRing(coordArray(0.2, 0.2, 
0.5, 0.2, 0.6, 0.7, 0.2, 0.6, 0.2, 0.2))
+                }
+                );
+        // polygon inside the hole, shouldn't intersect with the polygon
+        Polygon polygonInHole = GEOMETRY_FACTORY.createPolygon(coordArray(0.3, 
0.3, 0.4, 0.3, 0.3, 0.4, 0.3, 0.3));
+        // mbr of the polygon that cover all
+        Geometry mbr = target.getEnvelope();
+        HashSet<Long> targetCells = new 
HashSet<>(Arrays.asList(Functions.s2CellIDs(target, 10)));
+        HashSet<Long> inHoleCells = new 
HashSet<>(Arrays.asList(Functions.s2CellIDs(polygonInHole, 10)));
+        HashSet<Long> mbrCells = new 
HashSet<>(Arrays.asList(Functions.s2CellIDs(mbr, 10)));
+        assert mbrCells.containsAll(targetCells);
+        assert !intersects(targetCells, inHoleCells);
+        assert mbrCells.containsAll(targetCells);
+    }
+
+    @Test
+    public void getGoogleS2CellIDsLineString() {
+        // polygon with holes
+        LineString target = GEOMETRY_FACTORY.createLineString(coordArray(0.2, 
0.2, 0.3, 0.4, 0.4, 0.6));
+        LineString crossLine = 
GEOMETRY_FACTORY.createLineString(coordArray(0.4, 0.1, 0.1, 0.4));
+        // mbr of the polygon that cover all
+        Geometry mbr = target.getEnvelope();
+        // cover the target polygon, and convert cells back to polygons
+        HashSet<Long> targetCells = new 
HashSet<>(Arrays.asList(Functions.s2CellIDs(target, 15)));
+        HashSet<Long> crossCells = new 
HashSet<>(Arrays.asList(Functions.s2CellIDs(crossLine, 15)));
+        HashSet<Long> mbrCells = new 
HashSet<>(Arrays.asList(Functions.s2CellIDs(mbr, 15)));
+        assert intersects(targetCells, crossCells);
+        assert mbrCells.containsAll(targetCells);
+    }
+
+    @Test
+    public void getGoogleS2CellIDsMultiPolygon() {
+        // polygon with holes
+        Polygon[] geoms = new Polygon[] {
+                GEOMETRY_FACTORY.createPolygon(coordArray(0.1, 0.1, 0.5, 0.1, 
0.1, 0.6, 0.1, 0.1)),
+                GEOMETRY_FACTORY.createPolygon(coordArray(0.2, 0.1, 0.6, 0.3, 
0.7, 0.6, 0.2, 0.5, 0.2, 0.1))
+        };
+        MultiPolygon target = GEOMETRY_FACTORY.createMultiPolygon(geoms);
+        Geometry mbr = target.getEnvelope();
+        LinearRing surroundRing = 
GEOMETRY_FACTORY.createLinearRing(coordArray(0.0, 0.0, 0.8, 0.0, 0.8, 0.8, 0.0, 
0.8, 0.0, 0.0));
+        System.out.println(GeomUtils.getWKT(surroundRing));
+        HashSet<Long> targetCells = new 
HashSet<>(Arrays.asList(Functions.s2CellIDs(target, 10)));
+        HashSet<Long> mbrCells = new 
HashSet<>(Arrays.asList(Functions.s2CellIDs(mbr, 10)));
+        HashSet<Long> surroundCells = new 
HashSet<>(Arrays.asList(Functions.s2CellIDs(surroundRing, 10)));
+        assert mbrCells.containsAll(targetCells);
+        assert !intersects(targetCells, surroundCells);
+    }
+
+    @Test
+    public void getGoogleS2CellIDsMultiLineString() {
+        // polygon with holes
+        MultiLineString target = GEOMETRY_FACTORY.createMultiLineString(
+                new LineString[] {
+                        GEOMETRY_FACTORY.createLineString(coordArray(0.1, 0.1, 
0.2, 0.1, 0.3, 0.4, 0.5, 0.9)),
+                        GEOMETRY_FACTORY.createLineString(coordArray(0.5, 0.1, 
0.1, 0.5, 0.3, 0.1))
+                }
+        );
+        Geometry mbr = target.getEnvelope();
+        Point outsidePoint = GEOMETRY_FACTORY.createPoint(new Coordinate(0.3, 
0.7));

Review Comment:
   outsidePoint? it's used at row 321
   ```Long outsideCell = Functions.s2CellIDs(outsidePoint, 10)[0];```



-- 
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: dev-unsubscr...@sedona.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to