|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.sedona.common.S2Geography; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertFalse; |
| 22 | +import static org.junit.Assert.assertTrue; |
| 23 | + |
| 24 | +import org.apache.sedona.common.geography.Constructors; |
| 25 | +import org.apache.sedona.common.geography.Functions; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +/** |
| 29 | + * Localises a Geography ST_Contains correctness bug seen when arguments come from a DataFrame (i.e. |
| 30 | + * after a GeographyWKBSerializer round-trip). The WKBGeography fast path (getShapeIndexGeography → |
| 31 | + * WkbS2Shape) returns the wrong answer for some polygon-point pairs; the slow path through |
| 32 | + * S2Polygon (and direct ST_GeogFromWKT in a SELECT) is correct. |
| 33 | + */ |
| 34 | +public class WkbContainsRoundtripTest { |
| 35 | + |
| 36 | + /** |
| 37 | + * Direct Functions.contains call, no round-trip. Should return false: (10, 10) is far outside the |
| 38 | + * small polygon at (2..3, 2..3). |
| 39 | + */ |
| 40 | + @Test |
| 41 | + public void containsIsFalseWithoutRoundTrip() throws Exception { |
| 42 | + Geography poly = Constructors.geogFromWKT("POLYGON((2 2, 3 2, 3 3, 2 3, 2 2))", 4326); |
| 43 | + Geography pt = Constructors.geogFromWKT("POINT(10 10)", 4326); |
| 44 | + assertFalse(Functions.contains(poly, pt)); |
| 45 | + assertTrue(Functions.contains(poly, Constructors.geogFromWKT("POINT(2.5 2.5)", 4326))); |
| 46 | + } |
| 47 | + |
| 48 | + /** Control test mirroring GeographyFunctionTest's "ST_Contains point outside polygon". */ |
| 49 | + @Test |
| 50 | + public void controlPolygonAtOrigin() throws Exception { |
| 51 | + Geography poly = Constructors.geogFromWKT("POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))", 4326); |
| 52 | + Geography ptOutside = Constructors.geogFromWKT("POINT(2 2)", 4326); |
| 53 | + Geography ptInside = Constructors.geogFromWKT("POINT(0.5 0.5)", 4326); |
| 54 | + assertFalse("polygon at origin must NOT contain (2, 2)", Functions.contains(poly, ptOutside)); |
| 55 | + assertTrue("polygon at origin must contain (0.5, 0.5)", Functions.contains(poly, ptInside)); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Bypass WkbS2Shape and feed the polygon through PolygonGeography directly. If this passes while |
| 60 | + * the equivalent WKBGeography case fails, the bug is localised to WkbS2Shape (or to the |
| 61 | + * `result.shapeIndex.add(new WkbS2Shape(...))` path in WKBGeography.getShapeIndexGeography). |
| 62 | + */ |
| 63 | + @Test |
| 64 | + public void bypassWkbS2ShapeViaPolygonGeography() throws Exception { |
| 65 | + // Force the slow path: parse via WKTReader then DON'T wrap in WKBGeography. |
| 66 | + Geography poly = new WKTReader().read("POLYGON((2 2, 3 2, 3 3, 2 3, 2 2))"); |
| 67 | + poly.setSRID(4326); |
| 68 | + Geography ptOutside = new WKTReader().read("POINT(10 10)"); |
| 69 | + ptOutside.setSRID(4326); |
| 70 | + Geography ptInside = new WKTReader().read("POINT(2.5 2.5)"); |
| 71 | + ptInside.setSRID(4326); |
| 72 | + assertFalse( |
| 73 | + "[slow path] polygon at (2..3,2..3) must NOT contain (10, 10)", |
| 74 | + Functions.contains(poly, ptOutside)); |
| 75 | + assertTrue( |
| 76 | + "[slow path] polygon at (2..3,2..3) must contain (2.5, 2.5)", |
| 77 | + Functions.contains(poly, ptInside)); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Same logical inputs, but each Geography goes through the WKB serializer round-trip first — |
| 82 | + * which is what happens whenever a GeographyUDT column is read back from a DataFrame. |
| 83 | + */ |
| 84 | + @Test |
| 85 | + public void containsIsFalseAfterWkbRoundTrip() throws Exception { |
| 86 | + Geography poly = |
| 87 | + GeographyWKBSerializer.deserialize( |
| 88 | + GeographyWKBSerializer.serialize( |
| 89 | + Constructors.geogFromWKT("POLYGON((2 2, 3 2, 3 3, 2 3, 2 2))", 4326))); |
| 90 | + Geography ptOutside = |
| 91 | + GeographyWKBSerializer.deserialize( |
| 92 | + GeographyWKBSerializer.serialize(Constructors.geogFromWKT("POINT(10 10)", 4326))); |
| 93 | + Geography ptInside = |
| 94 | + GeographyWKBSerializer.deserialize( |
| 95 | + GeographyWKBSerializer.serialize(Constructors.geogFromWKT("POINT(2.5 2.5)", 4326))); |
| 96 | + assertFalse( |
| 97 | + "polygon at (2..3,2..3) must NOT contain (10, 10)", Functions.contains(poly, ptOutside)); |
| 98 | + assertTrue( |
| 99 | + "polygon at (2..3,2..3) must contain (2.5, 2.5)", Functions.contains(poly, ptInside)); |
| 100 | + } |
| 101 | +} |
0 commit comments