-
Notifications
You must be signed in to change notification settings - Fork 764
[GH-3061] Add geography constructors to SedonaFlink #3062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
flink/src/main/java/org/apache/sedona/flink/expressions/geography/GeographyConstructors.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| /* | ||
| * 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.flink.expressions.geography; | ||
|
|
||
| import org.apache.flink.table.annotation.DataTypeHint; | ||
| import org.apache.flink.table.functions.ScalarFunction; | ||
| import org.apache.sedona.common.S2Geography.Geography; | ||
| import org.apache.sedona.common.geography.Constructors; | ||
| import org.apache.sedona.flink.GeographyTypeSerializer; | ||
| import org.apache.sedona.flink.GeometryTypeSerializer; | ||
| import org.locationtech.jts.geom.Geometry; | ||
| import org.locationtech.jts.io.ParseException; | ||
|
|
||
| /** Constructors for the {@link Geography} type, wrapping {@link Constructors}. */ | ||
| public class GeographyConstructors { | ||
|
|
||
| public static class ST_GeogFromWKT extends ScalarFunction { | ||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval(@DataTypeHint("String") String wktString) throws ParseException { | ||
| return Constructors.geogFromWKT(wktString, 0); | ||
| } | ||
|
|
||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval( | ||
| @DataTypeHint("String") String wktString, @DataTypeHint("Int") Integer srid) | ||
| throws ParseException { | ||
| return Constructors.geogFromWKT(wktString, srid); | ||
| } | ||
| } | ||
|
|
||
| public static class ST_GeogFromText extends ScalarFunction { | ||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval(@DataTypeHint("String") String wktString) throws ParseException { | ||
| return Constructors.geogFromWKT(wktString, 0); | ||
| } | ||
|
|
||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval( | ||
| @DataTypeHint("String") String wktString, @DataTypeHint("Int") Integer srid) | ||
| throws ParseException { | ||
| return Constructors.geogFromWKT(wktString, srid); | ||
| } | ||
| } | ||
|
|
||
| public static class ST_GeogFromEWKT extends ScalarFunction { | ||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval(@DataTypeHint("String") String ewktString) throws ParseException { | ||
| return Constructors.geogFromEWKT(ewktString); | ||
| } | ||
| } | ||
|
|
||
| public static class ST_GeogCollFromText extends ScalarFunction { | ||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval(@DataTypeHint("String") String wktString) throws ParseException { | ||
| return Constructors.geogCollFromText(wktString, 0); | ||
| } | ||
|
|
||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval( | ||
| @DataTypeHint("String") String wktString, @DataTypeHint("Int") Integer srid) | ||
| throws ParseException { | ||
| return Constructors.geogCollFromText(wktString, srid); | ||
| } | ||
| } | ||
|
|
||
| public static class ST_GeogFromWKB extends ScalarFunction { | ||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval(@DataTypeHint("Bytes") byte[] wkb) throws ParseException { | ||
| return Constructors.geogFromWKB(wkb, 0); | ||
| } | ||
|
|
||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval(@DataTypeHint("Bytes") byte[] wkb, @DataTypeHint("Int") Integer srid) | ||
| throws ParseException { | ||
| return Constructors.geogFromWKB(wkb, srid); | ||
| } | ||
| } | ||
|
|
||
| public static class ST_GeogFromEWKB extends ScalarFunction { | ||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval(@DataTypeHint("Bytes") byte[] wkb) throws ParseException { | ||
| return Constructors.geogFromWKB(wkb); | ||
| } | ||
| } | ||
|
|
||
| public static class ST_GeogFromGeoHash extends ScalarFunction { | ||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval( | ||
| @DataTypeHint("String") String geoHash, @DataTypeHint("Int") Integer precision) { | ||
| return Constructors.geogFromGeoHash(geoHash, precision); | ||
| } | ||
|
|
||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval(@DataTypeHint("String") String geoHash) { | ||
| return Constructors.geogFromGeoHash(geoHash, null); | ||
| } | ||
| } | ||
|
|
||
| public static class ST_GeogToGeometry extends ScalarFunction { | ||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeometryTypeSerializer.class, | ||
| bridgedTo = Geometry.class) | ||
| public Geometry eval( | ||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| Object o) { | ||
| return Constructors.geogToGeometry((Geography) o); | ||
| } | ||
| } | ||
|
|
||
| public static class ST_GeomToGeography extends ScalarFunction { | ||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeographyTypeSerializer.class, | ||
| bridgedTo = Geography.class) | ||
| public Geography eval( | ||
| @DataTypeHint( | ||
| value = "RAW", | ||
| rawSerializer = GeometryTypeSerializer.class, | ||
| bridgedTo = Geometry.class) | ||
| Object o) { | ||
| return Constructors.geomToGeography((Geometry) o); | ||
| } | ||
| } | ||
| } | ||
181 changes: 181 additions & 0 deletions
181
flink/src/test/java/org/apache/sedona/flink/GeographyConstructorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /* | ||
| * 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.flink; | ||
|
|
||
| import static org.apache.flink.table.api.Expressions.$; | ||
| import static org.apache.flink.table.api.Expressions.call; | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import org.apache.flink.api.common.typeinfo.BasicTypeInfo; | ||
| import org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo; | ||
| import org.apache.flink.api.common.typeinfo.TypeInformation; | ||
| import org.apache.flink.api.java.typeutils.RowTypeInfo; | ||
| import org.apache.flink.streaming.api.datastream.DataStream; | ||
| import org.apache.flink.table.api.Table; | ||
| import org.apache.flink.types.Row; | ||
| import org.apache.sedona.common.S2Geography.Geography; | ||
| import org.apache.sedona.common.geography.Constructors; | ||
| import org.apache.sedona.flink.expressions.geography.GeographyConstructors; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.locationtech.jts.geom.Geometry; | ||
| import org.locationtech.jts.io.WKBWriter; | ||
| import org.locationtech.jts.io.WKTReader; | ||
|
|
||
| public class GeographyConstructorTest extends TestBase { | ||
|
|
||
| @BeforeClass | ||
| public static void onceExecutedBeforeAll() { | ||
| initialize(); | ||
| } | ||
|
|
||
| /** Build a single-row, single-column source table. */ | ||
| private Table sourceTable(String colName, Object value, TypeInformation<?> colType) { | ||
| List<Row> data = Collections.singletonList(Row.of(value)); | ||
| RowTypeInfo typeInfo = | ||
| new RowTypeInfo(new TypeInformation<?>[] {colType}, new String[] {colName}); | ||
| DataStream<Row> ds = env.fromCollection(data).returns(typeInfo); | ||
| return tableEnv.fromDataStream(ds); | ||
| } | ||
|
|
||
| private Geography evalGeog(Table source, String func, String inputCol) { | ||
| Table out = source.select(call(func, $(inputCol)).as("geog")); | ||
| return first(out).getFieldAs("geog"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeogFromWKT() throws Exception { | ||
| Table src = sourceTable("wkt", "POINT (1 2)", BasicTypeInfo.STRING_TYPE_INFO); | ||
| Geography result = | ||
| evalGeog(src, GeographyConstructors.ST_GeogFromWKT.class.getSimpleName(), "wkt"); | ||
| assertEquals(Constructors.geogFromWKT("POINT (1 2)", 0).toEWKT(), result.toEWKT()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeogFromWKTWithSrid() throws Exception { | ||
| Table src = sourceTable("wkt", "POINT (1 2)", BasicTypeInfo.STRING_TYPE_INFO); | ||
| Table out = | ||
| src.select( | ||
| call( | ||
| GeographyConstructors.ST_GeogFromWKT.class.getSimpleName(), | ||
| $("wkt"), | ||
| org.apache.flink.table.api.Expressions.lit(4326)) | ||
| .as("geog")); | ||
| Geography result = first(out).getFieldAs("geog"); | ||
| assertEquals(4326, result.getSRID()); | ||
| assertEquals(Constructors.geogFromWKT("POINT (1 2)", 4326).toEWKT(), result.toEWKT()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeogFromText() throws Exception { | ||
| Table src = sourceTable("wkt", "LINESTRING (0 0, 1 1, 2 2)", BasicTypeInfo.STRING_TYPE_INFO); | ||
| Geography result = | ||
| evalGeog(src, GeographyConstructors.ST_GeogFromText.class.getSimpleName(), "wkt"); | ||
| assertEquals( | ||
| Constructors.geogFromWKT("LINESTRING (0 0, 1 1, 2 2)", 0).toEWKT(), result.toEWKT()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeogFromEWKT() throws Exception { | ||
| Table src = sourceTable("ewkt", "SRID=4326;POINT (1 2)", BasicTypeInfo.STRING_TYPE_INFO); | ||
| Geography result = | ||
| evalGeog(src, GeographyConstructors.ST_GeogFromEWKT.class.getSimpleName(), "ewkt"); | ||
| assertEquals(4326, result.getSRID()); | ||
| assertEquals(Constructors.geogFromEWKT("SRID=4326;POINT (1 2)").toEWKT(), result.toEWKT()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeogCollFromText() throws Exception { | ||
| String wkt = "GEOMETRYCOLLECTION (POINT (1 2), LINESTRING (0 0, 1 1))"; | ||
| Table src = sourceTable("wkt", wkt, BasicTypeInfo.STRING_TYPE_INFO); | ||
| Geography result = | ||
| evalGeog(src, GeographyConstructors.ST_GeogCollFromText.class.getSimpleName(), "wkt"); | ||
| assertEquals(Constructors.geogCollFromText(wkt, 0).toEWKT(), result.toEWKT()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeogFromWKB() throws Exception { | ||
| Geometry point = new WKTReader().read("POINT (1 2)"); | ||
| byte[] wkb = new WKBWriter().write(point); | ||
| Table src = sourceTable("wkb", wkb, PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO); | ||
| Geography result = | ||
| evalGeog(src, GeographyConstructors.ST_GeogFromWKB.class.getSimpleName(), "wkb"); | ||
| assertEquals(Constructors.geogFromWKB(wkb, 0).toEWKT(), result.toEWKT()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeogFromEWKB() throws Exception { | ||
| Geometry point = new WKTReader().read("POINT (3 4)"); | ||
| point.setSRID(4326); | ||
| // includeSRID=true writes EWKB carrying the SRID flag + value, so this exercises | ||
| // end-to-end SRID extraction (a plain-WKB input would not). | ||
| byte[] ewkb = new WKBWriter(2, true).write(point); | ||
| Table src = sourceTable("wkb", ewkb, PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO); | ||
| Geography result = | ||
| evalGeog(src, GeographyConstructors.ST_GeogFromEWKB.class.getSimpleName(), "wkb"); | ||
| assertEquals(4326, result.getSRID()); | ||
| assertEquals(Constructors.geogFromWKB(ewkb).toEWKT(), result.toEWKT()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeogFromGeoHash() throws Exception { | ||
| Table src = sourceTable("hash", "9q9j8ue2v71y5zzy0s4q", BasicTypeInfo.STRING_TYPE_INFO); | ||
| Geography result = | ||
| evalGeog(src, GeographyConstructors.ST_GeogFromGeoHash.class.getSimpleName(), "hash"); | ||
| assertEquals( | ||
| Constructors.geogFromGeoHash("9q9j8ue2v71y5zzy0s4q", null).toEWKT(), result.toEWKT()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeomToGeography() throws Exception { | ||
| Table src = sourceTable("wkt", "POINT (1 2)", BasicTypeInfo.STRING_TYPE_INFO); | ||
| // Build a geometry column, then convert it to geography. | ||
| Table geomTable = | ||
| src.select( | ||
| call( | ||
| org.apache.sedona.flink.expressions.Constructors.ST_GeomFromWKT.class | ||
| .getSimpleName(), | ||
| $("wkt")) | ||
| .as("geom")); | ||
| Table out = | ||
| geomTable.select( | ||
| call(GeographyConstructors.ST_GeomToGeography.class.getSimpleName(), $("geom")) | ||
| .as("geog")); | ||
| Geography result = first(out).getFieldAs("geog"); | ||
| Geometry point = new WKTReader().read("POINT (1 2)"); | ||
| assertEquals(Constructors.geomToGeography(point).toEWKT(), result.toEWKT()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeogToGeometry() throws Exception { | ||
| Table src = sourceTable("wkt", "POINT (1 2)", BasicTypeInfo.STRING_TYPE_INFO); | ||
| Table geogTable = | ||
| src.select( | ||
| call(GeographyConstructors.ST_GeogFromWKT.class.getSimpleName(), $("wkt")).as("geog")); | ||
| Table out = | ||
| geogTable.select( | ||
| call(GeographyConstructors.ST_GeogToGeometry.class.getSimpleName(), $("geog")) | ||
| .as("geom")); | ||
| Geometry result = first(out).getFieldAs("geom"); | ||
| Geometry expected = Constructors.geogToGeometry(Constructors.geogFromWKT("POINT (1 2)", 0)); | ||
| assertEquals(expected.toText(), result.toText()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved GeographyConstructors into the org.apache.sedona.flink.expressions.geography sub-package, so the simple name no longer collides with expressions.Constructors (the geometry constructors) and the import is unambiguous. Catalog.java imports the sub-package explicitly since the wildcard import is not recursive.