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 c8d67402 [SEDONA-98] Added ST_IsEmpty function (#619)
c8d67402 is described below

commit c8d674020462249b9451b3f880a3c517ba974f55
Author: Arindam Jain <[email protected]>
AuthorDate: Sat Apr 23 00:08:36 2022 -0700

    [SEDONA-98] Added ST_IsEmpty function (#619)
---
 .../org/apache/sedona/core/utils/GeomUtils.java    |  2 +-
 docs/api/flink/Function.md                         | 15 +++++++++++++++
 docs/api/sql/Function.md                           | 15 +++++++++++++++
 .../main/java/org/apache/sedona/flink/Catalog.java |  3 ++-
 .../apache/sedona/flink/expressions/Functions.java | 10 +++++++++-
 .../java/org/apache/sedona/flink/FunctionTest.java | 11 ++++++++++-
 .../scala/org/apache/sedona/sql/UDF/Catalog.scala  |  1 +
 .../sql/sedona_sql/expressions/Functions.scala     | 22 ++++++++++++++++++++++
 .../org/apache/sedona/sql/functionTestScala.scala  | 16 ++++++++++++----
 9 files changed, 87 insertions(+), 8 deletions(-)

diff --git a/core/src/main/java/org/apache/sedona/core/utils/GeomUtils.java 
b/core/src/main/java/org/apache/sedona/core/utils/GeomUtils.java
index c28a8eed..145c6bbd 100644
--- a/core/src/main/java/org/apache/sedona/core/utils/GeomUtils.java
+++ b/core/src/main/java/org/apache/sedona/core/utils/GeomUtils.java
@@ -112,4 +112,4 @@ public class GeomUtils
         geom.geometryChanged();
         return geom;
     }
-}
\ No newline at end of file
+}
diff --git a/docs/api/flink/Function.md b/docs/api/flink/Function.md
index d3796fd3..8c613c1a 100644
--- a/docs/api/flink/Function.md
+++ b/docs/api/flink/Function.md
@@ -101,6 +101,21 @@ Result:
 +-----------------------------+
 ```
 
+## ST_IsEmpty
+
+Introduction: Test if a geometry is empty geometry
+
+Format: `ST_IsEmpty (A:geometry)`
+
+Since: `v1.2.1`
+
+Spark SQL example:
+
+```SQL
+SELECT ST_IsEmpty(polygondf.countyshape)
+FROM polygondf
+```
+
 ## ST_PointOnSurface
 
 Introduction: Returns a POINT guaranteed to lie on the surface.
diff --git a/docs/api/sql/Function.md b/docs/api/sql/Function.md
index fa9b5353..1adfa515 100644
--- a/docs/api/sql/Function.md
+++ b/docs/api/sql/Function.md
@@ -160,6 +160,21 @@ SELECT ST_IsValid(polygondf.countyshape)
 FROM polygondf
 ```
 
+## ST_IsEmpty
+
+Introduction: Test if a geometry is empty geometry
+
+Format: `ST_IsEmpty (A:geometry)`
+
+Since: `v1.2.1`
+
+Spark SQL example:
+
+```SQL
+SELECT ST_IsEmpty(polygondf.countyshape)
+FROM polygondf
+```
+
 ## ST_MakeValid
 
 Introduction: Given an invalid geometry, create a valid representation of the 
geometry.
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 d48d8835..d0c207ea 100644
--- a/flink/src/main/java/org/apache/sedona/flink/Catalog.java
+++ b/flink/src/main/java/org/apache/sedona/flink/Catalog.java
@@ -34,7 +34,8 @@ public class Catalog {
                 new Functions.ST_PointOnSurface(),
                 new Functions.ST_Reverse(),
                 new Functions.ST_AsEWKT(),
-                new Functions.ST_Force_2D()
+                new Functions.ST_Force_2D(),
+                new Functions.ST_IsEmpty()
         };
     }
 
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 689fd10a..9dd46016 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
@@ -123,4 +123,12 @@ public class Functions {
             return GeomUtils.get2dGeom(geom);
         }
     }
-}
\ No newline at end of file
+
+    public static class ST_IsEmpty extends ScalarFunction {
+        @DataTypeHint("Boolean")
+        public boolean eval(@DataTypeHint(value = "RAW", bridgedTo = 
org.locationtech.jts.geom.Geometry.class) Object o) {
+            Geometry geom = (Geometry) o;
+            return geom.isEmpty();
+        }
+    }
+}
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 5134b06e..0a1cc84b 100644
--- a/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
+++ b/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
@@ -101,4 +101,13 @@ public class FunctionTest extends TestBase{
         Geometry result = (Geometry) first(Forced2DTable).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());
     }
