Skip to content

Commit

Permalink
COMPAT: h3 v4 compatibility (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfleis authored Nov 15, 2024
1 parent e23998a commit 6d8d587
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions ci/310-oldest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies:
- zstd
- xarray=2022.3
- pandana
- h3-py<4
- pip
- pip:
- pulp
Expand Down
26 changes: 19 additions & 7 deletions libpysal/graph/_indices.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from packaging.version import Version


def _build_from_h3(ids, order=1):
"""Generate Graph from H3 hexagons.
Expand Down Expand Up @@ -30,14 +33,23 @@ def _build_from_h3(ids, order=1):

neighbors = {}
weights = {}
for ix in ids:
rings = h3.hex_range_distances(ix, order)
for i, ring in enumerate(rings):
if i == 0:
neighbors[ix] = []
weights[ix] = []
else:
if Version(h3.__version__) > Version("4.0"):
for ix in ids:
neighbors[ix] = []
weights[ix] = []
for i in range(1, order + 1):
ring = h3.grid_ring(ix, i)
neighbors[ix].extend(list(ring))
weights[ix].extend([i] * len(ring))
else:
for ix in ids:
rings = h3.hex_range_distances(ix, order)
for i, ring in enumerate(rings):
if i == 0:
neighbors[ix] = []
weights[ix] = []
else:
neighbors[ix].extend(list(ring))
weights[ix].extend([i] * len(ring))

return neighbors, weights

0 comments on commit 6d8d587

Please sign in to comment.