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/16 21:20:35 UTC
[incubator-sedona] branch master updated: [SEDONA-144] Add ST_AsGeoJSON to the Flink API (#664)
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 67d1a41c [SEDONA-144] Add ST_AsGeoJSON to the Flink API (#664)
67d1a41c is described below
commit 67d1a41c28814d6cbc4f0e4665ba5463b55eb8ff
Author: Kengo Seki <se...@apache.org>
AuthorDate: Wed Aug 17 06:20:30 2022 +0900
[SEDONA-144] Add ST_AsGeoJSON to the Flink API (#664)
---
.../java/org/apache/sedona/common/Functions.java | 11 +++++++++--
docs/api/flink/Function.md | 14 +++++++++++++
.../main/java/org/apache/sedona/flink/Catalog.java | 1 +
.../apache/sedona/flink/expressions/Functions.java | 9 +++++++++
.../java/org/apache/sedona/flink/FunctionTest.java | 8 ++++++++
.../sql/sedona_sql/expressions/Functions.scala | 23 ++++------------------
6 files changed, 45 insertions(+), 21 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 7ce2dc30..a7777134 100644
--- a/common/src/main/java/org/apache/sedona/common/Functions.java
+++ b/common/src/main/java/org/apache/sedona/common/Functions.java
@@ -13,7 +13,6 @@
*/
package org.apache.sedona.common;
-import java.util.Optional;
import org.apache.sedona.common.utils.GeomUtils;
import org.apache.sedona.common.utils.GeometryGeoHashEncoder;
import org.geotools.geometry.jts.JTS;
@@ -25,7 +24,7 @@ import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
-
+import org.wololo.jts2geojson.GeoJSONWriter;
public class Functions {
@@ -118,6 +117,14 @@ public class Functions {
return GeomUtils.getEWKT(geometry);
}
+ public static String asGeoJson(Geometry geometry) {
+ if (geometry == null) {
+ return null;
+ }
+ GeoJSONWriter writer = new GeoJSONWriter();
+ return writer.write(geometry).toString();
+ }
+
public static Geometry force2D(Geometry geometry) {
return GeomUtils.get2dGeom(geometry);
}
diff --git a/docs/api/flink/Function.md b/docs/api/flink/Function.md
index bf5daf1b..181ddfa7 100644
--- a/docs/api/flink/Function.md
+++ b/docs/api/flink/Function.md
@@ -16,6 +16,20 @@ SELECT ST_AsEWKT(polygondf.countyshape)
FROM polygondf
```
+## ST_AsGeoJSON
+
+Introduction: Return the [GeoJSON](https://geojson.org/) string representation of a geometry
+
+Format: `ST_AsGeoJSON (A:geometry)`
+
+Since: `v1.3.0`
+
+Spark SQL example:
+```SQL
+SELECT ST_AsGeoJSON(polygondf.countyshape)
+FROM polygondf
+```
+
## ST_Buffer
Introduction: Returns a geometry/geography that represents all points whose distance from this Geometry/geography is less than or equal to distance.
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 3d8dd88a..1a71293b 100644
--- a/flink/src/main/java/org/apache/sedona/flink/Catalog.java
+++ b/flink/src/main/java/org/apache/sedona/flink/Catalog.java
@@ -40,6 +40,7 @@ public class Catalog {
new Functions.ST_PointN(),
new Functions.ST_ExteriorRing(),
new Functions.ST_AsEWKT(),
+ new Functions.ST_AsGeoJSON(),
new Functions.ST_Force_2D(),
new Functions.ST_IsEmpty(),
new Functions.ST_YMax(),
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 3a89b904..f1663e5c 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
@@ -26,6 +26,7 @@ import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
+import org.wololo.jts2geojson.GeoJSONWriter;
import scala.Option;
import static org.locationtech.jts.geom.Coordinate.NULL_ORDINATE;
@@ -132,6 +133,14 @@ public class Functions {
}
}
+ public static class ST_AsGeoJSON extends ScalarFunction {
+ @DataTypeHint("String")
+ public String eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o) {
+ Geometry geom = (Geometry) o;
+ return org.apache.sedona.common.Functions.asGeoJson(geom);
+ }
+ }
+
public static class ST_Force_2D 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) {
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 46b91b32..e188d277 100644
--- a/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
+++ b/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
@@ -154,6 +154,14 @@ public class FunctionTest extends TestBase{
assertEquals("POLYGON ((-0.5 -0.5, -0.5 0.5, 0.5 0.5, 0.5 -0.5, -0.5 -0.5))", result);
}
+ @Test
+ public void testGeoJSON() {
+ Table polygonTable = createPolygonTable(testDataSize);
+ polygonTable = polygonTable.select(call(Functions.ST_AsGeoJSON.class.getSimpleName(), $(polygonColNames[0])));
+ String result = (String) first(polygonTable).getField(0);
+ assertEquals("{\"type\":\"Polygon\",\"coordinates\":[[[-0.5,-0.5],[-0.5,0.5],[0.5,0.5],[0.5,-0.5],[-0.5,-0.5]]]}", result);
+ }
+
@Test
public void testForce2D() {
Table polygonTable = createPolygonTable(1);
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 a4097f18..fb71e249 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
@@ -19,26 +19,22 @@
package org.apache.spark.sql.sedona_sql.expressions
import org.apache.sedona.common.Functions
-import org.apache.sedona.common.utils.GeomUtils
import org.apache.sedona.core.geometryObjects.Circle
import org.apache.sedona.sql.utils.GeometrySerializer
import org.apache.spark.internal.Logging
import org.apache.spark.sql.catalyst.InternalRow
-import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, CodegenFallback, ExprCode}
+import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.util.{ArrayData, GenericArrayData}
import org.apache.spark.sql.sedona_sql.UDT.GeometryUDT
import org.apache.spark.sql.sedona_sql.expressions.collect.Collect
-import org.apache.spark.sql.sedona_sql.expressions.geohash.{GeoHashDecoder, InvalidGeoHashException}
import org.apache.spark.sql.sedona_sql.expressions.implicits._
import org.apache.spark.sql.sedona_sql.expressions.subdivide.GeometrySubDivider
-import org.apache.spark.sql.types.{ArrayType, _}
+import org.apache.spark.sql.types._
import org.apache.spark.unsafe.types.UTF8String
-import org.geotools.geometry.jts.JTS
-import org.geotools.referencing.CRS
import org.locationtech.jts.algorithm.MinimumBoundingCircle
import org.locationtech.jts.geom.util.GeometryFixer
-import org.locationtech.jts.geom.{PrecisionModel, _}
+import org.locationtech.jts.geom._
import org.locationtech.jts.io.{ByteOrderValues, WKBWriter, WKTWriter}
import org.locationtech.jts.linearref.LengthIndexedLine
import org.locationtech.jts.operation.IsSimpleOp
@@ -48,8 +44,6 @@ import org.locationtech.jts.operation.linemerge.LineMerger
import org.locationtech.jts.operation.valid.IsValidOp
import org.locationtech.jts.precision.GeometryPrecisionReducer
import org.locationtech.jts.simplify.TopologyPreservingSimplifier
-import org.opengis.referencing.operation.MathTransform
-import org.wololo.jts2geojson.GeoJSONWriter
import org.locationtech.jts.geom.Geometry
import org.locationtech.jts.geom.Coordinate
@@ -497,16 +491,7 @@ case class ST_AsText(inputExpressions: Seq[Expression])
}
case class ST_AsGeoJSON(inputExpressions: Seq[Expression])
- extends UnaryGeometryExpression with CodegenFallback {
-
- override protected def nullSafeEval(geometry: Geometry): Any = {
- val writer = new GeoJSONWriter()
- UTF8String.fromString(writer.write(geometry).toString)
- }
-
- override def dataType: DataType = StringType
-
- override def children: Seq[Expression] = inputExpressions
+ extends InferredUnaryExpression(Functions.asGeoJson) {
protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = {
copy(inputExpressions = newChildren)