-}
\ No newline at end of file
+
+    @Test
+    public void testIsEmpty() {
+        Table polygonTable = createPolygonTable(testDataSize);
+        polygonTable = 
polygonTable.select(call(Functions.ST_IsEmpty.class.getSimpleName(), 
$(polygonColNames[0])));
+        boolean result = (boolean) first(polygonTable).getField(0);
+        assertEquals(false, result);
+    }
+}
+
diff --git a/sql/src/main/scala/org/apache/sedona/sql/UDF/Catalog.scala 
b/sql/src/main/scala/org/apache/sedona/sql/UDF/Catalog.scala
index 32af95f6..df9d7c0b 100644
--- a/sql/src/main/scala/org/apache/sedona/sql/UDF/Catalog.scala
+++ b/sql/src/main/scala/org/apache/sedona/sql/UDF/Catalog.scala
@@ -57,6 +57,7 @@ object Catalog {
     ST_SymDifference,
     ST_Union,
     ST_IsValid,
+    ST_IsEmpty,
     ST_PrecisionReduce,
     ST_Equals,
     ST_Touches,
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 52d53323..a24a549e 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
@@ -1616,3 +1616,25 @@ case class ST_AsEWKT(inputExpressions: Seq[Expression])
     copy(inputExpressions = newChildren)
   }
 }
+
+/**
+ * Test if Geometry is empty geometry.
+ *
+ * @param inputExpressions
+ */
+case class ST_IsEmpty(inputExpressions: Seq[Expression])
+  extends UnaryGeometryExpression with CodegenFallback {
+  assert(inputExpressions.length == 1)
+
+  override protected def nullSafeEval(geometry: Geometry): Any = {
+    geometry.isEmpty()
+  }
+
+  override def dataType: DataType = BooleanType
+
+  override def children: Seq[Expression] = inputExpressions
+
+  protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = 
{
+    copy(inputExpressions = newChildren)
+  }
+}
diff --git a/sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala 
b/sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala
index 70df33d0..71411cc0 100644
--- a/sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala
+++ b/sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala
@@ -1349,18 +1349,26 @@ class functionTestScala extends TestBaseScala with 
Matchers with GeometrySample
       "'POINT(0 5)'"
         -> "POINT (0 5)",
       "'POLYGON((0 0 2, 0 5 2, 5 0 2, 0 0 2), (1 1 2, 3 1 2, 1 3 2, 1 1 2))'"
-        ->"POLYGON ((0 0, 0 5, 5 0, 0 0), (1 1, 3 1, 1 3, 1 1))",
+        -> "POLYGON ((0 0, 0 5, 5 0, 0 0), (1 1, 3 1, 1 3, 1 1))",
       "'LINESTRING(0 5 1, 0 0 1, 0 10 2)'"
-        ->"LINESTRING (0 5, 0 0, 0 10)"
+        -> "LINESTRING (0 5, 0 0, 0 10)"
     )
 
-    for((inputGeom, expectedGeom) <- geomTestCases1) {
+    for ((inputGeom, expectedGeom) <- geomTestCases1) {
       var df = sparkSession.sql(s"select 
ST_AsText(ST_Force_2D(ST_GeomFromText($inputGeom)))")
       var result = df.collect()
-      assert(result.head.get(0).asInstanceOf[String]==expectedGeom)
+      assert(result.head.get(0).asInstanceOf[String] == expectedGeom)
     }
   }
 
+  it ("Should pass ST_IsEmpty") {
+    var df = sparkSession.sql("SELECT ST_SetSrid(ST_GeomFromWKT('POLYGON((0 
0,0 1,1 1,1 0,0 0))'), 4326) as point")
+    df.createOrReplaceTempView("table")
+    df = sparkSession.sql("SELECT ST_IsEmpty(point) from table")
+    val s = false
+    assert(df.first().get(0).asInstanceOf[Boolean] == s)
+  }
+
   it("handles nulls") {
     var functionDf: DataFrame = null
     functionDf = sparkSession.sql("select ST_Distance(null, null)")

Reply via email to