Skip to content

Commit fdced3f

Browse files
committed
fixes and dggrid4py updates
1 parent c23927a commit fdced3f

24 files changed

Lines changed: 436 additions & 283 deletions

‎README.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ website/src/components/Z7Explorer/igeo7-config.mjs -> ORIENTATION_LON
7070
```
7171

7272
The explorer and both harnesses import from that file, so there is no second
73-
copy to keep in step. Be aware that 11.2 and 11.25 give **identical** results
74-
through resolution 5 and first diverge at resolution 6, so a resolution-5 test
75-
point cannot tell them apart.
73+
copy to keep in step.
7674

7775
**2. Authalic latitude conversion**, which is mandatory and is *not* part of the
7876
config object, because webDggrid does not apply it for you. It is a round trip

‎website/docs/api/hierarchy.md‎

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,35 @@ Because resolution is encoded in the Z7 string as its length, the parent is simp
3030
```python
3131
from dggrid4py import igeo7
3232

33-
z7_str = "0800433" # resolution 5
33+
z7_str = "0001022" # resolution 5
3434

3535
# Parent at resolution 4
36-
parent_r4 = z7_str[:-1] # "080043"
36+
parent_r4 = z7_str[:-1] # "000102"
3737

3838
# Parent at resolution 2
39-
parent_r2 = z7_str[:4] # "0800" (2 base chars + 2 digits)
39+
parent_r2 = z7_str[:4] # "0001" (2 base chars + 2 digits)
4040

4141
# Helper function for arbitrary target resolution
4242
def cell_to_parent(z7_str: str, resolution: int) -> str:
4343
return z7_str[:2 + resolution]
4444

45-
print(cell_to_parent("0800433", 3)) # "08004"
45+
print(cell_to_parent("0001022", 3)) # "00010"
4646
```
4747

4848
### Julia
4949

5050
```julia
5151
using IGEO7
5252

53-
idx = z7string_to_index("0800433")
53+
idx = z7string_to_index("0001022")
5454

5555
# Default: parent at res - 1
5656
parent = get_parent(idx)
57-
index_to_z7string(parent) # "080043"
57+
index_to_z7string(parent) # "000102"
5858

5959
# Explicit target resolution
6060
parent_r2 = get_parent(idx, 2)
61-
index_to_z7string(parent_r2) # "0800"
61+
index_to_z7string(parent_r2) # "0001"
6262
```
6363

6464
---
@@ -83,32 +83,39 @@ Because children are formed by appending digits 0–6, this is pure string manip
8383
### Python
8484

8585
```python
86-
z7_str = "0800433" # resolution 5
86+
z7_str = "0001022" # resolution 5
8787

8888
# Direct children at resolution 6
8989
children = [z7_str + str(d) for d in range(7)]
90-
# ["08004330", "08004331", "08004332",
91-
# "08004333", "08004334", "08004335", "08004336"]
90+
# ["00010220", "00010221", "00010222",
91+
# "00010223", "00010224", "00010225", "00010226"]
9292

