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 4e22dac8 [SEDONA-115] Add ST_XMax and ST_XMin function (#622)
4e22dac8 is described below

commit 4e22dac89cd3035ef0379df8b2455f570865d320
Author: Venkata Pankaj Kodavandlapalli 
<[email protected]>
AuthorDate: Sat Apr 23 19:55:03 2022 -0700

    [SEDONA-115] Add ST_XMax and ST_XMin function (#622)
    
    Co-authored-by: Venkata Pankaj Kodavandlapalli 
<[email protected]>
---
 docs/api/flink/Function.md                         | 40 +++++++++++++-
 docs/api/sql/Function.md                           | 40 +++++++++++++-
 .../main/java/org/apache/sedona/flink/Catalog.java |  4 +-
 .../apache/sedona/flink/expressions/Functions.java | 30 +++++++++++
 .../java/org/apache/sedona/flink/FunctionTest.java | 16 ++++++
 .../scala/org/apache/sedona/sql/UDF/Catalog.scala  |  2 +
 .../sql/sedona_sql/expressions/Functions.scala     | 62 ++++++++++++++++++++++
 .../org/apache/sedona/sql/functionTestScala.scala  | 12 +++++
 8 files changed, 203 insertions(+), 3 deletions(-)

diff --git a/docs/api/flink/Function.md b/docs/api/flink/Function.md
index 8d2ba930..cec8e180 100644
--- a/docs/api/flink/Function.md
+++ b/docs/api/flink/Function.md
@@ -230,4 +230,42 @@ Spark SQL example:
 ```SQL
 SELECT ST_AsEWKT(polygondf.countyshape)
 FROM polygondf
-```
\ No newline at end of file
+```
+
+## ST_XMax
+
+Introduction: Returns the maximum X coordinate of a geometry
+
+Format: `ST_XMax (A:geometry)`
+
+Since: `v1.2.1`
+
+Example:
+
+```SQL
+SELECT ST_XMax(df.geometry) AS xmax
+FROM df
+```
+
+Input: `POLYGON ((-1 -11, 0 10, 1 11, 2 12, -1 -11))`
+
+Output: `2`
+
+## ST_XMin
+
+Introduction: Returns the minimum X coordinate of a geometry
+
+Format: `ST_XMin (A:geometry)`
+
+Since: `v1.2.1`
+
+Example:
+
+```SQL
+SELECT ST_XMin(df.geometry) AS xmin
+FROM df
+```
+
+Input: `POLYGON ((-1 -11, 0 10, 1 11, 2 12, -1 -11))`
+
+Output: `-1`
\ No newline at end of file
diff --git a/docs/api/sql/Function.md b/docs/api/sql/Function.md
index 1080f1d7..1833b9dd 100644
--- a/docs/api/sql/Function.md
+++ b/docs/api/sql/Function.md
@@ -1177,4 +1177,42 @@ Result:
 +---------------------------------------------------------------+
 |POLYGON((0 0,0 5,5 0,0 0),(1 1,3 1,1 3,1 1))                                |
 +---------------------------------------------------------------+
-```
\ No newline at end of file
+```
+
+## ST_XMax
+
+Introduction: Returns the maximum X coordinate of a geometry
+
+Format: `ST_XMax (A:geometry)`
+
+Since: `v1.2.1`
+
+Example:
+
+```SQL
+SELECT ST_XMax(df.geometry) AS xmax
+FROM df
+```
+
+Input: `POLYGON ((-1 -11, 0 10, 1 11, 2 12, -1 -11))`
+
+Output: `2`
+
+## ST_XMin
+
+Introduction: Returns the minimum X coordinate of a geometry
+
+Format: `ST_XMin (A:geometry)`
+
+Since: `v1.2.1`
+
+Example:
+
+```SQL
+SELECT ST_XMin(df.geometry) AS xmin
+FROM df
+```
+
+Input: `POLYGON ((-1 -11, 0 10, 1 11, 2 12, -1 -11))`
+
+Output: `-1`
\ No newline at end of file
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 6fe3ab43..22374d48 100644
--- a/flink/src/main/java/org/apache/sedona/flink/Catalog.java
+++ b/flink/src/main/java/org/apache/sedona/flink/Catalog.java
@@ -37,7 +37,9 @@ public class Catalog {
                 new Functions.ST_Force_2D(),
                 new Functions.ST_IsEmpty(),
                 new Functions.ST_YMax(),
-                new Functions.ST_YMin()
+                new Functions.ST_YMin(),
+                new Functions.ST_XMax(),
+                new Functions.ST_XMin(),
         };
     }
 
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 46b4c713..0d9d4e4e 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
@@ -159,4 +159,34 @@ public class Functions {
             return geom.isEmpty();
         }
     }
+
+    public static class ST_XMax extends ScalarFunction {
+        @DataTypeHint("Double")
+        public Double eval(@DataTypeHint(value = "RAW", bridgedTo = 
org.locationtech.jts.geom.Geometry.class) Object o) {
+            Geometry geom = (Geometry) o;
+            Coordinate[] coord = geom.getCoordinates();
+            double max = Double.MIN_VALUE;
+            for (int i = 0; i < coord.length; i++) {
+                if (coord[i].getX() > max) {
+                    max = coord[i].getX();
+                }
+            }
+            return max;
+        }
+    }
+
+    public static class ST_XMin extends ScalarFunction {
+        @DataTypeHint("Double")
+        public Double eval(@DataTypeHint(value = "RAW", bridgedTo = 
org.locationtech.jts.geom.Geometry.class) Object o) {
+            Geometry geom = (Geometry) o;
+            Coordinate[] coord = geom.getCoordinates();
+            double min = Double.MAX_VALUE;
+            for(int i=0;i< coord.length;i++){
+                if(coord[i].getX()<min){
+                    min = coord[i].getX();
+                }
+            }
+            return min;
+        }
+    }
 }
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 f4901e69..c23f84a5 100644
--- a/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
+++ b/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
@@ -128,5 +128,21 @@ public class FunctionTest extends TestBase{
         boolean result = (boolean) first(polygonTable).getField(0);
         assertEquals(false, result);
     }
+
+    @Test
+    public void testXMax() {
+        Table polygonTable = createPolygonTable(1);
+        Table MaxTable = 
polygonTable.select(call(Functions.ST_XMax.class.getSimpleName(), 
$(polygonColNames[0])));
+        double result = (double) first(MaxTable).getField(0);
+        assertEquals(0.5, result,0);
+    }
+
+    @Test
+    public void testXMin() {
+        Table polygonTable = createPolygonTable(1);
+        Table MinTable = 
polygonTable.select(call(Functions.ST_XMin.class.getSimpleName(), 
$(polygonColNames[0])));
+        double result = (double) first(MinTable).getField(0);
+        assertEquals(-0.5, result,0);
+    }
 }
 
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 882ed307..db112709 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
@@ -110,6 +110,8 @@ object Catalog {
     ST_Force_2D,
     ST_YMax,
     ST_YMin,
+    ST_XMax,
+    ST_XMin,
     // Expression for rasters
     RS_NormalizedDifference,
     RS_Mean,
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 ffc33275..4612465f 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
@@ -1684,3 +1684,65 @@ case class ST_IsEmpty(inputExpressions: Seq[Expression])
     copy(inputExpressions = newChildren)
   }
 }
+
+/**
+ * Test if returning Max X coordinate value.
+ *
+ * @param inputExpressions
+ */
+case class ST_XMax(inputExpressions: Seq[Expression])
+  extends UnaryGeometryExpression with CodegenFallback {
+  assert(inputExpressions.length == 1)
+
+
+  override protected def nullSafeEval(geometry: Geometry): Any = {
+    var coord:Array[Coordinate] = geometry.getCoordinates()
+    var maxval = Double.MinValue
+    for (point<-coord) {
+      if(point.getX()>maxval){
+        maxval = point.getX()
+      }
+    }
+    maxval
+
+  }
+
+  override def dataType: DataType = DoubleType
+
+  override def children: Seq[Expression] = inputExpressions
+
+  protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = 
{
+    copy(inputExpressions = newChildren)
+  }
+}
+
+/**
+ * Test if returning Min X coordinate value.
+ *
+ * @param inputExpressions
+ */
+case class ST_XMin(inputExpressions: Seq[Expression])
+  extends UnaryGeometryExpression with CodegenFallback {
+  assert(inputExpressions.length == 1)
+
+
+  override protected def nullSafeEval(geometry: Geometry): Any = {
+    var coord:Array[Coordinate] = geometry.getCoordinates()
+    var minval = Double.MaxValue
+    for (point<-coord) {
+      if(point.getX()<minval){
+        minval = point.getX()
+      }
+    }
+    minval
+
+  }
+
+  override def dataType: DataType = DoubleType
+
+  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 ff6bce37..397a1f7b 100644
--- a/sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala
+++ b/sql/src/test/scala/org/apache/sedona/sql/functionTestScala.scala
@@ -1379,6 +1379,18 @@ class functionTestScala extends TestBaseScala with 
Matchers with GeometrySample
     assert(df.first().get(0).asInstanceOf[Boolean] == s)
   }
 
+  it("Passed ST_XMax") {
+    var test = sparkSession.sql("SELECT ST_XMax(ST_GeomFromWKT('POLYGON ((-1 
-11, 0 10, 1 11, 2 12, -1 -11))'))")
+    assert(test.take(1)(0).get(0).asInstanceOf[Double] == 2.0)
+
+  }
+
+  it("Passed ST_XMin") {
+    var test = sparkSession.sql("SELECT ST_XMin(ST_GeomFromWKT('POLYGON ((-1 
-11, 0 10, 1 11, 2 12, -1 -11))'))")
+    assert(test.take(1)(0).get(0).asInstanceOf[Double] == -1.0)
+
+  }
+
   it("handles nulls") {
     var functionDf: DataFrame = null
     functionDf = sparkSession.sql("select ST_Distance(null, null)")

Reply via email to