From e8c0da0825e72db32fd3ae7d570b15fbebbe0314 Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Mon, 21 Oct 2024 20:41:38 +0200 Subject: [PATCH] COMPAT: h3 v4 compatibility --- ci/310-oldest.yaml | 1 + libpysal/graph/_indices.py | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/ci/310-oldest.yaml b/ci/310-oldest.yaml index bfdb8e42b..21688dcb9 100644 --- a/ci/310-oldest.yaml +++ b/ci/310-oldest.yaml @@ -30,6 +30,7 @@ dependencies: - zstd - xarray=2022.3 - pandana + - h3-py<4 - pip - pip: - pulp diff --git a/libpysal/graph/_indices.py b/libpysal/graph/_indices.py index bbb7e3678..b7e5cef89 100644 --- a/libpysal/graph/_indices.py +++ b/libpysal/graph/_indices.py @@ -1,3 +1,6 @@ +from packaging.version import Version + + def _build_from_h3(ids, order=1): """Generate Graph from H3 hexagons. @@ -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