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 2022/08/18 04:11:55 UTC

[incubator-sedona] branch master updated: [SEDONA-147] Add SRID funtions to the Flink API (#667)

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/incubator-sedona.git


The following commit(s) were added to refs/heads/master by this push:
     new cd191f57 [SEDONA-147] Add SRID funtions to the Flink API (#667)
cd191f57 is described below

commit cd191f570af55971c465a497148d7c84b6121db5
Author: Kengo Seki <se...@apache.org>
AuthorDate: Thu Aug 18 13:11:51 2022 +0900

    [SEDONA-147] Add SRID funtions to the Flink API (#667)
---
 .../java/org/apache/sedona/common/Functions.java   | 16 ++++++++++++
 docs/api/flink/Function.md                         | 28 ++++++++++++++++++++
 .../main/java/org/apache/sedona/flink/Catalog.java |  4 ++-
 .../apache/sedona/flink/expressions/Functions.java | 16 ++++++++++++
 .../java/org/apache/sedona/flink/FunctionTest.java | 18 +++++++++++++
 .../sql/sedona_sql/expressions/Functions.scala     | 30 ++--------------------
 6 files changed, 83 insertions(+), 29 deletions(-)

diff --git a/common/src/main/java/org/apache/sedona/common/Functions.java b/common/src/main/java/org/apache/sedona/common/Functions.java
index 581cd65d..87c358df 100644
--- a/common/src/main/java/org/apache/sedona/common/Functions.java
+++ b/common/src/main/java/org/apache/sedona/common/Functions.java
@@ -19,6 +19,7 @@ import org.geotools.geometry.jts.JTS;
 import org.geotools.referencing.CRS;
 import org.locationtech.jts.geom.Coordinate;
 import org.locationtech.jts.geom.Geometry;
+import org.locationtech.jts.geom.GeometryFactory;
 import org.locationtech.jts.geom.LineString;
 import org.opengis.referencing.FactoryException;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
@@ -140,4 +141,19 @@ public class Functions {
     public static Geometry buildArea(Geometry geometry) {
         return GeomUtils.buildArea(geometry);
     }
+
+    public static Geometry setSRID(Geometry geometry, int srid) {
+        if (geometry == null) {
+            return null;
+        }
+        GeometryFactory factory = new GeometryFactory(geometry.getPrecisionModel(), srid, geometry.getFactory().getCoordinateSequenceFactory());
+        return factory.createGeometry(geometry);
+    }
+
+    public static int getSRID(Geometry geometry) {
+        if (geometry == null) {
+            return 0;
+        }
+        return geometry.getSRID();
+    }
 }
diff --git a/docs/api/flink/Function.md b/docs/api/flink/Function.md
index afc0f36d..a69a3e5b 100644
--- a/docs/api/flink/Function.md
+++ b/docs/api/flink/Function.md
@@ -299,6 +299,34 @@ Input: `POLYGON ((-0.5 -0.5, -0.5 0.5, 0.5 0.5, 0.5 -0.5, -0.5 -0.5))`
 
 Output: `POLYGON ((-0.5 -0.5, 0.5 -0.5, 0.5 0.5, -0.5 0.5, -0.5 -0.5))`
 
+## ST_SetSRID
+
+Introduction: Sets the spatial refence system identifier (SRID) of the geometry.
+
+Format: `ST_SetSRID (A:geometry, srid: integer)`
+
+Since: `v1.3.0`
+
+Example:
+```SQL
+SELECT ST_SetSRID(polygondf.countyshape, 3021)
+FROM polygondf
+```
+
+## ST_SRID
+
+Introduction: Return the spatial refence system identifier (SRID) of the geometry.
+
+Format: `ST_SRID (A:geometry)`
+
+Since: `v1.3.0`
+
+Example:
+```SQL
+SELECT ST_SRID(polygondf.countyshape)
+FROM polygondf
+```
+
 ## ST_Transform
 
 Introduction:
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 f435de1f..6b36fb7f 100644
--- a/flink/src/main/java/org/apache/sedona/flink/Catalog.java
+++ b/flink/src/main/java/org/apache/sedona/flink/Catalog.java
@@ -50,7 +50,9 @@ public class Catalog {
                 new Functions.ST_YMin(),
                 new Functions.ST_XMax(),
                 new Functions.ST_XMin(),
-                new Functions.ST_BuildArea()
+                new Functions.ST_BuildArea(),
+                new Functions.ST_SetSRID(),
+                new Functions.ST_SRID()
         };
     }
 
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 2db89814..4c94b2e8 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
@@ -204,4 +204,20 @@ public class Functions {
             return org.apache.sedona.common.Functions.buildArea(geom);
         }
     }
+
+    public static class ST_SetSRID 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, int srid) {
+            Geometry geom = (Geometry) o;
+            return org.apache.sedona.common.Functions.setSRID(geom, srid);
+        }
+    }
+
+    public static class ST_SRID extends ScalarFunction {
+        @DataTypeHint("Integer")
+        public Integer eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o) {
+            Geometry geom = (Geometry) o;
+            return org.apache.sedona.common.Functions.getSRID(geom);
+        }
+    }
 }
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 e9910bdd..e6494cb4 100644
--- a/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
+++ b/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
@@ -226,5 +226,23 @@ public class FunctionTest extends TestBase{
         Geometry result = (Geometry) first(arealGeomTable).getField(0);
         assertEquals("POLYGON ((-0.5 -0.5, -0.5 0.5, 0.5 0.5, 0.5 -0.5, -0.5 -0.5))", result.toString());
     }
