Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build Graph from H3 #667

Closed
martinfleis opened this issue Dec 14, 2023 · 3 comments · Fixed by #694
Closed

Build Graph from H3 #667

martinfleis opened this issue Dec 14, 2023 · 3 comments · Fixed by #694

Comments

@martinfleis
Copy link
Member

I have an implementation of building a (higher-order) contiguity Graph based on H3 index. I'd like to expose it as a builder on `Graph, so dumping the code below before I'll forget.

import h3

from libpysal import graph

data = ... # dataframe with H3 IDs as an index, like those coming from tobler h3fy
order = 5

neighbors = {}
weights = {}
for ix in data.index:
   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))

matrix = graph.Graph.from_dicts(neighbors, weights)

It encodes the order as weight now, I was casting to a binary graph and used topological inverse weighting based on that (inverse = graph.Graph(1 / matrix.adjacency)). We can figure out what the API should be once the PR is alive?

I also think that the latest beta release of h3 completely changed the API so the h3 calls will need to change a bit but it's been beta for a year.

based on https://github.com/Urban-Analytics-Technology-Platform/demoland-project/blob/main/code/04_generalisation/create_spatial_weights.ipynb

@ljwolf
Copy link
Member

ljwolf commented Dec 14, 2023

It would be interesting to try to generalise this for many of the common hierarchical indices...

@knaaptime
Copy link
Member

I also think that the latest beta release of h3 completely changed the API so the h3 calls will need to change a bit but it's been beta for a year.

you think that's related to these failures somehow? not sure what would've changed

@martinfleis
Copy link
Member Author

@knaaptime no, the version of h3 has not changed in those test envs. neither did GEOS. not sure where that comes from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants