Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions common/src/main/java/org/apache/sedona/common/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2088,6 +2088,18 @@ public static Long[] h3ToChildren(long cell, int resolution) {
return H3Utils.h3.cellToChildren(cell, resolution).toArray(new Long[0]);
}

/**
* Returns the center child of an H3 cell at the requested resolution.
*
* @param cell the H3 cell
* @param resolution the resolution of the center child, between the input cell's resolution and
* 15
* @return the center child H3 cell, or the input cell when the resolutions are equal
*/
public static long h3ToCenterChild(long cell, int resolution) {
return H3Utils.h3.cellToCenterChild(cell, resolution);
}

/**
* Compacts H3 cells by recursively replacing complete child sets with their parents.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,58 @@ public void h3ToChildrenInvalidResolution() {
assertThrows(H3Exception.class, () -> Functions.h3ToChildren(cell, 4));
}

@Test
public void h3ToCenterChild() {
long parent = 601041811137363967L;
long immediate = Functions.h3ToCenterChild(parent, 6);
assertEquals(605545409825210367L, immediate);
assertTrue(H3Utils.h3.cellToChildren(parent, 6).contains(immediate));
for (int resolution : new int[] {6, 8, 15}) {
long child = Functions.h3ToCenterChild(parent, resolution);
assertEquals(resolution, H3Utils.h3.getResolution(child));
assertEquals(parent, Functions.h3ToParent(child, 5));
assertEquals(child, Functions.h3ToCenterChild(immediate, resolution));
}
// H3's documented center-child example, including a skip of two resolutions.
assertEquals(0x872834700ffffffL, Functions.h3ToCenterChild(0x85283473fffffffL, 7));
}

@Test
public void h3ToCenterChildPentagon() {
for (long pentagon : H3Utils.h3.getPentagons(5)) {
for (int resolution : new int[] {6, 8, 15}) {
long child = Functions.h3ToCenterChild(pentagon, resolution);
assertEquals(resolution, H3Utils.h3.getResolution(child));
assertTrue(H3Utils.h3.isPentagon(child));
assertEquals(pentagon, Functions.h3ToParent(child, 5));
}
}
}

@Test
public void h3ToCenterChildEqualResolutionAndBoundaries() {
for (int resolution : new int[] {0, 5, 15}) {
long cell = H3Utils.coordinateToCell(new Coordinate(1, 2), resolution);
assertEquals(cell, Functions.h3ToCenterChild(cell, resolution));
}
for (int resolution : new int[] {0, 14}) {
long cell = H3Utils.coordinateToCell(new Coordinate(1, 2), resolution);
long child = Functions.h3ToCenterChild(cell, resolution + 1);
assertEquals(resolution + 1, H3Utils.h3.getResolution(child));
assertEquals(cell, Functions.h3ToParent(child, resolution));
}
}

@Test
public void h3ToCenterChildInvalidResolution() {
long cell = H3Utils.coordinateToCell(new Coordinate(1, 2), 5);
for (int resolution : new int[] {-1, 16}) {
assertThrows(
IllegalArgumentException.class, () -> Functions.h3ToCenterChild(cell, resolution));
}
assertThrows(H3Exception.class, () -> Functions.h3ToCenterChild(cell, 4));
}

@Test
public void h3CompactRecursive() {
long parent = H3Utils.coordinateToCell(new Coordinate(1, 2), 5);
Expand Down
1 change: 1 addition & 0 deletions docs/api/flink/Geometry-Functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ These functions work with spatial indexing systems including Bing Tiles, H3, Hil
| [ST_H3CellIDs](Spatial-Indexing/ST_H3CellIDs.md) | `Array<Long>` | Cover the geometry by H3 cell IDs with the given resolution(level). To understand the cell statistics please refer to [H3 Doc](https://h3geo.org/docs/core-library/restable) H3 native fill functions... | v1.5.0 |
| [ST_H3Compact](Spatial-Indexing/ST_H3Compact.md) | `Array<Long>` | Compacts H3 cells by recursively replacing complete child sets with their parents. | v2.0.0 |
| [ST_H3KRing](Spatial-Indexing/ST_H3KRing.md) | `Array<Long>` | return the result of H3 function [gridDisk(cell, k)](https://h3geo.org/docs/api/traversal#griddisk). | v1.5.0 |
| [ST_H3ToCenterChild](Spatial-Indexing/ST_H3ToCenterChild.md) | Long | Returns the center child of an H3 cell at the requested resolution. | v2.0.0 |
| [ST_H3ToChildren](Spatial-Indexing/ST_H3ToChildren.md) | `Array<Long>` | Returns the children of an H3 cell at the requested resolution. | v2.0.0 |
| [ST_H3ToGeom](Spatial-Indexing/ST_H3ToGeom.md) | `Array<Geometry>` | 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 |
Expand Down
58 changes: 58 additions & 0 deletions docs/api/flink/Spatial-Indexing/ST_H3ToCenterChild.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!--
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.
-->

# ST_H3ToCenterChild

Introduction: Returns the center child of an H3 cell at the requested resolution using H3's [cellToCenterChild(cell, resolution)](https://h3geo.org/docs/api/hierarchy#celltocenterchild).

The requested resolution must be between the input cell's resolution and 15, inclusive. An equal resolution returns the input cell. A lower resolution or a resolution outside this range raises an error. A null cell or resolution returns null.

The function returns a single center descendant at the target resolution, including when skipping multiple resolutions. For a pentagon input, the center child is also a pentagon.

Format: `ST_H3ToCenterChild(cell: Long, resolution: Int)`

Return type: `Long`

Since: `v2.0.0`

SQL Example

Find the resolution 6 center child of a resolution 5 hexagon:

```sql
SELECT ST_H3ToCenterChild(601041811137363967, 6)
```

Output:

```text
605545409825210367
```

An equal resolution retains the input cell:

```sql
SELECT ST_H3ToCenterChild(601041811137363967, 5)
```

Output:

```text
601041811137363967
```
1 change: 1 addition & 0 deletions docs/api/sql/Geometry-Functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ These functions work with spatial indexing systems including Bing Tiles, H3, Hil
| [ST_H3CellIDs](Spatial-Indexing/ST_H3CellIDs.md) | `Array<Long>` | Cover the geometry by H3 cell IDs with the given resolution(level). To understand the cell statistics please refer to [H3 Doc](https://h3geo.org/docs/core-library/restable) H3 native fill functions... | v1.5.0 |
| [ST_H3Compact](Spatial-Indexing/ST_H3Compact.md) | `Array<Long>` | Compacts H3 cells by recursively replacing complete child sets with their parents. | v2.0.0 |
| [ST_H3KRing](Spatial-Indexing/ST_H3KRing.md) | `Array<Long>` | return the result of H3 function [gridDisk(cell, k)](https://h3geo.org/docs/api/traversal#griddisk). | v1.5.0 |
| [ST_H3ToCenterChild](Spatial-Indexing/ST_H3ToCenterChild.md) | Long | Returns the center child of an H3 cell at the requested resolution. | v2.0.0 |
| [ST_H3ToChildren](Spatial-Indexing/ST_H3ToChildren.md) | `Array<Long>` | Returns the children of an H3 cell at the requested resolution. | v2.0.0 |
| [ST_H3ToGeom](Spatial-Indexing/ST_H3ToGeom.md) | `Array<Geometry>` | 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 |
Expand Down
58 changes: 58 additions & 0 deletions docs/api/sql/Spatial-Indexing/ST_H3ToCenterChild.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!--
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.
-->

# ST_H3ToCenterChild

Introduction: Returns the center child of an H3 cell at the requested resolution using H3's [cellToCenterChild(cell, resolution)](https://h3geo.org/docs/api/hierarchy#celltocenterchild).

The requested resolution must be between the input cell's resolution and 15, inclusive. An equal resolution returns the input cell. A lower resolution or a resolution outside this range raises an error. A null cell or resolution returns null.

The function returns a single center descendant at the target resolution, including when skipping multiple resolutions. For a pentagon input, the center child is also a pentagon.

Format: `ST_H3ToCenterChild(cell: Long, resolution: Int)`

Return type: `Long`

Since: `v2.0.0`

SQL Example

Find the resolution 6 center child of a resolution 5 hexagon:

```sql
SELECT ST_H3ToCenterChild(601041811137363967, 6)
```

Output:

```text
605545409825210367
```

An equal resolution retains the input cell:

```sql
SELECT ST_H3ToCenterChild(601041811137363967, 5)
```

Output:

```text
601041811137363967
```
1 change: 1 addition & 0 deletions flink/src/main/java/org/apache/sedona/flink/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public static UserDefinedFunction[] getFuncs() {
new Functions.ST_H3ToGeom(),
new Functions.ST_H3ToParent(),
new Functions.ST_H3ToChildren(),
new Functions.ST_H3ToCenterChild(),
new Functions.ST_H3Uncompact(),
new Functions.ST_H3Compact(),
new Functions.ST_HilbertDistance(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2986,6 +2986,17 @@ public Long[] eval(
}
}

public static class ST_H3ToCenterChild extends ScalarFunction {
@DataTypeHint(value = "BIGINT")
public Long eval(
@DataTypeHint("BIGINT") Long cell, @DataTypeHint("INTEGER") Integer resolution) {
if (cell == null || resolution == null) {
return null;
}
return org.apache.sedona.common.Functions.h3ToCenterChild(cell, resolution);
}
}

public static class ST_H3Compact extends ScalarFunction {
@DataTypeHint(value = "ARRAY<BIGINT>")
public Long[] eval(@DataTypeHint("ARRAY<BIGINT>") Long[] cells) {
Expand Down
46 changes: 46 additions & 0 deletions flink/src/test/java/org/apache/sedona/flink/FunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2424,6 +2424,52 @@ public void testH3ToChildren() {
}
}

@Test
public void testH3ToCenterChild() {
long parent = 601041811137363967L;
Row result =
first(
tableEnv.sqlQuery(
"SELECT ST_H3ToCenterChild(cell, resolution), ST_H3ToCenterChild(cell, 5), "
+ "ST_H3ToParent(ST_H3ToCenterChild(cell, 15), 5) "
+ "FROM (VALUES ("
+ parent
+ ", 6)) AS input(cell, resolution)"));
assertEquals(605545409825210367L, result.getField(0));
assertEquals(parent, result.getField(1));
assertEquals(parent, result.getField(2));

long pentagon = H3Utils.h3.getPentagons(5).iterator().next();
long child =
(Long)
Objects.requireNonNull(
first(tableEnv.sqlQuery("SELECT ST_H3ToCenterChild(" + pentagon + ", 8)"))
.getField(0));
assertTrue(H3Utils.h3.isPentagon(child));
assertEquals(8, H3Utils.h3.getResolution(child));
assertEquals(pentagon, H3Utils.h3.cellToParent(child, 5));

Row nulls =
first(
tableEnv.sqlQuery(
"SELECT ST_H3ToCenterChild(CAST(NULL AS BIGINT), 6), "
+ "ST_H3ToCenterChild("
+ parent
+ ", CAST(NULL AS INT)), "
+ "ST_H3ToCenterChild(CAST(NULL AS BIGINT), CAST(NULL AS INT))"));
for (int i = 0; i < 3; i++) {
assertNull(nulls.getField(i));
}
Table nullable =
tableEnv.sqlQuery(
"SELECT ST_H3ToCenterChild(cell, resolution) "
+ "FROM (VALUES (CAST(NULL AS BIGINT), 6), ("
+ parent
+ ", CAST(NULL AS INT))) AS input(cell, resolution)");
assertNull(first(nullable).getField(0));
assertNull(last(nullable).getField(0));
}

@Test
public void testH3Compact() {
long parent = H3Utils.coordinateToCell(new Coordinate(1, 2), 5);
Expand Down
14 changes: 14 additions & 0 deletions python/sedona/spark/sql/st_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,20 @@ def ST_H3ToChildren(
return _call_st_function("ST_H3ToChildren", args)


@validate_argument_types
def ST_H3ToCenterChild(
cell: Union[ColumnOrName, int], resolution: Union[ColumnOrName, int]
) -> Column:
"""Return the center child of an H3 cell at the requested resolution.
:param cell: H3 cell
:param resolution: resolution of the center child, from the input resolution to 15
:return: center child H3 cell, or the input cell when the resolutions are equal
:rtype: Long
"""
args = (cell, resolution)
return _call_st_function("ST_H3ToCenterChild", args)


@validate_argument_types
def ST_H3Uncompact(
cells: Union[ColumnOrName, list], resolution: Union[ColumnOrName, int]
Expand Down
20 changes: 20 additions & 0 deletions python/tests/sql/test_dataframe_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,26 @@ def test_st_h3_to_children(self):
assert by_name == by_column == by_literal
assert df.select(stf.ST_H3ToChildren("cell", 5)).first()[0] == [parent]

def test_st_h3_to_center_child(self):
parent = 601041811137363967
df = self.spark.createDataFrame([(parent, 6)], ["cell", "resolution"])
by_name, by_column, by_literal = df.select(
stf.ST_H3ToCenterChild("cell", 6),
stf.ST_H3ToCenterChild(f.col("cell"), f.col("resolution")),
stf.ST_H3ToCenterChild(parent, 6),
).first()
assert by_name == by_column == by_literal == 605545409825210367
assert df.select(stf.ST_H3ToCenterChild("cell", 5)).first()[0] == parent
nullable = self.spark.createDataFrame(
[(None, 6), (parent, None), (None, None)], "cell long, resolution int"
)
assert all(
row[0] is None
for row in nullable.select(
stf.ST_H3ToCenterChild("cell", "resolution")
).collect()
)

def test_st_h3_compact(self):
parent = 601041811137363967
children = self.spark.sql(f"SELECT ST_H3ToChildren({parent}, 6)").first()[0]
Expand Down
16 changes: 16 additions & 0 deletions python/tests/sql/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,22 @@ def test_st_h3_to_children(self):
""").first()
assert all(value is None for value in nulls)

def test_st_h3_to_center_child(self):
parent = 601041811137363967
child, same, ancestor = self.spark.sql(f"""
SELECT ST_H3ToCenterChild({parent}, 6),
ST_H3ToCenterChild({parent}, 5),
ST_H3ToParent(ST_H3ToCenterChild({parent}, 15), 5)
""").first()
assert child == 605545409825210367
assert same == ancestor == parent
nulls = self.spark.sql(f"""
SELECT ST_H3ToCenterChild(CAST(NULL AS BIGINT), 6),
ST_H3ToCenterChild({parent}, CAST(NULL AS INT)),
ST_H3ToCenterChild(CAST(NULL AS BIGINT), CAST(NULL AS INT))
""").first()
assert all(value is None for value in nulls)

def test_st_h3_compact(self):
parent = 601041811137363967
original, compacted, restored = self.spark.sql(f"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ object Catalog extends AbstractCatalog with Logging {
function[ST_H3ToGeom](),
function[ST_H3ToParent](),
function[ST_H3ToChildren](),
function[ST_H3ToCenterChild](),
function[ST_H3Uncompact](),
function[ST_H3Compact](),
function[ST_HilbertDistance](),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,14 @@ private[apache] case class ST_H3ToChildren(inputExpressions: Seq[Expression])
}
}

private[apache] case class ST_H3ToCenterChild(inputExpressions: Seq[Expression])
extends InferredExpression(Functions.h3ToCenterChild _) {

protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]) = {
copy(inputExpressions = newChildren)
}
}

private[apache] case class ST_H3Compact(inputExpressions: Seq[Expression])
extends InferredExpression(Functions.h3Compact _) {

Expand Down
Loading
Loading