+
+    @Test
+    public void testSetSRID() {
+        Table polygonTable = createPolygonTable(1);
+        polygonTable = polygonTable
+                .select(call(Functions.ST_SetSRID.class.getSimpleName(), $(polygonColNames[0]), 3021))
+                .select(call(Functions.ST_SRID.class.getSimpleName(), $("_c0")));
+        int result = (int) first(polygonTable).getField(0);
+        assertEquals(3021, result);
+    }
+
+    @Test
+    public void testSRID() {
+        Table polygonTable = createPolygonTable(1);
+        polygonTable = polygonTable.select(call(Functions.ST_SRID.class.getSimpleName(), $(polygonColNames[0])));
+        int result = (int) first(polygonTable).getField(0);
+        assertEquals(0, result);
+    }
 }
 
diff --git a/sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala b/sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala
index 88b46e79..592758bd 100644
--- a/sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala
+++ b/sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala
@@ -503,16 +503,7 @@ case class ST_AsEWKB(inputExpressions: Seq[Expression])
 }
 
 case class ST_SRID(inputExpressions: Seq[Expression])
-  extends UnaryGeometryExpression with CodegenFallback {
-  inputExpressions.validateLength(1)
-
-  override protected def nullSafeEval(geometry: Geometry): Any = {
-    geometry.getSRID
-  }
-
-  override def dataType: DataType = IntegerType
-
-  override def children: Seq[Expression] = inputExpressions
+  extends InferredUnaryExpression(Functions.getSRID) {
 
   protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = {
     copy(inputExpressions = newChildren)
@@ -520,24 +511,7 @@ case class ST_SRID(inputExpressions: Seq[Expression])
 }
 
 case class ST_SetSRID(inputExpressions: Seq[Expression])
-  extends Expression with CodegenFallback {
-  inputExpressions.validateLength(2)
-
-  override def nullable: Boolean = true
-
-  override def eval(input: InternalRow): Any = {
-    val srid = inputExpressions(1).eval(input).asInstanceOf[Integer]
-    inputExpressions(0).toGeometry(input) match {
-      case geometry: Geometry =>
-        val factory = new GeometryFactory(geometry.getPrecisionModel, srid, geometry.getFactory.getCoordinateSequenceFactory)
-        factory.createGeometry(geometry).toGenericArrayData
-      case _ => null
-    }
-  }
-
-  override def dataType: DataType = GeometryUDT
-
-  override def children: Seq[Expression] = inputExpressions
+  extends InferredBinaryExpression(Functions.setSRID) {
 
   protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = {
     copy(inputExpressions = newChildren)