|
| 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.flink; |
| 20 | + |
| 21 | +import static org.apache.flink.table.api.Expressions.$; |
| 22 | +import static org.apache.flink.table.api.Expressions.call; |
| 23 | +import static org.apache.flink.table.api.Expressions.lit; |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | +import static org.junit.Assert.assertFalse; |
| 26 | +import static org.junit.Assert.assertTrue; |
| 27 | + |
| 28 | +import java.util.Collections; |
| 29 | +import org.apache.flink.api.common.typeinfo.BasicTypeInfo; |
| 30 | +import org.apache.flink.api.common.typeinfo.TypeInformation; |
| 31 | +import org.apache.flink.api.java.typeutils.RowTypeInfo; |
| 32 | +import org.apache.flink.streaming.api.datastream.DataStream; |
| 33 | +import org.apache.flink.table.api.ApiExpression; |
| 34 | +import org.apache.flink.table.api.Table; |
| 35 | +import org.apache.flink.types.Row; |
| 36 | +import org.apache.sedona.flink.expressions.Predicates; |
| 37 | +import org.apache.sedona.flink.expressions.geography.GeographyConstructors; |
| 38 | +import org.junit.BeforeClass; |
| 39 | +import org.junit.Test; |
| 40 | + |
| 41 | +public class GeographyPredicateTest extends TestBase { |
| 42 | + |
| 43 | + // Geography polygons follow the spherical right-hand rule: a CCW ring's interior is the enclosed |
| 44 | + // region. The polygons below are CCW so containment matches the intuitive (planar) expectation; a |
| 45 | + // CW ring would denote the complementary region on the sphere. |
| 46 | + |
| 47 | + @BeforeClass |
| 48 | + public static void onceExecutedBeforeAll() { |
| 49 | + initialize(); |
| 50 | + } |
| 51 | + |
| 52 | + /** Evaluate a predicate call over two geography columns built from {@code wktA}/{@code wktB}. */ |
| 53 | + private Object eval(String wktA, String wktB, PredicateFactory factory) { |
| 54 | + RowTypeInfo ti = |
| 55 | + new RowTypeInfo( |
| 56 | + new TypeInformation<?>[] { |
| 57 | + BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO |
| 58 | + }, |
| 59 | + new String[] {"a", "b"}); |
| 60 | + DataStream<Row> ds = |
| 61 | + env.fromCollection(Collections.singletonList(Row.of(wktA, wktB))).returns(ti); |
| 62 | + Table t = |
| 63 | + tableEnv |
| 64 | + .fromDataStream(ds) |
| 65 | + .select( |
| 66 | + call(GeographyConstructors.ST_GeogFromWKT.class.getSimpleName(), $("a"), lit(4326)) |
| 67 | + .as("ga"), |
| 68 | + call(GeographyConstructors.ST_GeogFromWKT.class.getSimpleName(), $("b"), lit(4326)) |
| 69 | + .as("gb")); |
| 70 | + return first(t.select(factory.build($("ga"), $("gb")).as("o"))).getFieldAs("o"); |
| 71 | + } |
| 72 | + |
| 73 | + private interface PredicateFactory { |
| 74 | + ApiExpression build(ApiExpression a, ApiExpression b); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testIntersects() { |
| 79 | + String poly = "POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))"; |
| 80 | + assertTrue( |
| 81 | + (Boolean) |
| 82 | + eval( |
| 83 | + poly, |
| 84 | + "POINT (1 1)", |
| 85 | + (a, b) -> call(Predicates.ST_Intersects.class.getSimpleName(), a, b))); |
| 86 | + assertFalse( |
| 87 | + (Boolean) |
| 88 | + eval( |
| 89 | + poly, |
| 90 | + "POINT (5 5)", |
| 91 | + (a, b) -> call(Predicates.ST_Intersects.class.getSimpleName(), a, b))); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testContains() { |
| 96 | + assertTrue( |
| 97 | + (Boolean) |
| 98 | + eval( |
| 99 | + "POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))", |
| 100 | + "POINT (1 1)", |
| 101 | + (a, b) -> call(Predicates.ST_Contains.class.getSimpleName(), a, b))); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testWithin() { |
| 106 | + assertTrue( |
| 107 | + (Boolean) |
| 108 | + eval( |
| 109 | + "POINT (1 1)", |
| 110 | + "POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))", |
| 111 | + (a, b) -> call(Predicates.ST_Within.class.getSimpleName(), a, b))); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testEquals() { |
| 116 | + assertTrue( |
| 117 | + (Boolean) |
| 118 | + eval( |
| 119 | + "POINT (1 1)", |
| 120 | + "POINT (1 1)", |
| 121 | + (a, b) -> call(Predicates.ST_Equals.class.getSimpleName(), a, b))); |
| 122 | + assertFalse( |
| 123 | + (Boolean) |
| 124 | + eval( |
| 125 | + "POINT (1 1)", |
| 126 | + "POINT (2 2)", |
| 127 | + (a, b) -> call(Predicates.ST_Equals.class.getSimpleName(), a, b))); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testDWithin() { |
| 132 | + // Two points ~111 km apart (1 degree of latitude near the equator). |
| 133 | + assertTrue( |
| 134 | + (Boolean) |
| 135 | + eval( |
| 136 | + "POINT (0 0)", |
| 137 | + "POINT (0 1)", |
| 138 | + (a, b) -> call(Predicates.ST_DWithin.class.getSimpleName(), a, b, lit(200000.0)))); |
| 139 | + assertFalse( |
| 140 | + (Boolean) |
| 141 | + eval( |
| 142 | + "POINT (0 0)", |
| 143 | + "POINT (0 1)", |
| 144 | + (a, b) -> call(Predicates.ST_DWithin.class.getSimpleName(), a, b, lit(50000.0)))); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void testGeometryStillWorks() { |
| 149 | + // The geometry overload must remain selectable on the same predicate. |
| 150 | + RowTypeInfo ti = |
| 151 | + new RowTypeInfo( |
| 152 | + new TypeInformation<?>[] { |
| 153 | + BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO |
| 154 | + }, |
| 155 | + new String[] {"a", "b"}); |
| 156 | + DataStream<Row> ds = |
| 157 | + env.fromCollection( |
| 158 | + Collections.singletonList( |
| 159 | + Row.of("POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))", "POINT (1 1)"))) |
| 160 | + .returns(ti); |
| 161 | + String geom = |
| 162 | + org.apache.sedona.flink.expressions.Constructors.ST_GeomFromWKT.class.getSimpleName(); |
| 163 | + Table t = |
| 164 | + tableEnv |
| 165 | + .fromDataStream(ds) |
| 166 | + .select(call(geom, $("a")).as("ga"), call(geom, $("b")).as("gb")); |
| 167 | + Object out = |
| 168 | + first( |
| 169 | + t.select( |
| 170 | + call(Predicates.ST_Contains.class.getSimpleName(), $("ga"), $("gb")).as("o"))) |
| 171 | + .getFieldAs("o"); |
| 172 | + assertEquals(true, out); |
| 173 | + } |
| 174 | +} |
0 commit comments