This is an automated email from the ASF dual-hosted git repository.
jiayu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sedona.git
The following commit(s) were added to refs/heads/master by this push:
new d8eb59a7 [SEDONA-303] Port all Sedona Spark functions to Sedona Flink
-- Step 3 (#884)
d8eb59a7 is described below
commit d8eb59a7b074e734edf03e09c27e7870541cb7d2
Author: Junhao Liu <[email protected]>
AuthorDate: Tue Jul 4 14:24:55 2023 +0800
[SEDONA-303] Port all Sedona Spark functions to Sedona Flink -- Step 3
(#884)
---
docs/api/flink/Function.md | 129 +++++++++++++++++++++
.../main/java/org/apache/sedona/flink/Catalog.java | 5 +
.../apache/sedona/flink/expressions/Functions.java | 58 +++++++++
.../java/org/apache/sedona/flink/FunctionTest.java | 49 ++++++++
4 files changed, 241 insertions(+)
diff --git a/docs/api/flink/Function.md b/docs/api/flink/Function.md
index a35dc52d..2b234826 100644
--- a/docs/api/flink/Function.md
+++ b/docs/api/flink/Function.md
@@ -1145,6 +1145,135 @@ Output:
+-----------------------------------------+
```
+## ST_LineMerge
+
+Introduction: Returns a LineString formed by sewing together the constituent
line work of a MULTILINESTRING.
+
+!!!note
+ Only works for MULTILINESTRING. Using other geometry will return a
GEOMETRYCOLLECTION EMPTY. If the MultiLineString can't be merged, the original
MULTILINESTRING is returned.
+
+Format: `ST_LineMerge (A:geometry)`
+
+Since: `v1.5.0`
+
+Example:
+```sql
+SELECT ST_LineMerge(geometry)
+FROM df
+```
+
+## ST_LineSubstring
+
+Introduction: Return a linestring being a substring of the input one starting
and ending at the given fractions of total 2d length. Second and third
arguments are Double values between 0 and 1. This only works with LINESTRINGs.
+
+Format: `ST_LineSubstring (geom: geometry, startfraction: Double, endfraction:
Double)`
+
+Since: `v1.5.0`
+
+Example:
+```sql
+SELECT ST_LineSubstring(ST_GeomFromWKT('LINESTRING(25 50, 100 125, 150 190)'),
0.333, 0.666) as Substring
+```
+
+Output:
+```
++------------------------------------------------------------------------------------------------+
+|Substring
|
++------------------------------------------------------------------------------------------------+
+|LINESTRING (69.28469348539744 94.28469348539744, 100 125, 111.70035626068274
140.21046313888758)|
++------------------------------------------------------------------------------------------------+
+```
+
+## ST_MakePolygon
+
+Introduction: Function to convert closed linestring to polygon including holes
+
+Format: `ST_MakePolygon(geom: geometry, holes: array<geometry>)`
+
+Since: `v1.5.0`
+
+Example:
+
+Query:
+```sql
+SELECT
+ ST_MakePolygon(
+ ST_GeomFromText('LINESTRING(7 -1, 7 6, 9 6, 9 1, 7 -1)'),
+ ARRAY(ST_GeomFromText('LINESTRING(6 2, 8 2, 8 1, 6 1, 6 2)'))
+ ) AS polygon
+```
+
+Result:
+
+```
++----------------------------------------------------------------+
+|polygon |
++----------------------------------------------------------------+
+|POLYGON ((7 -1, 7 6, 9 6, 9 1, 7 -1), (6 2, 8 2, 8 1, 6 1, 6 2))|
++----------------------------------------------------------------+
+
+```
+
+## ST_MakeValid
+
+Introduction: Given an invalid geometry, create a valid representation of the
geometry.
+
+Collapsed geometries are either converted to empty (keepCollaped=true) or a
valid geometry of lower dimension (keepCollapsed=false).
+Default is keepCollapsed=false.
+
+Format: `ST_MakeValid (A:geometry)`
+
+Format: `ST_MakeValid (A:geometry, keepCollapsed:Boolean)`
+
+Since: `v1.5.0`
+
+Example:
+
+```sql
+WITH linestring AS (
+ SELECT ST_GeomFromWKT('LINESTRING(1 1, 1 1)') AS geom
+) SELECT ST_MakeValid(geom), ST_MakeValid(geom, true) FROM linestring
+```
+
+Result:
+```
++------------------+------------------------+
+|st_makevalid(geom)|st_makevalid(geom, true)|
++------------------+------------------------+
+| LINESTRING EMPTY| POINT (1 1)|
++------------------+------------------------+
+```
+
+## ST_Multi
+
+Introduction: Returns a MultiGeometry object based on the geometry input.
+ST_Multi is basically an alias for ST_Collect with one geometry.
+
+Format
+
+`ST_Multi(geom: geometry)`
+
+Since: `v1.5.0`
+
+Example:
+
+```sql
+SELECT ST_Multi(
+ ST_GeomFromText('POINT(1 1)')
+) AS geom
+```
+
+Result:
+
+```
++---------------------------------------------------------------+
+|geom |
++---------------------------------------------------------------+
+|MULTIPOINT (1 1) |
++---------------------------------------------------------------+
+```
+
+
## ST_Normalize
Introduction: Returns the input geometry in its normalized form.
diff --git a/flink/src/main/java/org/apache/sedona/flink/Catalog.java
b/flink/src/main/java/org/apache/sedona/flink/Catalog.java
index 655a0e35..5999071f 100644
--- a/flink/src/main/java/org/apache/sedona/flink/Catalog.java
+++ b/flink/src/main/java/org/apache/sedona/flink/Catalog.java
@@ -107,6 +107,11 @@ public class Catalog {
new Functions.ST_RemovePoint(),
new Functions.ST_SetPoint(),
new Functions.ST_LineFromMultiPoint(),
+ new Functions.ST_LineMerge(),
+ new Functions.ST_LineSubstring(),
+ new Functions.ST_MakePolygon(),
+ new Functions.ST_MakeValid(),
+ new Functions.ST_Multi(),
new Functions.ST_Split(),
new Functions.ST_S2CellIDs(),
new Functions.ST_GeometricMedian(),
diff --git
a/flink/src/main/java/org/apache/sedona/flink/expressions/Functions.java
b/flink/src/main/java/org/apache/sedona/flink/expressions/Functions.java
index 6bde1356..8291b3de 100644
--- a/flink/src/main/java/org/apache/sedona/flink/expressions/Functions.java
+++ b/flink/src/main/java/org/apache/sedona/flink/expressions/Functions.java
@@ -14,8 +14,10 @@
package org.apache.sedona.flink.expressions;
import org.apache.flink.table.annotation.DataTypeHint;
+import org.apache.flink.table.annotation.InputGroup;
import org.apache.flink.table.functions.ScalarFunction;
import org.locationtech.jts.geom.Geometry;
+import org.locationtech.jts.operation.buffer.BufferParameters;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.operation.TransformException;
@@ -641,6 +643,62 @@ public class Functions {
}
}
+ public static class ST_LineMerge extends ScalarFunction {
+ @DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class)
+ public Geometry eval(@DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class) Object o) {
+ Geometry geom = (Geometry) o;
+ return org.apache.sedona.common.Functions.lineMerge(geom);
+ }
+ }
+
+ public static class ST_LineSubstring extends ScalarFunction {
+ @DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class)
+ public Geometry eval(@DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class) Object o,
+ @DataTypeHint("Double") Double startFraction,
@DataTypeHint("Double") Double endFraction) {
+ Geometry geom = (Geometry) o;
+ return org.apache.sedona.common.Functions.lineSubString(geom,
startFraction, endFraction);
+ }
+ }
+
+ public static class ST_MakePolygon extends ScalarFunction {
+ @DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class)
+ public Geometry eval(@DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class) Object o1,
+ @DataTypeHint(inputGroup = InputGroup.ANY) Object
o2) {
+ Geometry outerLinestring = (Geometry) o1;
+ Geometry[] interiorLinestrings = (Geometry[]) o2;
+ return
org.apache.sedona.common.Functions.makePolygon(outerLinestring,
interiorLinestrings);
+ }
+
+ @DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class)
+ public Geometry eval(@DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class) Object o) {
+ Geometry linestring = (Geometry) o;
+ return org.apache.sedona.common.Functions.makePolygon(linestring,
null);
+ }
+ }
+
+ public static class ST_MakeValid extends ScalarFunction {
+ @DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class)
+ public Geometry eval(@DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class) Object o,
+ @DataTypeHint("Boolean") Boolean keepCollapsed) {
+ Geometry geom = (Geometry) o;
+ return org.apache.sedona.common.Functions.makeValid(geom,
keepCollapsed);
+ }
+
+ @DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class)
+ public Geometry eval(@DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class) Object o) {
+ Geometry geom = (Geometry) o;
+ return org.apache.sedona.common.Functions.makeValid(geom, false);
+ }
+ }
+
+ public static class ST_Multi extends ScalarFunction {
+ @DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class)
+ public Geometry eval(@DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class) Object o) {
+ Geometry geom = (Geometry) o;
+ return
org.apache.sedona.common.Functions.createMultiGeometryFromOneElement(geom);
+ }
+ }
+
public static class ST_Split extends ScalarFunction {
@DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class)
public Geometry eval(@DataTypeHint(value = "RAW", bridgedTo =
org.locationtech.jts.geom.Geometry.class) Object o1,
diff --git a/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
b/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
index f21298ba..6156709f 100644
--- a/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
+++ b/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
@@ -734,6 +734,55 @@ public class FunctionTest extends TestBase{
assertEquals("LINESTRING (10 40, 40 30, 20 20, 30 10)",
first(pointTable).getField(0).toString());
}
+ @Test
+ public void testLineMerge() {
+ Table table = tableEnv.sqlQuery("SELECT
ST_GeomFromWKT('MULTILINESTRING((10 160, 60 120), (120 140, 60 120), (120 140,
180 120))') AS multiline");
+ table =
table.select(call(Functions.ST_LineMerge.class.getSimpleName(),
$("multiline")));
+ Geometry result = (Geometry) first(table).getField(0);
+ assertEquals("LINESTRING (10 160, 60 120, 120 140, 180 120)",
result.toString());
+ }
+
+ @Test
+ public void testLineSubString() {
+ Table table = tableEnv.sqlQuery("SELECT ST_GeomFromWKT('LINESTRING (0
0, 2 0)') AS line");
+ table =
table.select(call(Functions.ST_LineSubstring.class.getSimpleName(), $("line"),
0.5, 1.0));
+ Geometry result = (Geometry) first(table).getField(0);
+ assertEquals("LINESTRING (1 0, 2 0)", result.toString());
+ }
+
+ @Test
+ public void testMakePolygon() {
+ Table table = tableEnv.sqlQuery("SELECT ST_GeomFromWKT('LINESTRING (0
0, 1 0, 1 1, 0 0)') AS line");
+ table =
table.select(call(Functions.ST_MakePolygon.class.getSimpleName(), $("line")));
+ Geometry result = (Geometry) first(table).getField(0);
+ assertEquals("POLYGON ((0 0, 1 0, 1 1, 0 0))", result.toString());
+ }
+
+ @Test
+ public void testMakePolygonWithHoles() {
+ Table table = tableEnv.sqlQuery("SELECT ST_GeomFromText('LINESTRING (0
0, 1 0, 1 1, 0 0)') AS line," +
+ "array[ST_GeomFromText('LINESTRING
(0.5 0.1, 0.7 0.1, 0.7 0.3, 0.5 0.1)')] AS holes");
+ table =
table.select(call(Functions.ST_MakePolygon.class.getSimpleName(), $("line"),
$("holes")));
+ Geometry result = (Geometry) first(table).getField(0);
+ assertEquals("POLYGON ((0 0, 1 0, 1 1, 0 0), (0.5 0.1, 0.7 0.1, 0.7
0.3, 0.5 0.1))", result.toString());
+ }
+
+ @Test
+ public void testMakeValid() {
+ Table table = tableEnv.sqlQuery("SELECT ST_GeomFromWKT('POLYGON ((1 5,
1 1, 3 3, 5 3, 7 1, 7 5, 5 3, 3 3, 1 5))') AS polygon");
+ table =
table.select(call(Functions.ST_MakeValid.class.getSimpleName(), $("polygon")));
+ Geometry result = (Geometry) first(table).getField(0);
+ assertEquals("MULTIPOLYGON (((1 5, 3 3, 1 1, 1 5)), ((5 3, 7 5, 7 1, 5
3)))", result.toString());
+ }
+
+ @Test
+ public void testMulti() {
+ Table table = tableEnv.sqlQuery("SELECT ST_GeomFromWKT('POINT (0 0)')
AS geom");
+ table = table.select(call(Functions.ST_Multi.class.getSimpleName(),
$("geom")));
+ Geometry result = (Geometry) first(table).getField(0);
+ assertEquals("MULTIPOINT ((0 0))", result.toString());
+ }
+
@Test
public void testSplit() {
Table pointTable = tableEnv.sqlQuery("SELECT
ST_Split(ST_GeomFromWKT('LINESTRING (0 0, 1.5 1.5, 2 2)'),
ST_GeomFromWKT('MULTIPOINT (0.5 0.5, 1 1)'))");