DRILL-5960: Add ST_AsGeoJSON functionality from PostGIS
This closes #1034
Project: http://git-wip-us.apache.org/repos/asf/drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/b8598b6b
Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/b8598b6b
Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/b8598b6b
Branch: refs/heads/master
Commit: b8598b6b69156b1a7ec984a4ffa2559658a1e98a
Parents: 0e4136f
Author: chris <chris@thinkdataworks.com>
Authored: Tue Nov 14 09:28:36 2017 -0500
Committer: Parth Chandra <parthc@apache.org>
Committed: Wed Nov 22 11:03:07 2017 -0800
----------------------------------------------------------------------
.../exec/expr/fn/impl/gis/STAsGeoJSON.java | 64 ++++++++++++++++++++
.../expr/fn/impl/gis/TestGeometryFunctions.java | 28 ++++++++-
2 files changed, 91 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/drill/blob/b8598b6b/contrib/gis/src/main/java/org/apache/drill/exec/expr/fn/impl/gis/STAsGeoJSON.java
----------------------------------------------------------------------
diff --git a/contrib/gis/src/main/java/org/apache/drill/exec/expr/fn/impl/gis/STAsGeoJSON.java
b/contrib/gis/src/main/java/org/apache/drill/exec/expr/fn/impl/gis/STAsGeoJSON.java
new file mode 100644
index 0000000..1c934bb
--- /dev/null
+++ b/contrib/gis/src/main/java/org/apache/drill/exec/expr/fn/impl/gis/STAsGeoJSON.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Wrapper for ESRI ST_AsGeoJson function to convert geometry to valid geojson
+ */
+package org.apache.drill.exec.expr.fn.impl.gis;
+
+import javax.inject.Inject;
+
+import org.apache.drill.exec.expr.DrillSimpleFunc;
+import org.apache.drill.exec.expr.annotations.FunctionTemplate;
+import org.apache.drill.exec.expr.annotations.Output;
+import org.apache.drill.exec.expr.annotations.Param;
+import org.apache.drill.exec.expr.holders.VarBinaryHolder;
+import org.apache.drill.exec.expr.holders.VarCharHolder;
+
+import io.netty.buffer.DrillBuf;
+
+@FunctionTemplate(name = "st_asgeojson", scope = FunctionTemplate.FunctionScope.SIMPLE,
+ nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
+// Naming convention from PostGIS documentation
+public class STAsGeoJSON implements DrillSimpleFunc {
+ @Param
+ VarBinaryHolder geomParam;
+
+ @Output
+ VarCharHolder out;
+
+ @Inject
+ DrillBuf buffer;
+
+ public void setup() {
+ }
+
+ public void eval() {
+ com.esri.core.geometry.ogc.OGCGeometry geom = com.esri.core.geometry.ogc.OGCGeometry
+ .fromBinary(geomParam.buffer.nioBuffer(geomParam.start, geomParam.end - geomParam.start));
+
+ String geoJson = geom.asGeoJson();
+ byte[] geoJsonBytes = geoJson.getBytes();
+ int outputSize = geoJsonBytes.length;
+
+ buffer = out.buffer = buffer.reallocIfNeeded(outputSize);
+ out.start = 0;
+ out.end = outputSize;
+ buffer.setBytes(0, geoJsonBytes);
+ }
+}
http://git-wip-us.apache.org/repos/asf/drill/blob/b8598b6b/contrib/gis/src/test/java/org/apache/drill/exec/expr/fn/impl/gis/TestGeometryFunctions.java
----------------------------------------------------------------------
diff --git a/contrib/gis/src/test/java/org/apache/drill/exec/expr/fn/impl/gis/TestGeometryFunctions.java
b/contrib/gis/src/test/java/org/apache/drill/exec/expr/fn/impl/gis/TestGeometryFunctions.java
index 63c9e07..e52291b 100644
--- a/contrib/gis/src/test/java/org/apache/drill/exec/expr/fn/impl/gis/TestGeometryFunctions.java
+++ b/contrib/gis/src/test/java/org/apache/drill/exec/expr/fn/impl/gis/TestGeometryFunctions.java
@@ -23,6 +23,8 @@ import org.junit.Test;
public class TestGeometryFunctions extends BaseTestQuery {
String wktPoint = "POINT (-121.895 37.339)";
+ String geoJson = "{\"type\":\"Point\",\"coordinates\":[-121.895,37.339],"
+ + "\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}}}";
@Test
public void testGeometryFromTextCreation() throws Exception {
@@ -49,6 +51,30 @@ public class TestGeometryFunctions extends BaseTestQuery {
}
@Test
+ public void testGeoJSONCreationFromPoint() throws Exception {
+ testBuilder()
+ .sqlQuery("select ST_AsGeoJSON(ST_Point(-121.895, 37.339)) "
+ + "from cp.`/sample-data/CA-cities.csv` limit 1")
+ .ordered()
+ .baselineColumns("EXPR$0")
+ .baselineValues(geoJson)
+ .build()
+ .run();
+ }
+
+ @Test
+ public void testGeoJSONCreationFromGeom() throws Exception {
+ testBuilder()
+ .sqlQuery("select ST_AsGeoJSON(ST_GeomFromText('" + wktPoint + "')) "
+ + "from cp.`/sample-data/CA-cities.csv` limit 1")
+ .ordered()
+ .baselineColumns("EXPR$0")
+ .baselineValues(geoJson)
+ .build()
+ .run();
+ }
+
+ @Test
public void testSTWithinQuery() throws Exception {
testBuilder()
@@ -72,4 +98,4 @@ public class TestGeometryFunctions extends BaseTestQuery {
.build()
.run();
}
-}
\ No newline at end of file
+}
|