H3 neighbourhood
In [1]:
Copied!
from srai.neighbourhoods import H3Neighbourhood
from srai.regionalizers import H3Regionalizer, geocode_to_region_gdf
from srai.plotting.folium_wrapper import plot_neighbours, plot_all_neighbourhood
from srai.neighbourhoods import H3Neighbourhood
from srai.regionalizers import H3Regionalizer, geocode_to_region_gdf
from srai.plotting.folium_wrapper import plot_neighbours, plot_all_neighbourhood
In [2]:
Copied!
gdf_sf = geocode_to_region_gdf("Wrocław, PL")
regions_gdf = H3Regionalizer(8).transform(gdf_sf)
gdf_sf = geocode_to_region_gdf("Wrocław, PL")
regions_gdf = H3Regionalizer(8).transform(gdf_sf)
Region somewhere in the middle of the area under analysis¶
Direct neighbours¶
In [3]:
Copied!
neighbourhood_with_regions = H3Neighbourhood(regions_gdf)
region_id = "881e204089fffff"
neighbours_ids = neighbourhood_with_regions.get_neighbours(region_id)
plot_neighbours(regions_gdf, region_id, neighbours_ids)
neighbourhood_with_regions = H3Neighbourhood(regions_gdf)
region_id = "881e204089fffff"
neighbours_ids = neighbourhood_with_regions.get_neighbours(region_id)
plot_neighbours(regions_gdf, region_id, neighbours_ids)
Out[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Only 3rd ring¶
In [4]:
Copied!
neighbours_ids = neighbourhood_with_regions.get_neighbours_at_distance(region_id, 3)
plot_neighbours(regions_gdf, region_id, neighbours_ids)
neighbours_ids = neighbourhood_with_regions.get_neighbours_at_distance(region_id, 3)
plot_neighbours(regions_gdf, region_id, neighbours_ids)
Out[4]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Regions up to distance 3¶
In [5]:
Copied!
neighbours_ids = neighbourhood_with_regions.get_neighbours_up_to_distance(region_id, 3)
plot_neighbours(regions_gdf, region_id, neighbours_ids)
neighbours_ids = neighbourhood_with_regions.get_neighbours_up_to_distance(region_id, 3)
plot_neighbours(regions_gdf, region_id, neighbours_ids)
Out[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Full neighbourhood¶
In [6]:
Copied!
plot_all_neighbourhood(regions_gdf, region_id, neighbourhood_with_regions)
plot_all_neighbourhood(regions_gdf, region_id, neighbourhood_with_regions)
Out[6]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Edge region¶
The Neighbourhood only returns the indices of available regions if provided with regions_gdf
In [7]:
Copied!
edge_region_id = "881e2050bdfffff"
neighbours_ids = neighbourhood_with_regions.get_neighbours(edge_region_id)
print(neighbours_ids)
edge_region_id = "881e2050bdfffff"
neighbours_ids = neighbourhood_with_regions.get_neighbours(edge_region_id)
print(neighbours_ids)
{'881e2050abfffff', '881e2050b5fffff', '881e205087fffff', '881e2050a3fffff'}
In [8]:
Copied!
plot_neighbours(regions_gdf, edge_region_id, neighbours_ids)
plot_neighbours(regions_gdf, edge_region_id, neighbours_ids)
Out[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook
On the other hand, an "unbiased" H3Neighbourhood returns all 6 neighbours from H3.
In [9]:
Copied!
neighbourhood_without_regions = H3Neighbourhood()
neighbourhood_without_regions.get_neighbours(edge_region_id)
neighbourhood_without_regions = H3Neighbourhood()
neighbourhood_without_regions.get_neighbours(edge_region_id)
Out[9]:
{'881e205087fffff', '881e2050a3fffff', '881e2050abfffff', '881e2050b1fffff', '881e2050b5fffff', '881e2050b9fffff'}
Regions with a gap¶
One thing to note when using the H3Neighbourhood is that it uses the h3 library under the hood. The only post-processing step is to select from among the regions under analysis. Due to that some neighbours can be included as K-th neighbours, even if there is no path of length K between them and the regions you ask for. This is because H3 still treats them as a part of the K-th ring. An example is shown below
In [10]:
Copied!
edge_region_id = "881e2050bdfffff"
neighbours_ids = neighbourhood_with_regions.get_neighbours_at_distance(edge_region_id, 2)
print(neighbours_ids)
edge_region_id = "881e2050bdfffff"
neighbours_ids = neighbourhood_with_regions.get_neighbours_at_distance(edge_region_id, 2)
print(neighbours_ids)
{'881e2050b7fffff', '881e2050a7fffff', '881e204249fffff', '881e2050a1fffff', '881e2050b3fffff', '881e20424bfffff'}
In [11]:
Copied!
plot_neighbours(regions_gdf, edge_region_id, neighbours_ids)
plot_neighbours(regions_gdf, edge_region_id, neighbours_ids)
Out[11]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [12]:
Copied!
plot_all_neighbourhood(regions_gdf, edge_region_id, neighbourhood_with_regions)
plot_all_neighbourhood(regions_gdf, edge_region_id, neighbourhood_with_regions)
Out[12]:
Make this Notebook Trusted to load map: File -> Trust Notebook