Created
May 28, 2024 03:29
-
-
Save ChocopieKewpie/05794de43c32a2c92242a2a5a971ceba to your computer and use it in GitHub Desktop.
H3 uber DGGS. Simple script to return a dataframe with neighbourhood values, k_ring= 1
This file contains hidden or 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
""" | |
Simple script to return a dataframe with neighbourhood values, k_ring= 1 | |
""" | |
import h3 | |
import pandas as pd | |
import h3pandas | |
def get_h3_neighbour(h3_index): | |
# Get the 6 neighbors for the given H3 cell | |
neighbors = h3.k_ring(h3_index, 1) | |
neighbors.remove(h3_index) # Remove the center cell itself | |
return list(neighbors) | |
# Sample H3 cells and their data | |
data = { | |
'h3_index': [ | |
'87bb0d8caffffff', | |
'87bb0d8d9ffffff', | |
'87bb0d8ddffffff', | |
'87bb0d8c8ffffff' | |
], | |
'data': [1, 10, 6, 20] | |
} | |
df = pd.DataFrame(data) | |
def get_neighbors_data(h3_index, df): | |
neighbours = get_h3_neighbour(h3_index) | |
neighbours_data = [] | |
for neighbour in neighbours: | |
if neighbour in df['h3_index'].values: | |
neighbours_data.append(df[df['h3_index'] == neighbour]['data'].values[0]) | |
else: | |
neighbours_data.append(None) | |
return neighbours_data | |
# Apply the function to the DataFrame | |
df['neighbors_data'] = df['h3_index'].apply(lambda x: get_neighbors_data(x, df)) | |
df=df.set_index('h3_index') | |
h3=df.h3.h3_to_geo_boundary() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment