-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ENH: add Graph.build_h3 * lint * don't test with oldest
- Loading branch information
1 parent
798ed8a
commit 3afbab7
Showing
7 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ dependencies: | |
# testing | ||
- codecov | ||
- matplotlib | ||
- tobler | ||
- h3-py | ||
- pytest | ||
- pytest-cov | ||
- pytest-mpl | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ dependencies: | |
# testing | ||
- codecov | ||
- matplotlib | ||
- tobler | ||
- h3-py | ||
- pytest | ||
- pytest-cov | ||
- pytest-mpl | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ dependencies: | |
# testing | ||
- codecov | ||
- matplotlib | ||
- tobler | ||
- h3-py | ||
- pytest | ||
- pytest-cov | ||
- pytest-mpl | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ dependencies: | |
# testing | ||
- codecov | ||
- matplotlib | ||
- tobler | ||
- h3-py | ||
- pytest | ||
- pytest-cov | ||
- pytest-mpl | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
def _build_from_h3(ids, order=1): | ||
"""Generate Graph from H3 hexagons. | ||
Encode a graph from a set of H3 hexagons. The graph is generated by | ||
considering the H3 hexagons as nodes and connecting them based on their | ||
contiguity. The contiguity is defined by the order parameter, which | ||
specifies the number of steps to consider as neighbors. | ||
Requires the `h3` library. | ||
Parameters | ||
---------- | ||
ids : array-like | ||
Array of H3 IDs encoding focal geometries | ||
order : int, optional | ||
Order of contiguity, by default 1 | ||
Returns | ||
------- | ||
tuple(dict, dict) | ||
""" | ||
try: | ||
import h3 | ||
except ImportError as e: | ||
raise ImportError( | ||
"This function requires the `h3` library. " | ||
"You can install it with `conda install h3-py` or " | ||
"`pip install h3`." | ||
) from e | ||
|
||
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: | ||
neighbors[ix].extend(list(ring)) | ||
weights[ix].extend([i] * len(ring)) | ||
|
||
return neighbors, weights |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import geopandas as gpd | ||
import pytest | ||
from geodatasets import get_path | ||
|
||
from libpysal import graph | ||
|
||
pytest.importorskip("h3") | ||
pytest.importorskip("tobler") | ||
|
||
|
||
class TestH3: | ||
def setup_method(self): | ||
from tobler.util import h3fy | ||
|
||
gdf = gpd.read_file(get_path("geoda guerry")) | ||
h3_geoms = h3fy(gdf, resolution=4) | ||
self.h3_ids = h3_geoms.index | ||
|
||
def test_h3(self): | ||
g = graph.Graph.build_h3(self.h3_ids) | ||
assert g.n == len(self.h3_ids) | ||
assert g.pct_nonzero == 1.69921875 | ||
assert len(g) == 1740 | ||
assert g.adjacency.max() == 1 | ||
|
||
@pytest.mark.parametrize("order", range(2, 6)) | ||
def test_h3_order(self, order): | ||
g = graph.Graph.build_h3(self.h3_ids, order) | ||
assert g.n == len(self.h3_ids) | ||
assert g.adjacency.max() == order | ||
|
||
def test_h3_binary(self): | ||
g = graph.Graph.build_h3(self.h3_ids, order=4, weight="binary") | ||
assert g.n == len(self.h3_ids) | ||
assert g.adjacency.max() == 1 |