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/sedona.git
The following commit(s) were added to refs/heads/master by this push:
new db3f643058 [GH-2255]add test for ST_GeogFromWKB(byte array) (#2256)
db3f643058 is described below
commit db3f643058c9dc0dd242f77925efa583e63e2af0
Author: Zhuocheng Shang <[email protected]>
AuthorDate: Sun Aug 10 16:14:52 2025 -0700
[GH-2255]add test for ST_GeogFromWKB(byte array) (#2256)
* add test for ST_GeogFromWKB(byte array)
* clean test code
* remove intercepted exception
---
.../sedona/common/Geography/ConstructorsTest.java | 68 ++++++++++++++++++++++
.../sedona_sql/expressions/st_constructors.scala | 10 ++--
.../geography/ConstructorsDataFrameAPITest.scala | 65 +++++++++++++++++++++
.../sedona/sql/geography/ConstructorsTest.scala | 23 ++++++++
4 files changed, 161 insertions(+), 5 deletions(-)
diff --git
a/common/src/test/java/org/apache/sedona/common/Geography/ConstructorsTest.java
b/common/src/test/java/org/apache/sedona/common/Geography/ConstructorsTest.java
new file mode 100644
index 0000000000..d1d949f1a7
--- /dev/null
+++
b/common/src/test/java/org/apache/sedona/common/Geography/ConstructorsTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.
+ */
+package org.apache.sedona.common.Geography;
+
+import static org.junit.Assert.assertEquals;
+
+import com.google.common.geometry.S2LatLng;
+import com.google.common.geometry.S2Point;
+import org.apache.sedona.common.S2Geography.Geography;
+import org.apache.sedona.common.S2Geography.SinglePointGeography;
+import org.apache.sedona.common.S2Geography.WKBWriter;
+import org.apache.sedona.common.geography.Constructors;
+import org.junit.Test;
+import org.locationtech.jts.io.ParseException;
+
+public class ConstructorsTest {
+
+ @Test
+ public void geogFromWKB() throws ParseException {
+ S2Point pt = S2LatLng.fromDegrees(45, -64).toPoint();
+ SinglePointGeography geog = new SinglePointGeography(pt);
+
+ // Test WKB without SRID
+ WKBWriter wkbWriter = new WKBWriter();
+ byte[] wkb = wkbWriter.write(geog);
+
+ Geography result = Constructors.geogFromWKB(wkb);
+ assertEquals(geog.toString(), result.toString());
+ assertEquals(0, result.getSRID());
+
+ // Test specifying SRID
+ result = Constructors.geogFromWKB(wkb, 1000);
+ assertEquals(geog.toString(), result.toString());
+ assertEquals(1000, result.getSRID());
+
+ // Test EWKB with SRID
+ wkbWriter = new WKBWriter(2, true);
+ geog.setSRID(2000);
+ wkb = wkbWriter.write(geog);
+ result = Constructors.geogFromWKB(wkb);
+ assertEquals(geog.toString(), result.toString());
+ assertEquals(2000, result.getSRID());
+
+ // Test overriding SRID
+ result = Constructors.geogFromWKB(wkb, 3000);
+ assertEquals(geog.toString(), result.toString());
+ assertEquals(3000, result.getSRID());
+ result = Constructors.geogFromWKB(wkb, 0);
+ assertEquals(geog.toString(), result.toString());
+ assertEquals(0, result.getSRID());
+ }
+}
diff --git
a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_constructors.scala
b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_constructors.scala
index 42ba815220..8921a2af70 100644
---
a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_constructors.scala
+++
b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_constructors.scala
@@ -102,11 +102,11 @@ object st_constructors {
wrapExpression[ST_GeogFromText](wkt, srid)
def ST_GeogFromText(wkt: String, srid: Int): Column =
wrapExpression[ST_GeogFromText](wkt, srid)
- def ST_GeogFromWKB(wkt: Column): Column =
wrapExpression[ST_GeogFromWKB](wkt, 0)
- def ST_GeogFromWKB(wkt: String): Column =
wrapExpression[ST_GeogFromWKB](wkt, 0)
- def ST_GeogFromWKB(wkt: Column, srid: Column): Column =
- wrapExpression[ST_GeogFromWKT](wkt, srid)
- def ST_GeogFromWKB(wkt: String, srid: Int): Column =
wrapExpression[ST_GeogFromWKB](wkt, srid)
+ def ST_GeogFromWKB(wkb: Column): Column =
wrapExpression[ST_GeogFromWKB](wkb, 0)
+ def ST_GeogFromWKB(wkb: String): Column =
wrapExpression[ST_GeogFromWKB](wkb, 0)
+ def ST_GeogFromWKB(wkb: Column, srid: Column): Column =
+ wrapExpression[ST_GeogFromWKB](wkb, srid)
+ def ST_GeogFromWKB(wkb: String, srid: Int): Column =
wrapExpression[ST_GeogFromWKB](wkb, srid)
def ST_LineFromText(wkt: Column): Column =
wrapExpression[ST_LineFromText](wkt)
def ST_LineFromText(wkt: String): Column =
wrapExpression[ST_LineFromText](wkt)
diff --git
a/spark/common/src/test/scala/org/apache/sedona/sql/geography/ConstructorsDataFrameAPITest.scala
b/spark/common/src/test/scala/org/apache/sedona/sql/geography/ConstructorsDataFrameAPITest.scala
new file mode 100644
index 0000000000..1208ecbf8f
--- /dev/null
+++
b/spark/common/src/test/scala/org/apache/sedona/sql/geography/ConstructorsDataFrameAPITest.scala
@@ -0,0 +1,65 @@
+/*
+ * 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.
+ */
+package org.apache.sedona.sql.geography
+
+import org.apache.sedona.common.S2Geography.{Geography, WKBReader}
+import org.apache.sedona.sql.TestBaseScala
+import org.apache.spark.sql.functions.col
+import org.apache.spark.sql.sedona_sql.expressions.{implicits, st_constructors}
+import org.junit.Assert.{assertEquals, assertFalse, assertTrue}
+import org.locationtech.jts.geom.PrecisionModel
+
+class ConstructorsDataFrameAPITest extends TestBaseScala {
+ import sparkSession.implicits._
+
+ it("Passed ST_GeogFromWKB point") {
+
+ val wkbSeq =
+ Seq(Array[Byte](1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 64, 0, 0, 0, 0, 0,
0, 46, 64))
+
+ val df =
wkbSeq.toDF("wkb").select(st_constructors.ST_GeogFromWKB(col("wkb")))
+ val actualResult = df.take(1)(0).get(0).asInstanceOf[Geography].toString()
+ val expectedResult = "POINT (10 15)"
+ assert(actualResult == expectedResult)
+ }
+
+ it("passed ST_GeogFromWKB linestring") {
+ val wkbSeq = Seq[Array[Byte]](
+ Array[Byte](1, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, -124, -42, 0, -64, 0,
0, 0, 0, -128, -75,
+ -42, -65, 0, 0, 0, 96, -31, -17, -9, -65, 0, 0, 0, -128, 7, 93, -27,
-65))
+ val df =
wkbSeq.toDF("wkb").select(st_constructors.ST_GeogFromWKB(col("wkb")))
+ val actualResult = df.take(1)(0).get(0).asInstanceOf[Geography].toString()
+ val expectedResult =
+ "LINESTRING (-2.1 -0.4, -1.5 -0.7)"
+ assert(actualResult == expectedResult)
+ }
+
+ it("passed ST_GeogFromWKB collection") {
+ val hexStr =
+
"0107000020E6100000090000000101000020E61000000000000000000000000000000000F03F0101000020E61000000000000000000000000000000000F03F0101000020E6100000000000000000004000000000000008400102000020E61000000200000000000000000000400000000000000840000000000000104000000000000014400102000020E6100000020000000000000000000000000000000000F03F000000000000004000000000000008400102000020E6100000020000000000000000001040000000000000144000000000000018400000000000001C400103000020E6100000020000000500000000000
[...]
+ val array = WKBReader.hexToBytes(hexStr)
+ val wkbSeq = Seq[Array[Byte]](array)
+ val df =
wkbSeq.toDF("wkb").select(st_constructors.ST_GeogFromWKB(col("wkb")))
+ val actualResult = df.take(1)(0).get(0).asInstanceOf[Geography].toString()
+ val expectedResult =
+ "GEOMETRYCOLLECTION (POINT (0 1), POINT (0 1), POINT (2 3), LINESTRING
(2 3, 4 5), LINESTRING (0 1, 2 3), LINESTRING (4 5, 6 7), POLYGON ((0 0, 0 10,
10 10, 10 0, 0 0), (9 1, 9 9, 1 9, 1 1, 9 1)), POLYGON ((0 0, 0 10, 10 10, 10
0, 0 0), (9 1, 9 9, 1 9, 1 1, 9 1)), POLYGON ((-9 0, -9 10, -1 10, -1 0, -9
0)))"
+ assert(actualResult == expectedResult)
+ }
+
+}
diff --git
a/spark/common/src/test/scala/org/apache/sedona/sql/geography/ConstructorsTest.scala
b/spark/common/src/test/scala/org/apache/sedona/sql/geography/ConstructorsTest.scala
index 994cfd7ccb..8efb45f51b 100644
---
a/spark/common/src/test/scala/org/apache/sedona/sql/geography/ConstructorsTest.scala
+++
b/spark/common/src/test/scala/org/apache/sedona/sql/geography/ConstructorsTest.scala
@@ -73,4 +73,27 @@ class ConstructorsTest extends TestBaseScala {
"GEOMETRYCOLLECTION (POINT (50 50), LINESTRING (20 30, 40 60, 80 90),
POLYGON ((35 15, 45 15, 40 25, 35 15), (30 10, 40 20, 30 20, 30 10)))"
assert(expected.equals(actual))
}
+
+ it("Passed ST_GeogFromWKB") {
+ // RAW binary array
+ val wkbSeq = Seq[Array[Byte]](
+ Array[Byte](1, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, -124, -42, 0, -64, 0,
0, 0, 0, -128, -75,
+ -42, -65, 0, 0, 0, 96, -31, -17, -9, -65, 0, 0, 0, -128, 7, 93, -27,
-65))
+ val rawWkbDf = wkbSeq.toDF("wkb")
+ rawWkbDf.createOrReplaceTempView("rawWKBTable")
+ val geometries = {
+ sparkSession.sql("SELECT ST_GeogFromWKB(rawWKBTable.wkb) as countyshape
from rawWKBTable")
+ }
+ val expectedGeom =
+ "LINESTRING (-2.1047439575195317 -0.35482788085937506,
-1.4960645437240603 -0.6676061153411864)"
+ assert(
+ geometries
+ .first()
+ .getAs[Geography](0)
+ .toString(new PrecisionModel(1e16))
+ .equals(expectedGeom))
+ // null input
+ val nullGeom = sparkSession.sql("SELECT ST_GeogFromWKB(null)")
+ assert(nullGeom.first().isNullAt(0))
+ }
}