Skip to content

Commit 6d8d587

Browse files
authored
COMPAT: h3 v4 compatibility (#768)
1 parent e23998a commit 6d8d587

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

ci/310-oldest.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies:
3030
- zstd
3131
- xarray=2022.3
3232
- pandana
33+
- h3-py<4
3334
- pip
3435
- pip:
3536
- pulp

libpysal/graph/_indices.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from packaging.version import Version
2+
3+
14
def _build_from_h3(ids, order=1):
25
"""Generate Graph from H3 hexagons.
36
@@ -30,14 +33,23 @@ def _build_from_h3(ids, order=1):
3033

3134
neighbors = {}
3235
weights = {}
33-
for ix in ids:
34-
rings = h3.hex_range_distances(ix, order)
35-
for i, ring in enumerate(rings):
36-
if i == 0:
37-
neighbors[ix] = []
38-
weights[ix] = []
39-
else:
36+
if Version(h3.__version__) > Version("4.0"):
37+
for ix in ids:
38+
neighbors[ix] = []
39+
weights[ix] = []
40+
for i in range(1, order + 1):
41+
ring = h3.grid_ring(ix, i)
4042
neighbors[ix].extend(list(ring))
4143
weights[ix].extend([i] * len(ring))
44+
else:
45+
for ix in ids:
46+
rings = h3.hex_range_distances(ix, order)
47+
for i, ring in enumerate(rings):
48+
if i == 0:
49+
neighbors[ix] = []
50+
weights[ix] = []
51+
else:
52+
neighbors[ix].extend(list(ring))
53+
weights[ix].extend([i] * len(ring))
4254

4355
return neighbors, weights

0 commit comments

Comments
 (0)