Overriding include center
In [1]:
Copied!
from srai.neighbourhoods import H3Neighbourhood
from srai.neighbourhoods import H3Neighbourhood
Overriding include_center¶
The default behavior of all Neighbourhoods is not to include the center region. This can be overridden by setting include_center=True.
It can be done in:
Neighbourhood's__init__method,- all
Neighbourhood's methods:get_neighboursandget_neighbours_up_to_distance- include the center region in the output,get_neighbours_at_distance- include the center region in the output ifdistance=0.
Overriding using method parameter¶
The default behaviour is not to include the center region in the output.
In [2]:
Copied!
neighbourhood_with_regions = H3Neighbourhood()
region_id = "881e204089fffff"
neighbours_ids = neighbourhood_with_regions.get_neighbours(region_id)
print(f"Center region included: {region_id in neighbours_ids}")
neighbours_ids
neighbourhood_with_regions = H3Neighbourhood()
region_id = "881e204089fffff"
neighbours_ids = neighbourhood_with_regions.get_neighbours(region_id)
print(f"Center region included: {region_id in neighbours_ids}")
neighbours_ids
Center region included: False
Out[2]:
{'881e204081fffff',
'881e20408bfffff',
'881e20408dfffff',
'881e2040c3fffff',
'881e2040c7fffff',
'881e2040d5fffff'}
You can override it by setting include_center=True in the method call.
In [3]:
Copied!
neighbours_ids = neighbourhood_with_regions.get_neighbours(region_id, include_center=True)
print(f"Center region included: {region_id in neighbours_ids}")
neighbours_ids
neighbours_ids = neighbourhood_with_regions.get_neighbours(region_id, include_center=True)
print(f"Center region included: {region_id in neighbours_ids}")
neighbours_ids
Center region included: True
Out[3]:
{'881e204081fffff',
'881e204089fffff',
'881e20408bfffff',
'881e20408dfffff',
'881e2040c3fffff',
'881e2040c7fffff',
'881e2040d5fffff'}
Overriding using init parameter¶
If including the center region by default is the desired behaviour, it can be set in the Neighbourhood's __init__ method.
In [4]:
Copied!
neighbourhood_with_regions = H3Neighbourhood(include_center=True)
neighbours_ids = neighbourhood_with_regions.get_neighbours(region_id)
print(f"Center region included: {region_id in neighbours_ids}")
neighbours_ids
neighbourhood_with_regions = H3Neighbourhood(include_center=True)
neighbours_ids = neighbourhood_with_regions.get_neighbours(region_id)
print(f"Center region included: {region_id in neighbours_ids}")
neighbours_ids
Center region included: True
Out[4]:
{'881e204081fffff',
'881e204089fffff',
'881e20408bfffff',
'881e20408dfffff',
'881e2040c3fffff',
'881e2040c7fffff',
'881e2040d5fffff'}
You can still override it afterwards by setting include_center=False in the method call.
In [5]:
Copied!
neighbours_ids = neighbourhood_with_regions.get_neighbours(region_id, include_center=False)
print(f"Center region included: {region_id in neighbours_ids}")
neighbours_ids
neighbours_ids = neighbourhood_with_regions.get_neighbours(region_id, include_center=False)
print(f"Center region included: {region_id in neighbours_ids}")
neighbours_ids
Center region included: False
Out[5]:
{'881e204081fffff',
'881e20408bfffff',
'881e20408dfffff',
'881e2040c3fffff',
'881e2040c7fffff',
'881e2040d5fffff'}
Neighbours up to distance¶
The above applies to all Neighbourhood's methods.
In [6]:
Copied!
neighbours_ids = neighbourhood_with_regions.get_neighbours_up_to_distance(region_id, 2)
print(f"Center region included: {region_id in neighbours_ids}")
neighbours_ids
neighbours_ids = neighbourhood_with_regions.get_neighbours_up_to_distance(region_id, 2)
print(f"Center region included: {region_id in neighbours_ids}")
neighbours_ids
Center region included: True
Out[6]:
{'881e204081fffff',
'881e204083fffff',
'881e204085fffff',
'881e204087fffff',
'881e204089fffff',
'881e20408bfffff',
'881e20408dfffff',
'881e20409dfffff',
'881e2040c1fffff',
'881e2040c3fffff',
'881e2040c5fffff',
'881e2040c7fffff',
'881e2040cbfffff',
'881e2040d1fffff',
'881e2040d5fffff',
'881e2040d7fffff',
'881e2040ddfffff',
'881e2040e3fffff',
'881e2040ebfffff'}
In [7]:
Copied!
neighbours_ids = neighbourhood_with_regions.get_neighbours_up_to_distance(
region_id, 2, include_center=False
)
print(f"Center region included: {region_id in neighbours_ids}")
neighbours_ids
neighbours_ids = neighbourhood_with_regions.get_neighbours_up_to_distance(
region_id, 2, include_center=False
)
print(f"Center region included: {region_id in neighbours_ids}")
neighbours_ids
Center region included: False
Out[7]:
{'881e204081fffff',
'881e204083fffff',
'881e204085fffff',
'881e204087fffff',
'881e20408bfffff',
'881e20408dfffff',
'881e20409dfffff',
'881e2040c1fffff',
'881e2040c3fffff',
'881e2040c5fffff',
'881e2040c7fffff',
'881e2040cbfffff',
'881e2040d1fffff',
'881e2040d5fffff',
'881e2040d7fffff',
'881e2040ddfffff',
'881e2040e3fffff',
'881e2040ebfffff'}