You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sedona.apache.org by ji...@apache.org on 2023/05/20 08:59:37 UTC

[sedona] 03/04: Finish Sedona Flink

This is an automated email from the ASF dual-hosted git repository.

jiayu pushed a commit to branch geodestic-distance
in repository https://gitbox.apache.org/repos/asf/sedona.git

commit 7e9ce3bd258be5c0399abb2f4d803b6abb938958
Author: Jia Yu <ji...@apache.org>
AuthorDate: Sat May 20 01:31:41 2023 -0700

    Finish Sedona Flink
---
 .../main/java/org/apache/sedona/flink/Catalog.java |  4 ++
 .../apache/sedona/flink/expressions/Functions.java | 44 +++++++++++++++++++++
 .../java/org/apache/sedona/flink/FunctionTest.java | 45 ++++++++++++++++++++++
 3 files changed, 93 insertions(+)

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 1075710a..66e4bffa 100644
--- a/flink/src/main/java/org/apache/sedona/flink/Catalog.java
+++ b/flink/src/main/java/org/apache/sedona/flink/Catalog.java
@@ -37,14 +37,18 @@ public class Catalog {
                 new Constructors.ST_MPolyFromText(),
                 new Constructors.ST_MLineFromText(),
                 new Functions.ST_Area(),
+                new Functions.ST_AreaSpheroid(),
                 new Functions.ST_Azimuth(),
                 new Functions.ST_Boundary(),
                 new Functions.ST_Buffer(),
                 new Functions.ST_ConcaveHull(),
                 new Functions.ST_Envelope(),
                 new Functions.ST_Distance(),
+                new Functions.ST_DistanceSphere(),
+                new Functions.ST_DistanceSpheroid(),
                 new Functions.ST_3DDistance(),
                 new Functions.ST_Length(),
+                new Functions.ST_LengthSpheroid(),
                 new Functions.ST_Transform(),
                 new Functions.ST_FlipCoordinates(),
                 new Functions.ST_GeoHash(),
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 11f5e9d8..608a461b 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
@@ -28,6 +28,14 @@ public class Functions {
         }
     }
 
+    public static class ST_AreaSpheroid extends ScalarFunction {
+        @DataTypeHint("Double")
+        public Double eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o) {
+            Geometry geom = (Geometry) o;
+            return org.apache.sedona.common.sphere.Spheroid.area(geom);
+        }
+    }
+
     public static class ST_Azimuth extends ScalarFunction {
         @DataTypeHint("Double")
         public Double eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o1,
@@ -89,6 +97,34 @@ public class Functions {
         }
     }
 
+    public static class ST_DistanceSphere extends ScalarFunction {
+        @DataTypeHint("Double")
+        public Double eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o1,
+                @DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o2) {
+            Geometry geom1 = (Geometry) o1;
+            Geometry geom2 = (Geometry) o2;
+            return org.apache.sedona.common.sphere.Haversine.distance(geom1, geom2);
+        }
+
+        @DataTypeHint("Double")
+        public Double eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o1,
+                @DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o2, @DataTypeHint("Double") Double radius) {
+            Geometry geom1 = (Geometry) o1;
+            Geometry geom2 = (Geometry) o2;
+            return org.apache.sedona.common.sphere.Haversine.distance(geom1, geom2, radius);
+        }
+    }
+
+    public static class ST_DistanceSpheroid extends ScalarFunction {
+        @DataTypeHint("Double")
+        public Double eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o1,
+                @DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o2) {
+            Geometry geom1 = (Geometry) o1;
+            Geometry geom2 = (Geometry) o2;
+            return org.apache.sedona.common.sphere.Spheroid.distance(geom1, geom2);
+        }
+    }
+
     public static class ST_3DDistance extends ScalarFunction {
         @DataTypeHint("Double")
         public Double eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o1,
@@ -107,6 +143,14 @@ public class Functions {
         }
     }
 
+    public static class ST_LengthSpheroid extends ScalarFunction {
+        @DataTypeHint("Double")
+        public Double eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o) {
+            Geometry geom = (Geometry) o;
+            return org.apache.sedona.common.sphere.Spheroid.length(geom);
+        }
+    }
+
     public static class ST_YMin extends ScalarFunction {
         @DataTypeHint("Double")
         public Double eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o){
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 02486e39..b34503e3 100644
--- a/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
+++ b/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
@@ -53,6 +53,15 @@ public class FunctionTest extends TestBase{
         assertEquals(1.0, result, 0);
     }
 
+    @Test
+    public void testAreaSpheroid() {
+        Table tbl = tableEnv.sqlQuery(
+                "SELECT ST_AreaSpheroid(ST_GeomFromWKT('Polygon ((35 34, 30 28, 34 25, 35 34))'))");
+        Double expected = 201824850811.76245;
+        Double actual = (Double) first(tbl).getField(0);
+        assertEquals(expected, actual);
+    }
+
     @Test
     public void testAzimuth() {
         Table pointTable = tableEnv.sqlQuery("SELECT ST_Azimuth(ST_GeomFromWKT('POINT (0 0)'), ST_GeomFromWKT('POINT (1 1)'))");
@@ -153,6 +162,33 @@ public class FunctionTest extends TestBase{
         assertEquals(0.0, first(pointTable).getField(0));
     }
 
+    @Test
+    public void testDistanceSpheroid() {
+        Table tbl = tableEnv.sqlQuery(
+                "SELECT ST_DistanceSpheroid(ST_GeomFromWKT('POINT (51.3168 -0.56)'), ST_GeomFromWKT('POINT (55.9533 -3.1883)'))");
+        Double expected = 544430.9411996207;
+        Double actual = (Double) first(tbl).getField(0);
+        assertEquals(expected, actual);
+    }
+
+    @Test
+    public void testDistanceSphere() {
+        Table tbl = tableEnv.sqlQuery(
+                "SELECT ST_DistanceSphere(ST_GeomFromWKT('POINT (51.3168 -0.56)'), ST_GeomFromWKT('POINT (55.9533 -3.1883)'))");
+        Double expected = 544405.4459192449;
+        Double actual = (Double) first(tbl).getField(0);
+        assertEquals(expected, actual);
+    }
+
+    @Test
+    public void testDistanceSphereWithRadius() {
+        Table tbl = tableEnv.sqlQuery(
+                "SELECT ST_DistanceSphere(ST_GeomFromWKT('POINT (51.3168 -0.56)'), ST_GeomFromWKT('POINT (55.9533 -3.1883)'), 6378137.0)");
+        Double expected = 544405.4459192449;
+        Double actual = (Double) first(tbl).getField(0);
+        assertEquals(expected, actual);
+    }
+
     @Test
     public void test3dDistance() {
         Table pointTable = tableEnv.sqlQuery("SELECT ST_3DDistance(ST_GeomFromWKT('POINT (0 0 0)'), ST_GeomFromWKT('POINT (1 1 1)'))");
@@ -168,6 +204,15 @@ public class FunctionTest extends TestBase{
         assertEquals(4, result, 0);
     }
 
+    @Test
+    public void testLengthSpheroid() {
+        Table tbl = tableEnv.sqlQuery(
+                "SELECT ST_LengthSpheroid(ST_GeomFromWKT('Polygon ((0 0, 0 90, 0 0))'))");
+        Double expected = 20037508.342789244;
+        Double actual = (Double) first(tbl).getField(0);
+        assertEquals(expected, actual);
+    }
+
     @Test
     public void testYMax() {
         Table polygonTable = createPolygonTable(1);