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 7b9396d6 [SEDONA-199] Added ST_NDims to Apache Sedona (#720)
7b9396d6 is described below

commit 7b9396d6bb3d083bec1fccfdb98d2007fcfe9df9
Author: AMRIT BHASKAR <[email protected]>
AuthorDate: Sat Dec 3 12:27:54 2022 -0700

    [SEDONA-199] Added ST_NDims to Apache Sedona (#720)
    
    Co-authored-by: Bhanu Tokas <[email protected]>
    Co-authored-by: sanket-19 <[email protected]>
    Co-authored-by: snjoshi3 <[email protected]>
    Co-authored-by: Bhanu Tokas <[email protected]>
---
 .../java/org/apache/sedona/common/Functions.java   | 18 ++++++++++++++++
 docs/api/flink/Function.md                         | 24 ++++++++++++++++++++++
 docs/api/sql/Function.md                           | 23 +++++++++++++++++++++
 .../main/java/org/apache/sedona/flink/Catalog.java |  1 +
 .../apache/sedona/flink/expressions/Functions.java |  7 +++++++
 .../java/org/apache/sedona/flink/FunctionTest.java | 15 ++++++++++++++
 python/sedona/sql/st_functions.py                  | 13 ++++++++++++
 python/tests/sql/test_function.py                  |  6 ++++++
 .../scala/org/apache/sedona/sql/UDF/Catalog.scala  |  1 +
 .../sql/sedona_sql/expressions/Functions.scala     | 13 ++++++++++++
 .../sql/sedona_sql/expressions/st_functions.scala  |  3 +++
 .../org/apache/sedona/sql/functionTestScala.scala  | 15 ++++++++++++++
 12 files changed, 139 insertions(+)

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 6ab744ca..57abcd0c 100644
--- a/common/src/main/java/org/apache/sedona/common/Functions.java
+++ b/common/src/main/java/org/apache/sedona/common/Functions.java
@@ -280,6 +280,24 @@ public class Functions {
         return geometry.getNumPoints();
     }
 
+    public static int nDims(Geometry geometry) {
+        int count_dimension =0;
+        Coordinate geom = geometry.getCoordinate();
+        Double x_cord = geom.getX();
+        Double y_cord = geom.getY();
+        Double z_cord = geom.getZ();
+        Double m_cord = geom.getM();
+        if(!java.lang.Double.isNaN(x_cord))
+            count_dimension++;
+        if(!java.lang.Double.isNaN(y_cord))
+            count_dimension++;
+        if(!java.lang.Double.isNaN(z_cord))
+            count_dimension++;
+        if(!java.lang.Double.isNaN(m_cord))
+            count_dimension++;
+        return count_dimension;
+    }
+
     public static int numGeometries(Geometry geometry) {
         return geometry.getNumGeometries();
     }
diff --git a/docs/api/flink/Function.md b/docs/api/flink/Function.md
index 1a630cf9..5acc7c14 100644
--- a/docs/api/flink/Function.md
+++ b/docs/api/flink/Function.md
@@ -513,6 +513,30 @@ SELECT ST_NPoints(polygondf.countyshape)
 FROM polygondf
 ```
 
+## ST_NDims
+
+Introduction: Returns the coordinate dimension of the geometry. It supports 2 
- (x,y) , 3 - (x,y,z). Currently the geometry serializer in sedona-sql does not 
support M dimension, 4D geometries with ZM coordinates will have their M 
coordinates dropped and became 3D geometries. We're working on a new geometry 
serializer to resolve this issue.
+
+Format: `ST_NDims(geom: geometry)`
+
+Since: `v1.3.1`
+
+Spark SQL example with z co-rodinate:
+
+```SQL
+SELECT ST_NDims(ST_GeomFromEWKT('POINT(1 1 2)'))
+```
+
+Output: `3`
+
+Spark SQL example with x,y co-ordinate:
+
+```SQL
+SELECT ST_NDims(ST_GeomFromText('POINT(1 1)'))
+```
+
+Output: `2`
+
 ## ST_NumGeometries
 
 Introduction: Returns the number of Geometries. If geometry is a 
GEOMETRYCOLLECTION (or MULTI*) return the number of geometries, for single 
geometries will return 1.
diff --git a/docs/api/sql/Function.md b/docs/api/sql/Function.md
index df3982cd..37e41681 100644
--- a/docs/api/sql/Function.md
+++ b/docs/api/sql/Function.md
@@ -880,6 +880,29 @@ Result:
 |MULTIPOINT (1 1)                                               |
 +---------------------------------------------------------------+
 ```
+## ST_NDims
+
+Introduction: Returns the coordinate dimension of the geometry. It supports 2 
- (x,y) , 3 - (x,y,z). Currently the geometry serializer in sedona-sql does not 
support M dimension, 4D geometries with ZM coordinates will have their M 
coordinates dropped and became 3D geometries. We're working on a new geometry 
serializer to resolve this issue.
+
+Format: `ST_NDims(geom: geometry)`
+
+Since: `v1.3.1`
+
+Spark SQL example with z co-rodinate:
+
+```SQL
+SELECT ST_NDims(ST_GeomFromEWKT('POINT(1 1 2)'))
+```
+
+Output: `3`
+
+Spark SQL example with x,y co-ordinate:
+
+```SQL
+SELECT ST_NDims(ST_GeomFromText('POINT(1 1)'))
+```
+
+Output: `2`
 
 ## ST_Normalize
 
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 24c892ba..f290361d 100644
--- a/flink/src/main/java/org/apache/sedona/flink/Catalog.java
+++ b/flink/src/main/java/org/apache/sedona/flink/Catalog.java
@@ -72,6 +72,7 @@ public class Catalog {
                 new Functions.ST_XMin(),
                 new Functions.ST_ZMax(),
                 new Functions.ST_ZMin(),
+                new Functions.ST_NDims(),
                 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 d3ec3f9d..6d046f4c 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,6 +123,13 @@ public class Functions {
         }
     }
 
+    public static class ST_NDims 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.nDims(geom);
+        }
+    }
 
     public static class ST_Transform extends ScalarFunction {
         @DataTypeHint(value = "RAW", bridgedTo = 
org.locationtech.jts.geom.Geometry.class)
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 95718f22..2c212231 100644
--- a/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
+++ b/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
@@ -404,6 +404,21 @@ public class FunctionTest extends TestBase{
         assertNull(first(polygonTable).getField(0));
     }
 
+    @Test
+    public void testNDimsFor2D() {
+        Table polygonTable = tableEnv.sqlQuery("SELECT ST_GeomFromWKT('POINT(1 
1)') AS " + polygonColNames[0]);
+        polygonTable = 
polygonTable.select(call(Functions.ST_NDims.class.getSimpleName(), 
$(polygonColNames[0])));
+        int result = (int) first(polygonTable).getField(0);
+        assertEquals(2, result, 0);
+    }
+
+    @Test
+    public void testNDims() {
+        Table polygonTable = tableEnv.sqlQuery("SELECT ST_GeomFromWKT('POINT(1 
1 2)') AS " + polygonColNames[0]);
+        polygonTable = 
polygonTable.select(call(Functions.ST_NDims.class.getSimpleName(), 
$(polygonColNames[0])));
+        int result = (int) first(polygonTable).getField(0);
+        assertEquals(3, result, 0);
+    }
     @Test
     public void testXMax() {
         Table polygonTable = createPolygonTable(1);
diff --git a/python/sedona/sql/st_functions.py 
b/python/sedona/sql/st_functions.py
index ae4c8daf..4de6e050 100644
--- a/python/sedona/sql/st_functions.py
+++ b/python/sedona/sql/st_functions.py
@@ -73,6 +73,7 @@ __all__ = [
     "ST_Multi",
     "ST_Normalize",
     "ST_NPoints",
+    "ST_NDims",
     "ST_NumGeometries",
     "ST_NumInteriorRings",
     "ST_PointN",
@@ -757,6 +758,18 @@ def ST_NPoints(geometry: ColumnOrName) -> Column:
     return _call_st_function("ST_NPoints", geometry)
 
 
+@validate_argument_types
+def ST_NDims(geometry: ColumnOrName) -> Column:
+    """Return the number of dimensions contained in a geometry.
+
+    :param geometry: Geometry column to return for.
+    :type geometry: ColumnOrName
+    :return: Number of dimensions in a geometry column as an integer column.
+    :rtype: Column
+    """
+    return _call_st_function("ST_NDims", geometry)
+
+
 @validate_argument_types
 def ST_NumGeometries(geometry: ColumnOrName) -> Column:
     """Return the number of geometries contained in a multi-geometry.
diff --git a/python/tests/sql/test_function.py 
b/python/tests/sql/test_function.py
index 275bfcf6..5fba932d 100644
--- a/python/tests/sql/test_function.py
+++ b/python/tests/sql/test_function.py
@@ -427,6 +427,12 @@ class TestPredicateJoin(TestBase):
         linestring_row = [lnstr_row[0] for lnstr_row in 
linestring_df.selectExpr("ST_ZMin(geom)").collect()]
         assert(linestring_row == [1.0])
 
+    def test_st_n_dims(self):
+        point_df = self.spark.sql("SELECT ST_GeomFromWKT('POINT(1 1 2)') as 
geom")
+        point_row = [pt_row[0] for pt_row in 
point_df.selectExpr("ST_NDims(geom)").collect()]
+        assert(point_row == [3])
+
+
     def test_st_start_point(self):
 
         point_df = create_sample_points_df(self.spark, 5)
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 11a1fe04..bd912feb 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
@@ -58,6 +58,7 @@ object Catalog {
     function[ST_3DDistance](),
     function[ST_ConvexHull](),
     function[ST_NPoints](),
+    function[ST_NDims](),
     function[ST_Buffer](),
     function[ST_Envelope](),
     function[ST_Length](),
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 fb0875ea..6ff5361c 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
@@ -124,6 +124,19 @@ case class ST_NPoints(inputExpressions: Seq[Expression])
   }
 }
 
+/**
+  * Return the number of Dimensions in geometry.
+  *
+  * @param inputExpressions
+  */
+case class ST_NDims(inputExpressions: Seq[Expression])
+  extends InferredUnaryExpression(Functions.nDims) with FoldableExpression {
+
+  protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = 
{
+    copy(inputExpressions = newChildren)
+  }
+}
+
 /**
   * Returns a geometry/geography that represents all points whose distance 
from this Geometry/geography is less than or equal to distance.
   *
diff --git 
a/sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_functions.scala
 
b/sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_functions.scala
index 694f067c..90de477c 100644
--- 
a/sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_functions.scala
+++ 
b/sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_functions.scala
@@ -181,6 +181,9 @@ object st_functions extends DataFrameAPI {
   def ST_NPoints(geometry: Column): Column = 
wrapExpression[ST_NPoints](geometry)
   def ST_NPoints(geometry: String): Column = 
wrapExpression[ST_NPoints](geometry)
 
+  def ST_NDims(geometry: Column): Column = wrapExpression[ST_NDims](geometry)
+  def ST_NDims(geometry: String): Column = wrapExpression[ST_NDims](geometry)
+
   def ST_NumGeometries(geometry: Column): Column = 
wrapExpression[ST_NumGeometries](geometry)
   def ST_NumGeometries(geometry: String): Column = 
wrapExpression[ST_NumGeometries](geometry)
 
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 02c092cd..5a28cdcc 100644
--- a/sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala
+++ b/sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala
@@ -459,6 +459,21 @@ class functionTestScala extends TestBaseScala with 
Matchers with GeometrySample
 
     }
 
+    it("Passed ST_NDims with 2D point") {
+      val test = sparkSession.sql("SELECT ST_NDims(ST_GeomFromWKT('POINT(1 
1)'))")
+      assert(test.take(1)(0).get(0).asInstanceOf[Int] == 2)
+    }
+
+    it("Passed ST_NDims with 3D point") {
+      val test = sparkSession.sql("SELECT ST_NDims(ST_GeomFromWKT('POINT(1 1 
2)'))")
+      assert(test.take(1)(0).get(0).asInstanceOf[Int] == 3)
+    }
+
+    it("Passed ST_NDims with Z coordinates") {
+      val test = sparkSession.sql("SELECT ST_NDims(ST_GeomFromWKT('POINTZ(1 1 
0.5)'))")
+      assert(test.take(1)(0).get(0).asInstanceOf[Int] == 3)
+    }
+
     it("Passed ST_GeometryType") {
       var test = sparkSession.sql("SELECT 
ST_GeometryType(ST_GeomFromText('LINESTRING(77.29 29.07,77.42 29.26,77.27 
29.31,77.29 29.07)'))")
       assert(test.take(1)(0).get(0).asInstanceOf[String].toUpperCase() == 
"ST_LINESTRING")

Reply via email to