|
1 | 1 | --- |
2 | 2 | id: hierarchy |
| 3 | +sidebar_position: 5 |
3 | 4 | title: Hierarchy Functions |
4 | 5 | --- |
5 | 6 |
|
6 | 7 | # Hierarchy Functions |
7 | 8 |
|
8 | | -:::note Coming soon |
9 | | -This page is under construction. |
| 9 | +Hierarchy functions navigate the parent–child relationships encoded directly in the Z7 index. |
| 10 | + |
| 11 | +## cellToParent |
| 12 | + |
| 13 | +Get the ancestor of a cell at a coarser resolution. |
| 14 | + |
| 15 | +``` |
| 16 | +cellToParent(cell_id, [resolution]) → cell_id |
| 17 | +``` |
| 18 | + |
| 19 | +| Parameter | Type | Description | |
| 20 | +|---|---|---| |
| 21 | +| `cell_id` | Z7 string / hex / int | The cell to find the parent of | |
| 22 | +| `resolution` | int (optional) | Target resolution; defaults to current resolution − 1 | |
| 23 | + |
| 24 | +**Returns:** Z7 cell ID at the target resolution. |
| 25 | + |
| 26 | +Because resolution is encoded in the Z7 string as its length, the parent is simply the string with its last digit removed. |
| 27 | + |
| 28 | +### Python |
| 29 | + |
| 30 | +```python |
| 31 | +from dggrid4py import igeo7 |
| 32 | + |
| 33 | +z7_str = "0800433" # resolution 5 |
| 34 | + |
| 35 | +# Parent at resolution 4 |
| 36 | +parent_r4 = z7_str[:-1] # "080043" |
| 37 | + |
| 38 | +# Parent at resolution 2 |
| 39 | +parent_r2 = z7_str[:4] # "0800" (2 base chars + 2 digits) |
| 40 | + |
| 41 | +# Helper function for arbitrary target resolution |
| 42 | +def cell_to_parent(z7_str: str, resolution: int) -> str: |
| 43 | + return z7_str[:2 + resolution] |
| 44 | + |
| 45 | +print(cell_to_parent("0800433", 3)) # "08004" |
| 46 | +``` |
| 47 | + |
| 48 | +### Julia |
| 49 | + |
| 50 | +```julia |
| 51 | +using IGEO7 |
| 52 | + |
| 53 | +idx = z7string_to_index("0800433") |
| 54 | + |
| 55 | +# Default: parent at res - 1 |
| 56 | +parent = get_parent(idx) |
| 57 | +index_to_z7string(parent) # "080043" |
| 58 | + |
| 59 | +# Explicit target resolution |
| 60 | +parent_r2 = get_parent(idx, 2) |
| 61 | +index_to_z7string(parent_r2) # "0800" |
| 62 | +``` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## cellToChildren |
| 67 | + |
| 68 | +Get all direct children of a cell at the next finer resolution. |
| 69 | + |
| 70 | +``` |
| 71 | +cellToChildren(cell_id, [child_resolution]) → list[cell_id] |
| 72 | +``` |
| 73 | + |
| 74 | +| Parameter | Type | Description | |
| 75 | +|---|---|---| |
| 76 | +| `cell_id` | Z7 string | The parent cell | |
| 77 | +| `child_resolution` | int (optional) | Target resolution; defaults to current + 1 | |
| 78 | + |
| 79 | +**Returns:** List of 7 cell IDs (6 for pentagons). |
| 80 | + |
| 81 | +Because children are formed by appending digits 0–6, this is pure string manipulation for the Z7 string format. |
| 82 | + |
| 83 | +### Python |
| 84 | + |
| 85 | +```python |
| 86 | +z7_str = "0800433" # resolution 5 |
| 87 | + |
| 88 | +# Direct children at resolution 6 |
| 89 | +children = [z7_str + str(d) for d in range(7)] |
| 90 | +# ["08004330", "08004331", "08004332", |
| 91 | +# "08004333", "08004334", "08004335", "08004336"] |
| 92 | + |
| 93 | +# Children at an arbitrary deeper resolution via dggrid4py |
| 94 | +children_gdf = dggrid.grid_cell_polygons_from_cellids( |
| 95 | + cell_id_list=[z7_str], |
| 96 | + dggs_type="IGEO7", |
| 97 | + resolution=8, # 3 levels deeper → 7³ = 343 cells |
| 98 | + clip_subset_type="COARSE_CELLS", |
| 99 | + clip_cell_res=5, |
| 100 | + input_address_type="Z7_STRING", |
| 101 | + output_address_type="Z7_STRING", |
| 102 | +) |
| 103 | +print(len(children_gdf)) # 343 |
| 104 | +``` |
| 105 | + |
| 106 | +### Julia |
| 107 | + |
| 108 | +```julia |
| 109 | +using IGEO7 |
| 110 | + |
| 111 | +idx = z7string_to_index("0800433") # res 5 |
| 112 | +res = get_resolution(idx) # 5 |
| 113 | + |
| 114 | +# Direct children: append each digit 0–6 |
| 115 | +children = [index_to_z7string(idx) * string(d) for d in 0:6] |
| 116 | +``` |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## cellToChildrenSize |
| 121 | + |
| 122 | +Get the number of children a cell has at a given resolution. |
| 123 | + |
| 124 | +``` |
| 125 | +cellToChildrenSize(cell_id, child_resolution) → int |
| 126 | +``` |
| 127 | + |
| 128 | +For cells spanning multiple resolution levels the count is $7^{\Delta r}$ for hexagons, slightly less for paths through pentagons. |
| 129 | + |
| 130 | +### Python |
| 131 | + |
| 132 | +```python |
| 133 | +def cell_to_children_size(z7_str: str, child_resolution: int) -> int: |
| 134 | + current_res = len(z7_str) - 2 |
| 135 | + delta = child_resolution - current_res |
| 136 | + if delta <= 0: |
| 137 | + return 0 |
| 138 | + return 7 ** delta # exact for hexagons; pentagons slightly fewer |
| 139 | + |
| 140 | +print(cell_to_children_size("0800433", 7)) # 7^2 = 49 |
| 141 | +print(cell_to_children_size("0800433", 8)) # 7^3 = 343 |
| 142 | +``` |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +## areNeighbourCells |
| 147 | + |
| 148 | +Check whether two cells at the same resolution share an edge. |
| 149 | + |
| 150 | +``` |
| 151 | +areNeighbourCells(cell_id_a, cell_id_b) → bool |
| 152 | +``` |
| 153 | + |
| 154 | +Currently implemented via geometry intersection in dggrid4py (load cell polygons and test for shared edges). Native Z7 arithmetic-based neighbour checking is available in [IGEO7.jl](https://github.com/allixender/IGEO7.jl). |
| 155 | + |
| 156 | +### Python — geometry-based |
| 157 | + |
| 158 | +```python |
| 159 | +from dggrid4py import igeo7 |
| 160 | + |
| 161 | +# Precompute the spatial index once |
| 162 | +gdf = dggrid.grid_cell_polygons_for_extent("IGEO7", resolution=9, ...) |
| 163 | +sindex = gdf.sindex.query(gdf.geometry, predicate="intersects") |
| 164 | + |
| 165 | +neighbours_of_a = igeo7.get_neighbours_by_z7( |
| 166 | + z7_idx="090264253", |
| 167 | + gdf=gdf, |
| 168 | + gpd_sindex=sindex, |
| 169 | + z7_col="global_id", |
| 170 | +) |
| 171 | +print("090264254" in neighbours_of_a) |
| 172 | +``` |
| 173 | + |
| 174 | +### Julia — Z7 arithmetic |
| 175 | + |
| 176 | +```julia |
| 177 | +using IGEO7 |
| 178 | +idx = z7string_to_index("0800433") |
| 179 | +neighbours = get_neighbours(idx) # Vector of Z7IndexUInt64 (6 entries, invalid ones = typemax) |
| 180 | +``` |
| 181 | + |
| 182 | +--- |
| 183 | + |
| 184 | +## compactCells / uncompactCells |
| 185 | + |
| 186 | +Represent a set of cells at mixed resolutions by merging complete groups of 7 siblings into their parent. |
| 187 | + |
| 188 | +This operation is not yet in dggrid4py but is achievable via Z7 string grouping: |
| 189 | + |
| 190 | +```python |
| 191 | +from collections import defaultdict |
| 192 | + |
| 193 | +def compact(cell_ids: list[str]) -> list[str]: |
| 194 | + """Merge sibling groups of 7 into their parent, recursively.""" |
| 195 | + by_parent = defaultdict(set) |
| 196 | + for c in cell_ids: |
| 197 | + by_parent[c[:-1]].add(c[-1]) |
| 198 | + result = [] |
| 199 | + for parent, digits in by_parent.items(): |
| 200 | + if len(digits) == 7: |
| 201 | + result.append(parent) # full group — promote to parent |
| 202 | + else: |
| 203 | + result.extend(parent + d for d in digits) |
| 204 | + return result |
| 205 | +``` |
| 206 | + |
| 207 | +:::note Pentagon exception |
| 208 | +Pentagon cells have only 6 children (digits 0–5 for the centre and 5 edge neighbours; digit 6 is absent). A compact operation over pentagonal children should treat a group of 6 as complete. |
10 | 209 | ::: |
0 commit comments