Intersection joiner
In [1]:
Copied!
import geopandas as gpd
from shapely import geometry
from srai.constants import FEATURES_INDEX, REGIONS_INDEX, WGS84_CRS
from srai.plotting.folium_wrapper import plot_regions
import geopandas as gpd
from shapely import geometry
from srai.constants import FEATURES_INDEX, REGIONS_INDEX, WGS84_CRS
from srai.plotting.folium_wrapper import plot_regions
Define example regions and features¶
In [2]:
Copied!
regions = gpd.GeoDataFrame(
geometry=[
geometry.Polygon([(-1, 0), (-1, -1), (0, -1), (0, 0)]),
geometry.Polygon([(1, 0), (1, 1), (0, 1), (0, 0)]),
geometry.Polygon([(-2, -1), (-2, -2), (-1, -2), (-1, -1)]),
geometry.Polygon([(-2, 0.5), (-2, -0.5), (-1, -0.5), (-1, 0.5)]),
],
crs=WGS84_CRS,
index=gpd.pd.Index(name=REGIONS_INDEX, data=[1, 2, 3, 4]),
)
features = gpd.GeoDataFrame(
geometry=[
geometry.Polygon([(-1.5, 0.5), (-1.5, 0), (-0.5, 0), (-0.5, 0.5)]),
geometry.Polygon([(-1.5, -1.5), (-1.5, -2.5), (-0.5, -2.5), (-0.5, -1.5)]),
geometry.Point((0, 0)),
geometry.Point((-0.5, -0.5)),
],
crs=WGS84_CRS,
index=gpd.pd.Index(name=FEATURES_INDEX, data=[1, 2, 3, 4]),
)
regions = gpd.GeoDataFrame(
geometry=[
geometry.Polygon([(-1, 0), (-1, -1), (0, -1), (0, 0)]),
geometry.Polygon([(1, 0), (1, 1), (0, 1), (0, 0)]),
geometry.Polygon([(-2, -1), (-2, -2), (-1, -2), (-1, -1)]),
geometry.Polygon([(-2, 0.5), (-2, -0.5), (-1, -0.5), (-1, 0.5)]),
],
crs=WGS84_CRS,
index=gpd.pd.Index(name=REGIONS_INDEX, data=[1, 2, 3, 4]),
)
features = gpd.GeoDataFrame(
geometry=[
geometry.Polygon([(-1.5, 0.5), (-1.5, 0), (-0.5, 0), (-0.5, 0.5)]),
geometry.Polygon([(-1.5, -1.5), (-1.5, -2.5), (-0.5, -2.5), (-0.5, -1.5)]),
geometry.Point((0, 0)),
geometry.Point((-0.5, -0.5)),
],
crs=WGS84_CRS,
index=gpd.pd.Index(name=FEATURES_INDEX, data=[1, 2, 3, 4]),
)
In [3]:
Copied!
regions
regions
Out[3]:
geometry | |
---|---|
region_id | |
1 | POLYGON ((-1.00000 0.00000, -1.00000 -1.00000,... |
2 | POLYGON ((1.00000 0.00000, 1.00000 1.00000, 0.... |
3 | POLYGON ((-2.00000 -1.00000, -2.00000 -2.00000... |
4 | POLYGON ((-2.00000 0.50000, -2.00000 -0.50000,... |
In [4]:
Copied!
features
features
Out[4]:
geometry | |
---|---|
feature_id | |
1 | POLYGON ((-1.50000 0.50000, -1.50000 0.00000, ... |
2 | POLYGON ((-1.50000 -1.50000, -1.50000 -2.50000... |
3 | POINT (0.00000 0.00000) |
4 | POINT (-0.50000 -0.50000) |
In [5]:
Copied!
folium_map = plot_regions(regions, colormap=["royalblue"])
features.explore(
m=folium_map,
style_kwds=dict(color="red", opacity=0.8, fillColor="red", fillOpacity=0.5),
marker_kwds=dict(radius=3),
)
folium_map = plot_regions(regions, colormap=["royalblue"])
features.explore(
m=folium_map,
style_kwds=dict(color="red", opacity=0.8, fillColor="red", fillOpacity=0.5),
marker_kwds=dict(radius=3),
)
Out[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Join regions with features using intersection joiner¶
In [6]:
Copied!
from srai.joiners import IntersectionJoiner
joiner = IntersectionJoiner()
joint = joiner.transform(regions, features, return_geom=True)
joint
from srai.joiners import IntersectionJoiner
joiner = IntersectionJoiner()
joint = joiner.transform(regions, features, return_geom=True)
joint
Out[6]:
geometry | ||
---|---|---|
region_id | feature_id | |
1 | 3 | POINT (0.00000 0.00000) |
2 | 3 | POINT (0.00000 0.00000) |
1 | 4 | POINT (-0.50000 -0.50000) |
1 | LINESTRING (-1.00000 0.00000, -0.50000 0.00000) | |
4 | 1 | POLYGON ((-1.50000 0.00000, -1.50000 0.50000, ... |
3 | 2 | POLYGON ((-1.50000 -1.50000, -1.00000 -1.50000... |
In [7]:
Copied!
folium_map = plot_regions(regions, colormap=["royalblue"])
features.explore(
m=folium_map,
style_kwds=dict(color="red", opacity=0.5, fillColor="red", fillOpacity=0.5),
marker_kwds=dict(radius=3),
)
joint.explore(
m=folium_map,
style_kwds=dict(color="yellow", opacity=1.0, fillColor="yellow", fillOpacity=1.0),
marker_kwds=dict(radius=3),
)
folium_map = plot_regions(regions, colormap=["royalblue"])
features.explore(
m=folium_map,
style_kwds=dict(color="red", opacity=0.5, fillColor="red", fillOpacity=0.5),
marker_kwds=dict(radius=3),
)
joint.explore(
m=folium_map,
style_kwds=dict(color="yellow", opacity=1.0, fillColor="yellow", fillOpacity=1.0),
marker_kwds=dict(radius=3),
)
Out[7]:
Make this Notebook Trusted to load map: File -> Trust Notebook