From a2ab2c80cbc365a780c801527363bcd9da3dc8ff Mon Sep 17 00:00:00 2001 From: Simen Omholt-Jensen Date: Thu, 29 Oct 2026 08:58:29 +0100 Subject: [PATCH] [GH-3511] Add ST_H3ToPoint --- .../org/apache/sedona/common/Functions.java | 14 ++++++ .../apache/sedona/common/FunctionsTest.java | 35 +++++++++++++++ docs/api/flink/Geometry-Functions.md | 1 + .../flink/Spatial-Indexing/ST_H3ToPoint.md | 44 +++++++++++++++++++ docs/api/sql/Geometry-Functions.md | 1 + docs/api/sql/Spatial-Indexing/ST_H3ToPoint.md | 44 +++++++++++++++++++ .../java/org/apache/sedona/flink/Catalog.java | 1 + .../sedona/flink/expressions/Functions.java | 13 ++++++ .../org/apache/sedona/flink/FunctionTest.java | 16 +++++++ python/sedona/spark/sql/st_functions.py | 11 +++++ python/tests/sql/test_dataframe_api.py | 16 +++++++ .../org/apache/sedona/sql/UDF/Catalog.scala | 1 + .../sedona_sql/expressions/Functions.scala | 9 ++++ .../sedona_sql/expressions/st_functions.scala | 4 ++ .../sedona/sql/dataFrameAPITestScala.scala | 14 ++++++ .../sedona/sql/functions/STH3Functions.scala | 27 ++++++++++++ 16 files changed, 251 insertions(+) create mode 100644 docs/api/flink/Spatial-Indexing/ST_H3ToPoint.md create mode 100644 docs/api/sql/Spatial-Indexing/ST_H3ToPoint.md 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 a45ad8c2ee0..b7ae9a2a4df 100644 --- a/common/src/main/java/org/apache/sedona/common/Functions.java +++ b/common/src/main/java/org/apache/sedona/common/Functions.java @@ -2125,6 +2125,20 @@ public static Long[] h3Uncompact(long[] cells, int resolution) { .toArray(new Long[0]); } + /** + * Returns the H3 cell center in longitude/latitude order, with SRID 0 as in h3ToGeom. + * + * @param cell H3 cell index + * @return the center point in degrees + * @throws IllegalArgumentException if cell is not a valid H3 cell index + */ + public static Geometry h3ToPoint(long cell) { + if (!H3Utils.h3.isValidCell(cell)) { + throw new IllegalArgumentException("Invalid H3 cell index: " + cell); + } + return new GeometryFactory().createPoint(H3Utils.cellToCoordinate(cell)); + } + /** * gets the polygon for each h3 index provided * diff --git a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java index 1b0c6c97fac..bebc5c52afe 100644 --- a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java +++ b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java @@ -1467,6 +1467,41 @@ public void testUnaryUnion() throws ParseException { assertEquals(expected, actual); } + @Test + public void h3ToPoint() { + Geometry center = Functions.h3ToPoint(0x8928308280fffffL); + assertTrue(center instanceof Point); + assertEquals(-122.41845932318309, center.getCoordinate().x, 1e-10); + assertEquals(37.776702349435695, center.getCoordinate().y, 1e-10); + assertEquals(0, center.getSRID()); + } + + @Test + public void h3ToPointAcrossResolutions() { + for (int resolution = 0; resolution <= 15; resolution++) { + long cell = H3Utils.coordinateToCell(new Coordinate(10.75, 59.91), resolution); + Geometry center = Functions.h3ToPoint(cell); + assertEquals(cell, H3Utils.coordinateToCell(center.getCoordinate(), resolution)); + for (long pentagon : H3Utils.h3.getPentagons(resolution)) { + Geometry pentagonCenter = Functions.h3ToPoint(pentagon); + assertEquals( + pentagon, H3Utils.coordinateToCell(pentagonCenter.getCoordinate(), resolution)); + assertEquals(0, pentagonCenter.getSRID()); + } + } + } + + @Test + public void h3ToPointUsesH3Center() { + long cell = H3Utils.coordinateToCell(new Coordinate(10.75, 59.91), 0); + Geometry center = Functions.h3ToPoint(cell); + Geometry polygon = Functions.h3ToGeom(new long[] {cell})[0]; + assertTrue(center.distance(polygon.getCentroid()) > 1e-6); + assertEquals(polygon.getSRID(), center.getSRID()); + assertThrows(IllegalArgumentException.class, () -> Functions.h3ToPoint(0L)); + assertThrows(IllegalArgumentException.class, () -> Functions.h3ToPoint(-1L)); + } + @Test public void testH3ToGeom() { Geometry target = diff --git a/docs/api/flink/Geometry-Functions.md b/docs/api/flink/Geometry-Functions.md index 164711a2433..5ff688d6769 100644 --- a/docs/api/flink/Geometry-Functions.md +++ b/docs/api/flink/Geometry-Functions.md @@ -355,6 +355,7 @@ These functions work with spatial indexing systems including Bing Tiles, H3, Hil | [ST_H3ToChildren](Spatial-Indexing/ST_H3ToChildren.md) | `Array` | Returns the children of an H3 cell at the requested resolution. | v2.0.0 | | [ST_H3ToGeom](Spatial-Indexing/ST_H3ToGeom.md) | `Array` | Return the result of H3 function [cellsToMultiPolygon(cells)](https://h3geo.org/docs/api/regions#cellstolinkedmultipolygon--cellstomultipolygon). | v1.6.0 | | [ST_H3ToParent](Spatial-Indexing/ST_H3ToParent.md) | Long | Returns the parent of an H3 cell at the requested resolution. | v2.0.0 | +| [ST_H3ToPoint](Spatial-Indexing/ST_H3ToPoint.md) | Geometry | Returns the center of an H3 cell as a point in longitude/latitude order with SRID 0. | v2.0.0 | | [ST_H3Uncompact](Spatial-Indexing/ST_H3Uncompact.md) | `Array` | Expands H3 cells to the requested resolution. | v2.0.0 | | [ST_HilbertDistance](Spatial-Indexing/ST_HilbertDistance.md) | Long | Maps the midpoint of a geometry's envelope to an unsigned Hilbert-curve address within a supplied extent. | v2.0.0 | | [ST_S2CellIDs](Spatial-Indexing/ST_S2CellIDs.md) | `Array` | Cover the geometry with Google S2 Cells, return the corresponding cell IDs with the given level. The level indicates the [size of cells](https://s2geometry.io/resources/s2cell_statistics.html). With... | v1.4.0 | diff --git a/docs/api/flink/Spatial-Indexing/ST_H3ToPoint.md b/docs/api/flink/Spatial-Indexing/ST_H3ToPoint.md new file mode 100644 index 00000000000..55772c57c18 --- /dev/null +++ b/docs/api/flink/Spatial-Indexing/ST_H3ToPoint.md @@ -0,0 +1,44 @@ + + +# ST_H3ToPoint + +Introduction: Returns the center of an H3 cell as a point geometry. This uses H3's cell center, which can differ from the centroid of the cell polygon because of the grid projection. + +Coordinates are longitude/latitude (X/Y) in degrees. The result has SRID 0, consistent with `ST_H3ToGeom`. To assign WGS 84, use `ST_SetSRID(ST_H3ToPoint(cell), 4326)`. + +Null input returns null. Invalid cell indexes raise an error. + +Format: `ST_H3ToPoint(cell: Long)` + +Return type: `Geometry` (Point) + +Since: `v2.0.0` + +SQL Example + +```sql +SELECT ST_AsText(ST_H3ToPoint(617700169958293503)) +``` + +Output (coordinates rounded): + +```text +POINT (-122.41845932318309 37.776702349435695) +``` diff --git a/docs/api/sql/Geometry-Functions.md b/docs/api/sql/Geometry-Functions.md index dcada7cbe4c..54bda54da81 100644 --- a/docs/api/sql/Geometry-Functions.md +++ b/docs/api/sql/Geometry-Functions.md @@ -363,6 +363,7 @@ These functions work with spatial indexing systems including Bing Tiles, H3, Hil | [ST_H3ToChildren](Spatial-Indexing/ST_H3ToChildren.md) | `Array` | Returns the children of an H3 cell at the requested resolution. | v2.0.0 | | [ST_H3ToGeom](Spatial-Indexing/ST_H3ToGeom.md) | `Array` | Return the result of H3 function [cellsToMultiPolygon(cells)](https://h3geo.org/docs/api/regions#cellstolinkedmultipolygon--cellstomultipolygon). | v1.6.0 | | [ST_H3ToParent](Spatial-Indexing/ST_H3ToParent.md) | Long | Returns the parent of an H3 cell at the requested resolution. | v2.0.0 | +| [ST_H3ToPoint](Spatial-Indexing/ST_H3ToPoint.md) | Geometry | Returns the center of an H3 cell as a point in longitude/latitude order with SRID 0. | v2.0.0 | | [ST_H3Uncompact](Spatial-Indexing/ST_H3Uncompact.md) | `Array` | Expands H3 cells to the requested resolution. | v2.0.0 | | [ST_HilbertDistance](Spatial-Indexing/ST_HilbertDistance.md) | Long | Maps the midpoint of a geometry's envelope to an unsigned Hilbert-curve address within a supplied extent. | v2.0.0 | | [ST_S2CellIDs](Spatial-Indexing/ST_S2CellIDs.md) | `Array` | Cover the geometry with Google S2 Cells, return the corresponding cell IDs with the given level. The level indicates the [size of cells](https://s2geometry.io/resources/s2cell_statistics.html). With... | v1.4.0 | diff --git a/docs/api/sql/Spatial-Indexing/ST_H3ToPoint.md b/docs/api/sql/Spatial-Indexing/ST_H3ToPoint.md new file mode 100644 index 00000000000..55772c57c18 --- /dev/null +++ b/docs/api/sql/Spatial-Indexing/ST_H3ToPoint.md @@ -0,0 +1,44 @@ + + +# ST_H3ToPoint + +Introduction: Returns the center of an H3 cell as a point geometry. This uses H3's cell center, which can differ from the centroid of the cell polygon because of the grid projection. + +Coordinates are longitude/latitude (X/Y) in degrees. The result has SRID 0, consistent with `ST_H3ToGeom`. To assign WGS 84, use `ST_SetSRID(ST_H3ToPoint(cell), 4326)`. + +Null input returns null. Invalid cell indexes raise an error. + +Format: `ST_H3ToPoint(cell: Long)` + +Return type: `Geometry` (Point) + +Since: `v2.0.0` + +SQL Example + +```sql +SELECT ST_AsText(ST_H3ToPoint(617700169958293503)) +``` + +Output (coordinates rounded): + +```text +POINT (-122.41845932318309 37.776702349435695) +``` 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 9b32ca826d1..808d1e4d57a 100644 --- a/flink/src/main/java/org/apache/sedona/flink/Catalog.java +++ b/flink/src/main/java/org/apache/sedona/flink/Catalog.java @@ -109,6 +109,7 @@ public static UserDefinedFunction[] getFuncs() { new Functions.ST_H3CellDistance(), new Functions.ST_H3CellIDs(), new Functions.ST_H3KRing(), + new Functions.ST_H3ToPoint(), new Functions.ST_H3ToGeom(), new Functions.ST_H3ToParent(), new Functions.ST_H3ToChildren(), 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 55cbfdccb40..03e679f4bde 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 @@ -2941,6 +2941,19 @@ public Long[] eval( } } + public static class ST_H3ToPoint extends ScalarFunction { + @DataTypeHint( + value = "RAW", + rawSerializer = GeometryTypeSerializer.class, + bridgedTo = Geometry.class) + public Geometry eval(@DataTypeHint("BIGINT") Long cell) { + if (cell == null) { + return null; + } + return org.apache.sedona.common.Functions.h3ToPoint(cell); + } + } + public static class ST_H3ToGeom extends ScalarFunction { @DataTypeHint( value = "RAW", 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 2c3f1fadacd..f8d9d4c9f58 100644 --- a/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java +++ b/flink/src/test/java/org/apache/sedona/flink/FunctionTest.java @@ -2352,6 +2352,22 @@ public void testH3KRing() { assert full.containsAll(exactRing); } + @Test + public void testH3ToPoint() { + Geometry center = + (Geometry) + first( + tableEnv.sqlQuery( + "SELECT ST_H3ToPoint(cell) FROM (VALUES (617700169958293503)) AS cells(cell)")) + .getField(0); + assertNotNull(center); + assertEquals("Point", center.getGeometryType()); + assertEquals(-122.41845932318309, center.getCoordinate().x, 1e-10); + assertEquals(37.776702349435695, center.getCoordinate().y, 1e-10); + assertEquals(0, center.getSRID()); + assertNull(first(tableEnv.sqlQuery("SELECT ST_H3ToPoint(CAST(NULL AS BIGINT))")).getField(0)); + } + @Test public void testH3ToGeom() { Table pointTable = diff --git a/python/sedona/spark/sql/st_functions.py b/python/sedona/spark/sql/st_functions.py index 0321bf1a3c7..8c44c71025d 100644 --- a/python/sedona/spark/sql/st_functions.py +++ b/python/sedona/spark/sql/st_functions.py @@ -1006,6 +1006,17 @@ def ST_H3KRing( return _call_st_function("ST_H3KRing", args) +@validate_argument_types +def ST_H3ToPoint(cell: Union[ColumnOrName, int]) -> Column: + """Return an H3 cell's center as a point in longitude/latitude order, with SRID 0. + + :param cell: H3 cell index + :return: center point in degrees + :rtype: Geometry + """ + return _call_st_function("ST_H3ToPoint", cell) + + @validate_argument_types def ST_H3ToGeom(cells: Union[ColumnOrName, list]) -> Column: """Cover Geometry with H3 Cells and return a List of Long type cell IDs diff --git a/python/tests/sql/test_dataframe_api.py b/python/tests/sql/test_dataframe_api.py index 1772e0dcc32..c59dc3b0e6a 100644 --- a/python/tests/sql/test_dataframe_api.py +++ b/python/tests/sql/test_dataframe_api.py @@ -2004,6 +2004,22 @@ def run_spatial_query(): for future in futures: future.result() + def test_st_h3_to_point(self): + cell = 617700169958293503 + df = self.spark.createDataFrame([(cell,), (None,)], "cell long") + rows = df.select( + stf.ST_H3ToPoint("cell"), + stf.ST_H3ToPoint(f.col("cell")), + stf.ST_H3ToPoint(cell), + ).collect() + for point in rows[0]: + assert point.geom_type == "Point" + assert abs(point.x + 122.41845932318309) < 1e-10 + assert abs(point.y - 37.776702349435695) < 1e-10 + assert rows[1][0] is None + assert rows[1][1] is None + assert rows[1][2] == rows[0][2] + def test_st_h3_to_parent(self): df = self.spark.createDataFrame([(614552609325318143,)], ["cell"]) parent = df.select(stf.ST_H3ToParent("cell", 5)).first()[0] diff --git a/spark/common/src/main/scala/org/apache/sedona/sql/UDF/Catalog.scala b/spark/common/src/main/scala/org/apache/sedona/sql/UDF/Catalog.scala index 1fd9f52daf0..e59ebaa94a1 100644 --- a/spark/common/src/main/scala/org/apache/sedona/sql/UDF/Catalog.scala +++ b/spark/common/src/main/scala/org/apache/sedona/sql/UDF/Catalog.scala @@ -316,6 +316,7 @@ object Catalog extends AbstractCatalog with Logging { function[ST_H3CellDistance](), function[ST_H3CellIDs](), function[ST_H3KRing](), + function[ST_H3ToPoint](), function[ST_H3ToGeom](), function[ST_H3ToParent](), function[ST_H3ToChildren](), diff --git a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala index 092c6b99d15..e0968c6e213 100644 --- a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala +++ b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/Functions.scala @@ -1718,6 +1718,15 @@ private[apache] case class ST_H3KRing(inputExpressions: Seq[Expression]) } } +private[apache] case class ST_H3ToPoint(inputExpressions: Seq[Expression]) + extends InferredExpression(Functions.h3ToPoint _) + with FoldableExpression { + + protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = { + copy(inputExpressions = newChildren) + } +} + private[apache] case class ST_H3ToGeom(inputExpressions: Seq[Expression]) extends InferredExpression(Functions.h3ToGeom _) with FoldableExpression { diff --git a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_functions.scala b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_functions.scala index 06b0201e661..5fab65c4e3e 100644 --- a/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_functions.scala +++ b/spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/st_functions.scala @@ -298,6 +298,10 @@ object st_functions { def ST_H3KRing(cell: Long, k: Integer, exactRing: Boolean): Column = wrapExpression[ST_H3KRing](cell, k, exactRing) + def ST_H3ToPoint(cell: Column): Column = wrapExpression[ST_H3ToPoint](cell) + + def ST_H3ToPoint(cell: Long): Column = wrapExpression[ST_H3ToPoint](cell) + def ST_H3ToGeom(cellIds: Column): Column = wrapExpression[ST_H3ToGeom](cellIds) def ST_H3ToGeom(cellIds: Array[Long]): Column = wrapExpression[ST_H3ToGeom](cellIds) diff --git a/spark/common/src/test/scala/org/apache/sedona/sql/dataFrameAPITestScala.scala b/spark/common/src/test/scala/org/apache/sedona/sql/dataFrameAPITestScala.scala index 08fd08e2ef2..2005b9bce68 100644 --- a/spark/common/src/test/scala/org/apache/sedona/sql/dataFrameAPITestScala.scala +++ b/spark/common/src/test/scala/org/apache/sedona/sql/dataFrameAPITestScala.scala @@ -2116,6 +2116,20 @@ class dataFrameAPITestScala extends TestBaseScala { } } + it("Passed ST_H3ToPoint") { + val cell = 617700169958293503L + val row = sparkSession + .sql(s"SELECT $cell AS cell") + .select(ST_H3ToPoint(col("cell")), ST_H3ToPoint(cell)) + .first() + (0 until 2).foreach { i => + val point = row.getAs[org.locationtech.jts.geom.Geometry](i) + assert(math.abs(point.getCoordinate.x + 122.41845932318309) < 1e-10) + assert(math.abs(point.getCoordinate.y - 37.776702349435695) < 1e-10) + assert(point.getSRID == 0) + } + } + it("Passed ST_H3ToCenterChild") { val cell = 601041811137363967L val result = sparkSession diff --git a/spark/common/src/test/scala/org/apache/sedona/sql/functions/STH3Functions.scala b/spark/common/src/test/scala/org/apache/sedona/sql/functions/STH3Functions.scala index c644b52e37c..cea8ced8ac3 100644 --- a/spark/common/src/test/scala/org/apache/sedona/sql/functions/STH3Functions.scala +++ b/spark/common/src/test/scala/org/apache/sedona/sql/functions/STH3Functions.scala @@ -30,6 +30,33 @@ import scala.collection.mutable class STH3Functions extends TestBaseScala with Matchers with GeometrySample with GivenWhenThen { import sparkSession.implicits._ + describe("ST_H3ToPoint") { + it("returns the H3 center as longitude/latitude with SRID 0") { + val row = sparkSession + .sql(""" + |SELECT ST_X(ST_H3ToPoint(617700169958293503)), + |ST_Y(ST_H3ToPoint(617700169958293503)), + |ST_SRID(ST_H3ToPoint(617700169958293503)), + |ST_GeometryType(ST_H3ToPoint(617700169958293503)) + |""".stripMargin) + .first() + assert(math.abs(row.getDouble(0) + 122.41845932318309) < 1e-10) + assert(math.abs(row.getDouble(1) - 37.776702349435695) < 1e-10) + assert(row.getInt(2) == 0) + assert(row.getString(3) == "ST_Point") + } + + it("handles columns and propagates nulls") { + val rows = Seq[java.lang.Long](617700169958293503L, null) + .toDF("cell") + .selectExpr("ST_H3ToPoint(cell)") + .collect() + assert(rows(0).getAs[org.locationtech.jts.geom.Geometry](0).getSRID == 0) + assert(rows(1).isNullAt(0)) + assert(sparkSession.sql("SELECT ST_H3ToPoint(CAST(NULL AS BIGINT))").first().isNullAt(0)) + } + } + describe("should pass ST_H3CellIDs") { it("should return null while using ST_H3CellIDs when geometry is empty") {