Adjacency neighbourhood
In [1]:
Copied!
import numpy as np
from shapely.geometry import Point
import geopandas as gpd
from srai.constants import WGS84_CRS
from srai.neighbourhoods import AdjacencyNeighbourhood
from srai.regionalizers import (
AdministrativeBoundaryRegionalizer,
VoronoiRegionalizer,
geocode_to_region_gdf,
)
from srai.plotting.folium_wrapper import plot_regions, plot_neighbours, plot_all_neighbourhood
import numpy as np
from shapely.geometry import Point
import geopandas as gpd
from srai.constants import WGS84_CRS
from srai.neighbourhoods import AdjacencyNeighbourhood
from srai.regionalizers import (
AdministrativeBoundaryRegionalizer,
VoronoiRegionalizer,
geocode_to_region_gdf,
)
from srai.plotting.folium_wrapper import plot_regions, plot_neighbours, plot_all_neighbourhood
Adjacency Neighbourhood¶
It can generate neighbourhoods for all geodataframes with touching geometries.
Real boundaries example - Italy¶
In [2]:
Copied!
it_gdf = geocode_to_region_gdf(query=["R365331"], by_osmid=True)
plot_regions(it_gdf)
it_gdf = geocode_to_region_gdf(query=["R365331"], by_osmid=True)
plot_regions(it_gdf)
Out[2]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [3]:
Copied!
regionalizer = AdministrativeBoundaryRegionalizer(admin_level=4)
it_regions_gdf = regionalizer.transform(it_gdf)
regionalizer = AdministrativeBoundaryRegionalizer(admin_level=4)
it_regions_gdf = regionalizer.transform(it_gdf)
Loading boundaries: 68: 100%|██████████| 6/6 [00:26<00:00, 4.50s/it] /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/srai/regionalizers/administrative_boundary_regionalizer.py:167: FutureWarning: `unary_union` returned None due to all-None GeoSeries. In future, `unary_union` will return 'GEOMETRYCOLLECTION EMPTY' instead. ].geometry.unary_union
In [4]:
Copied!
plot_regions(it_regions_gdf)
plot_regions(it_regions_gdf)
Out[4]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [5]:
Copied!
neighbourhood = AdjacencyNeighbourhood(it_regions_gdf)
neighbourhood = AdjacencyNeighbourhood(it_regions_gdf)
Nearest neighbours¶
In [6]:
Copied!
region_id = "Lazio"
neighbours = neighbourhood.get_neighbours(region_id)
neighbours
region_id = "Lazio"
neighbours = neighbourhood.get_neighbours(region_id)
neighbours
Out[6]:
{'Abruzzo', 'Campania', 'Marche', 'Molise', 'Tuscany', 'Umbria'}
In [7]:
Copied!
plot_neighbours(it_regions_gdf, region_id, neighbours)
plot_neighbours(it_regions_gdf, region_id, neighbours)
Out[7]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Neighbours at a distance¶
In [8]:
Copied!
region_id = "Basilicata"
neighbours = neighbourhood.get_neighbours_at_distance(region_id, 2)
neighbours
region_id = "Basilicata"
neighbours = neighbourhood.get_neighbours_at_distance(region_id, 2)
neighbours
Out[8]:
{'Lazio', 'Molise'}
In [9]:
Copied!
plot_neighbours(it_regions_gdf, region_id, neighbours)
plot_neighbours(it_regions_gdf, region_id, neighbours)
Out[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Regions without neighbours¶
In [10]:
Copied!
region_id = "Sardinia"
neighbours = neighbourhood.get_neighbours(region_id)
neighbours
region_id = "Sardinia"
neighbours = neighbourhood.get_neighbours(region_id)
neighbours
Out[10]:
set()
In [11]:
Copied!
plot_neighbours(it_regions_gdf, region_id, neighbours)
plot_neighbours(it_regions_gdf, region_id, neighbours)
Out[11]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Plotting all neighbourhood¶
In [12]:
Copied!
region_id = "Campania"
plot_all_neighbourhood(it_regions_gdf, region_id, neighbourhood)
region_id = "Campania"
plot_all_neighbourhood(it_regions_gdf, region_id, neighbourhood)
Out[12]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Voronoi example - Australia¶
In [13]:
Copied!
au_gdf = geocode_to_region_gdf(query=["R80500"], by_osmid=True)
plot_regions(au_gdf)
au_gdf = geocode_to_region_gdf(query=["R80500"], by_osmid=True)
plot_regions(au_gdf)
Out[13]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [14]:
Copied!
import numpy as np
from shapely.geometry import Point
import geopandas as gpd
from srai.constants import WGS84_CRS
def generate_random_points(shape, n_points=500):
minx, miny, maxx, maxy = shape.bounds
pts = []
while len(pts) < 4:
randx = np.random.uniform(minx, maxx, n_points)
randy = np.random.uniform(miny, maxy, n_points)
coords = np.vstack((randx, randy)).T
# use only the points inside the geographic area
pts = [p for p in list(map(Point, coords)) if p.within(shape)]
del coords # not used any more
return pts
import numpy as np
from shapely.geometry import Point
import geopandas as gpd
from srai.constants import WGS84_CRS
def generate_random_points(shape, n_points=500):
minx, miny, maxx, maxy = shape.bounds
pts = []
while len(pts) < 4:
randx = np.random.uniform(minx, maxx, n_points)
randy = np.random.uniform(miny, maxy, n_points)
coords = np.vstack((randx, randy)).T
# use only the points inside the geographic area
pts = [p for p in list(map(Point, coords)) if p.within(shape)]
del coords # not used any more
return pts
In [15]:
Copied!
pts = generate_random_points(au_gdf.geometry[0])
au_seeds_gdf = gpd.GeoDataFrame(
{"geometry": pts},
index=list(range(len(pts))),
crs=WGS84_CRS,
)
pts = generate_random_points(au_gdf.geometry[0])
au_seeds_gdf = gpd.GeoDataFrame(
{"geometry": pts},
index=list(range(len(pts))),
crs=WGS84_CRS,
)
In [16]:
Copied!
vr = VoronoiRegionalizer(seeds=au_seeds_gdf)
au_result_gdf = vr.transform(gdf=au_gdf)
vr = VoronoiRegionalizer(seeds=au_seeds_gdf)
au_result_gdf = vr.transform(gdf=au_gdf)
Generating spherical polygons: 100%|██████████| 86/86 [00:01<00:00, 52.50it/s] Interpolating edges: 100%|██████████| 329/329 [00:00<00:00, 903.54it/s] Generating polygons: 100%|██████████| 86/86 [00:00<00:00, 545.37it/s]
In [17]:
Copied!
folium_map = plot_regions(au_result_gdf, tiles_style="CartoDB positron")
au_seeds_gdf.explore(
m=folium_map,
style_kwds=dict(color="#444", opacity=1, fillColor="#f2f2f2", fillOpacity=1),
marker_kwds=dict(radius=3),
)
folium_map = plot_regions(au_result_gdf, tiles_style="CartoDB positron")
au_seeds_gdf.explore(
m=folium_map,
style_kwds=dict(color="#444", opacity=1, fillColor="#f2f2f2", fillOpacity=1),
marker_kwds=dict(radius=3),
)
Out[17]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [18]:
Copied!
neighbourhood = AdjacencyNeighbourhood(regions_gdf=au_result_gdf)
neighbourhood = AdjacencyNeighbourhood(regions_gdf=au_result_gdf)
Nearest neighbours¶
In [19]:
Copied!
region_id = 0
neighbours = neighbourhood.get_neighbours(region_id)
neighbours
region_id = 0
neighbours = neighbourhood.get_neighbours(region_id)
neighbours
Out[19]:
{4, 13, 30, 57}
In [20]:
Copied!
plot_neighbours(au_result_gdf, region_id, neighbours)
plot_neighbours(au_result_gdf, region_id, neighbours)
Out[20]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Neighbours at a distance¶
In [21]:
Copied!
region_id = 0
neighbours = neighbourhood.get_neighbours_at_distance(region_id, 3)
neighbours
region_id = 0
neighbours = neighbourhood.get_neighbours_at_distance(region_id, 3)
neighbours
Out[21]:
{1, 5, 25, 29, 34, 36, 40, 58, 59, 60, 71, 74, 83}
In [22]:
Copied!
plot_neighbours(au_result_gdf, region_id, neighbours)
plot_neighbours(au_result_gdf, region_id, neighbours)
Out[22]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Plotting all neighbourhood¶
In [23]:
Copied!
region_id = 0
plot_all_neighbourhood(au_result_gdf, region_id, neighbourhood)
region_id = 0
plot_all_neighbourhood(au_result_gdf, region_id, neighbourhood)
Out[23]:
Make this Notebook Trusted to load map: File -> Trust Notebook