93-
# Children at an arbitrary deeper resolution via dggrid4py
94-
children_gdf = dggrid.grid_cell_polygons_from_cellids(
93+
# Cells at a deeper resolution inside the parent, with geometry, via dggrid4py
94+
# (dggrid, IGEO7_META and geoseries_to_geodetic as in the API Overview)
95+
cells_gdf = dggrid.grid_cell_polygons_from_cellids(
9596
cell_id_list=[z7_str],
9697
dggs_type="IGEO7",
97-
resolution=8, # 3 levels deeper → 7³ = 343 cells
98+
resolution=6,
9899
clip_subset_type="COARSE_CELLS",
99100
clip_cell_res=5,
100-
input_address_type="Z7_STRING",
101-
output_address_type="Z7_STRING",
101+
**IGEO7_META,
102102
)
103-
print(len(children_gdf)) # 343
103+
cells_gdf["geometry"] = geoseries_to_geodetic(cells_gdf.geometry)
104+
105+
# COARSE_CELLS is a spatial clip to the parent cell, so it also returns
106+
# neighbouring cells that overlap it. Keep the index children by prefix:
107+
children_gdf = cells_gdf[cells_gdf["name"].str.startswith(z7_str)]
108+
print(len(children_gdf)) # 7
104109
```
105110

111+
Over several levels the spatial clip and the index hierarchy drift apart, because the descendants of a cell do not exactly fill its hexagon. For the complete set of index descendants, enumerate the digit strings and request their geometry with `grid_cell_polygons_from_cellids`.
112+
106113
### Julia
107114

108115
```julia
109116
using IGEO7
110117

111-
idx = z7string_to_index("0800433") # res 5
118+
idx = z7string_to_index("0001022") # res 5
112119
res = get_resolution(idx) # 5
113120

114121
# Direct children: append each digit 0–6
@@ -137,8 +144,8 @@ def cell_to_children_size(z7_str: str, child_resolution: int) -> int:
137144
return 0
138145
return 7 ** delta # exact for hexagons; pentagons slightly fewer
139146

140-
print(cell_to_children_size("0800433", 7)) # 7^2 = 49
141-
print(cell_to_children_size("0800433", 8)) # 7^3 = 343
147+
print(cell_to_children_size("0001022", 7)) # 7^2 = 49
148+
print(cell_to_children_size("0001022", 8)) # 7^3 = 343
142149
```
143150

144151
---
@@ -159,23 +166,23 @@ Currently implemented via geometry intersection in dggrid4py (load cell polygons
159166
from dggrid4py import igeo7
160167

161168
# Precompute the spatial index once
162-
gdf = dggrid.grid_cell_polygons_for_extent("IGEO7", resolution=9, ...)
169+
gdf = dggrid.grid_cell_polygons_for_extent("IGEO7", resolution=9, ..., **IGEO7_META)
163170
sindex = gdf.sindex.query(gdf.geometry, predicate="intersects")
164171

165172
neighbours_of_a = igeo7.get_neighbours_by_z7(
166-
z7_idx="090264253",
173+
z7_idx="00010224545",
167174
gdf=gdf,
168175
gpd_sindex=sindex,
169-
z7_col="global_id",
176+
z7_col="name",
170177
)
171-
print("090264254" in neighbours_of_a)
178+
print("00010224540" in neighbours_of_a) # True
172179
```
173180

174181
### Julia — Z7 arithmetic
175182

176183
```julia
177184
using IGEO7
178-
idx = z7string_to_index("0800433")
185+
idx = z7string_to_index("0001022")
179186
neighbours = get_neighbours(idx) # Vector of Z7IndexUInt64 (6 entries, invalid ones = typemax)
180187
```
181188

‎website/docs/api/indexing.md‎

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,44 @@ latLngToCell(lat, lng, resolution) → cell_id
2626

2727
### Python — dggrid4py
2828

29-
For a single point or a batch via GeoDataFrame:
29+
For a single point or a batch via GeoDataFrame, with `dggrid`, `IGEO7_META` and the `auxlat` functions from the [API Overview](./overview#initialising-dggridv8):
3030

3131
```python
32-
import geopandas as gpd
33-
from dggrid4py import DGGRIDv7
34-
35-
dggrid = DGGRIDv7(executable="/usr/local/bin/dggrid", working_dir="/tmp")
36-
3732
# Single point: Tartu, Estonia
3833
points = gpd.GeoDataFrame(
39-
geometry=gpd.points_from_xy([26.722], [58.378]),
34+
geometry=gpd.points_from_xy([26.7220], [58.3776]),
4035
crs=4326,
4136
)
4237

38+
# WGS84 → authalic sphere; despite the parameter name, DGGRID expects authalic latitudes here
39+
points_authalic = gpd.GeoDataFrame(geometry=geoseries_to_authalic(points.geometry), crs=4326)
40+
4341
result = dggrid.cells_for_geo_points(
44-
geodf_points_wgs84=points,
42+
geodf_points_wgs84=points_authalic,
4543
cell_ids_only=True,
4644
dggs_type="IGEO7",
4745
resolution=9,
46+
**IGEO7_META,
4847
)
49-
print(result["name"].iloc[0]) # Z7 hex string
48+
print(result["name"].iloc[0]) # Z7 string: 00010224545
5049
```
5150

52-
For Z7 string output, transform the result with `igeo7.z7hex_to_z7string`.
51+
For Z7 hex output, set `"output_hier_ndx_form": "INT64"` in the configuration (here: `004252cbffffffff`).
5352

5453
**Batch indexing** with a large GeoDataFrame works identically — pass all points at once:
5554

5655
```python
57-
gdf_cities = gpd.read_file("cities.gpkg")
56+
gdf_cities = gpd.read_file("cities.gpkg").to_crs(4326)
57+
cities_authalic = gpd.GeoDataFrame(geometry=geoseries_to_authalic(gdf_cities.geometry), crs=4326)
58+
5859
indexed = dggrid.cells_for_geo_points(
59-
geodf_points_wgs84=gdf_cities,
60+
geodf_points_wgs84=cities_authalic,
6061
cell_ids_only=True,
6162
dggs_type="IGEO7",
6263
resolution=9,
64+
**IGEO7_META,
6365
)
66+
gdf_cities["name"] = indexed["name"].values
6467
```
6568

6669
### Julia — IGEO7.jl
@@ -80,34 +83,33 @@ cellToLatLng(cell_id) → (lat, lng)
8083
### Python — dggrid4py
8184

8285
```python
83-
# Get centroids for a list of cell IDs
84-
import pandas as pd
85-
86-
cell_ids = ["0800433", "0800434", "0800435"]
86+
# Get centroids for a list of cell IDs (all of the same resolution)
87+
cell_ids = ["0001250", "0001254", "0001240"]
8788

88-
gdf_centroids = dggrid.grid_cell_centroids_for_extent(
89+
gdf_centroids = dggrid.grid_cell_centroids_from_cellids(
90+
cell_id_list=cell_ids,
8991
dggs_type="IGEO7",
9092
resolution=5,
91-
clip_geom=None, # global
93+
**IGEO7_META,
9294
)
93-
# Filter to your cells
94-
mask = gdf_centroids["global_id"].isin(cell_ids)
95-
print(gdf_centroids[mask][["global_id", "geometry"]])
95+
# authalic sphere → WGS84
96+
gdf_centroids["geometry"] = geoseries_to_geodetic(gdf_centroids.geometry)
97+
gdf_centroids["lat"] = gdf_centroids.geometry.y
98+
gdf_centroids["lng"] = gdf_centroids.geometry.x
99+
print(gdf_centroids[["name", "lat", "lng"]])
96100
```
97101

98-
Alternatively, generate the full cell polygon and take its centroid:
102+
To get the full cell polygons instead:
99103

100104
```python
101105
gdf_cells = dggrid.grid_cell_polygons_from_cellids(
102106
cell_id_list=cell_ids,
103107
dggs_type="IGEO7",
104108
resolution=5,
105-
input_address_type="Z7_STRING",
109+
**IGEO7_META,
106110
)
107-
gdf_cells["centroid"] = gdf_cells.geometry.centroid
108-
gdf_cells["lat"] = gdf_cells["centroid"].y
109-
gdf_cells["lng"] = gdf_cells["centroid"].x
110-
print(gdf_cells[["global_id", "lat", "lng"]])
111+
gdf_cells["geometry"] = geoseries_to_geodetic(gdf_cells.geometry)
112+
print(gdf_cells[["name", "geometry"]])
111113
```
112114

113115
---
@@ -122,7 +124,7 @@ These functions convert between the three Z7 representations without any DGGRID
122124
from dggrid4py import igeo7
123125

124126
# Hex ↔ Z7 string
125-
z7_str = igeo7.z7hex_to_z7string("0042aad3ffffffff") # "090625251"
127+
z7_str = igeo7.z7hex_to_z7string("0042aad3ffffffff") # "00010252551"
126128
z7_int = igeo7.z7hex_to_z7int("0042aad3ffffffff") # integer
127129
z7_hex = igeo7.z7int_to_z7hex(z7_int) # back to hex string
128130
```
@@ -132,8 +134,8 @@ z7_hex = igeo7.z7int_to_z7hex(z7_int) # back to hex string
132134
```julia
133135
using IGEO7
134136

135-
idx = z7string_to_index("0800433") # Z7IndexUInt64
136-
str = index_to_z7string(idx) # "0800433"
137+
idx = z7string_to_index("0001022") # Z7IndexUInt64
138+
str = index_to_z7string(idx) # "0001022"
137139
hex = z7int_to_z7hex(idx.raw) # hex string
138140
int_val = z7hex_to_z7int("0042aad3ffffffff") # UInt64
139141
```

‎website/docs/api/inspection.md‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ from dggrid4py import igeo7
2525
res = igeo7.get_z7hex_resolution("0042aad3ffffffff") # 9
2626

2727
# From Z7 string
28-
res = igeo7.get_z7string_resolution("0800433") # 5
28+
res = igeo7.get_z7string_resolution("0001022") # 5
2929
```
3030

3131
Resolution is the count of valid digits (values 0–6) before the first sentinel (`7`). For the Z7 string format it is simply `len(z7_string) - 2`.
@@ -34,7 +34,7 @@ Resolution is the count of valid digits (values 0–6) before the first sentinel
3434

3535
```julia
3636
using IGEO7
37-
idx = z7string_to_index("0800433")
37+
idx = z7string_to_index("0001022")
3838
get_resolution(idx) # 5
3939
```
4040

@@ -59,16 +59,16 @@ base_cell, digits = igeo7.decode_z7hex_index("0042aad3ffffffff")
5959
print(base_cell) # 0
6060

6161
# From Z7 string: first two characters
62-
z7_str = "0800433"
63-
base_cell = int(z7_str[:2]) # 8
62+
z7_str = "0001022"
63+
base_cell = int(z7_str[:2]) # 0
6464
```
6565

6666
### Julia
6767

6868
```julia
6969
using IGEO7
70-
idx = z7string_to_index("0800433")
71-
get_base_cell(idx) # 0x08 (UInt8)
70+
idx = z7string_to_index("0001022")
71+
get_base_cell(idx) # 0x00 (UInt8)
7272
```
7373

7474
---
@@ -93,8 +93,8 @@ print(valid) # [0, 1, 0, 2, 5, 2, 5, 5, 1] (9 digits → resolution 9)
9393

9494
```julia
9595
using IGEO7
96-
idx = z7string_to_index("0800433")
97-
get_digits(idx) # (0, 0, 4, 3, 3, 7, 7, 7, ..., 7) — tuple of 20
96+
idx = z7string_to_index("0001022")
97+
get_digits(idx) # (0, 1, 0, 2, 2, 7, 7, 7, ..., 7) — tuple of 20
9898
```
9999

100100
---
@@ -152,15 +152,15 @@ def is_pentagon(z7_str: str) -> bool:
152152
# Pentagon cells have only digit '0' after the base cell
153153
return all(c == '0' for c in z7_str[2:])
154154

155-
print(is_pentagon("0800000")) # True — resolution 5 pentagon
156-
print(is_pentagon("0800433")) # False — regular hexagon
155+
print(is_pentagon("0000000")) # True — resolution 5 pentagon
156+
print(is_pentagon("0001022")) # False — regular hexagon
157157
```
158158

159159
### Julia
160160

161161
```julia
162162
using IGEO7
163-
idx = z7string_to_index("0800000")
163+
idx = z7string_to_index("0000000")
164164
digits = get_digits(idx)
165165
res = get_resolution(idx)
166166
all(digits[1:res] .== 0x00) # true → pentagon
@@ -191,18 +191,18 @@ from dggrid4py import igeo7
191191
parent, digit, is_center = igeo7.get_z7hex_local_pos("0042aad3ffffffff")
192192

193193
# From Z7 string
194-
parent, digit, is_center = igeo7.get_z7string_local_pos("0800433")
195-
print(parent) # "080043"
196-
print(digit) # "3"
194+
parent, digit, is_center = igeo7.get_z7string_local_pos("0001022")
195+
print(parent) # "000102"
196+
print(digit) # "2"
197197
print(is_center) # False
198198
```
199199

200200
### Julia
201201

202202
```julia
203203
using IGEO7
204-
z7_str = index_to_z7string(z7string_to_index("0800433"))
205-
parent = z7_str[1:end-1] # "080043"
206-
digit = z7_str[end] # '3'
204+
z7_str = index_to_z7string(z7string_to_index("0001022"))
205+
parent = z7_str[1:end-1] # "000102"
206+
digit = z7_str[end] # '2'
207207
is_center = digit == '0'
208208
```

0 commit comments

Comments
 (0)