Skip to main content
added 54 characters in body
Source Link
Bera
  • 77.8k
  • 14
  • 78
  • 187

Reproject both data frames to for example EPSG:23032, intersect and groupby region:

# -*- coding: utf-8 -*-
import geopandas as gpd

region = gpd.read_file(r"C:\GIS\data\testdata\Ny mapp\contours_circonscriptions_legislatives_03052022\circonscriptions_legislatives_030522.shp")
road = gpd.read_file(r"C:\GIS\data\testdata\Ny mapp\ROUTE500_3-0__SHP_LAMB93_FXX_2021-11-03\ROUTE500_3-0__SHP_LAMB93_FXX_2021-11-03\ROUTE500\1_DONNEES_LIVRAISON_2022-01-00175\R500_3-0_SHP_LAMB93_FXX-ED211\RESEAU_ROUTIER\TRONCON_ROUTE.shp")

region = region.to_crs("epsg:23032")
road = road.to_crs("epsg:23032")

intersected = gpd.overlay(df1=region, df2=road, how="intersection", keep_geom_type=False)
# intersected.geometry.isna().any()
# False

intersected["roadlength"] = intersected.geometry.length

#Calculate sum of road length per dep, in kilometers
result = intersected.groupby("dep")["roadlength"].sum().div(1000).round(0).astype(int).reset_index()
result = result.rename(columns={"roadlength":"roadlength_km"})

# result.head()
#   dep  roadlength_km
# 0  01           9713
# 1  02           8936
# 2  03          10317
# 3  04           3412
# 4  05           2577

Reproject both data frames to for example EPSG:23032, intersect and groupby region:

# -*- coding: utf-8 -*-
import geopandas as gpd

region = gpd.read_file(r"C:\GIS\data\testdata\Ny mapp\contours_circonscriptions_legislatives_03052022\circonscriptions_legislatives_030522.shp")
road = gpd.read_file(r"C:\GIS\data\testdata\Ny mapp\ROUTE500_3-0__SHP_LAMB93_FXX_2021-11-03\ROUTE500_3-0__SHP_LAMB93_FXX_2021-11-03\ROUTE500\1_DONNEES_LIVRAISON_2022-01-00175\R500_3-0_SHP_LAMB93_FXX-ED211\RESEAU_ROUTIER\TRONCON_ROUTE.shp")

region = region.to_crs("epsg:23032")
road = road.to_crs("epsg:23032")

intersected = gpd.overlay(df1=region, df2=road, how="intersection", keep_geom_type=False)
# intersected.geometry.isna().any()
# False

intersected["roadlength"] = intersected.geometry.length

result = intersected.groupby("dep")["roadlength"].sum().div(1000).round(0).astype(int).reset_index()
result = result.rename(columns={"roadlength":"roadlength_km"})

# result.head()
#   dep  roadlength_km
# 0  01           9713
# 1  02           8936
# 2  03          10317
# 3  04           3412
# 4  05           2577

Reproject both data frames to for example EPSG:23032, intersect and groupby region:

# -*- coding: utf-8 -*-
import geopandas as gpd

region = gpd.read_file(r"C:\GIS\data\testdata\Ny mapp\contours_circonscriptions_legislatives_03052022\circonscriptions_legislatives_030522.shp")
road = gpd.read_file(r"C:\GIS\data\testdata\Ny mapp\ROUTE500_3-0__SHP_LAMB93_FXX_2021-11-03\ROUTE500_3-0__SHP_LAMB93_FXX_2021-11-03\ROUTE500\1_DONNEES_LIVRAISON_2022-01-00175\R500_3-0_SHP_LAMB93_FXX-ED211\RESEAU_ROUTIER\TRONCON_ROUTE.shp")

region = region.to_crs("epsg:23032")
road = road.to_crs("epsg:23032")

intersected = gpd.overlay(df1=region, df2=road, how="intersection", keep_geom_type=False)
# intersected.geometry.isna().any()
# False

intersected["roadlength"] = intersected.geometry.length

#Calculate sum of road length per dep, in kilometers
result = intersected.groupby("dep")["roadlength"].sum().div(1000).round(0).astype(int).reset_index()
result = result.rename(columns={"roadlength":"roadlength_km"})

# result.head()
#   dep  roadlength_km
# 0  01           9713
# 1  02           8936
# 2  03          10317
# 3  04           3412
# 4  05           2577
Source Link
Bera
  • 77.8k
  • 14
  • 78
  • 187

Reproject both data frames to for example EPSG:23032, intersect and groupby region:

# -*- coding: utf-8 -*-
import geopandas as gpd

region = gpd.read_file(r"C:\GIS\data\testdata\Ny mapp\contours_circonscriptions_legislatives_03052022\circonscriptions_legislatives_030522.shp")
road = gpd.read_file(r"C:\GIS\data\testdata\Ny mapp\ROUTE500_3-0__SHP_LAMB93_FXX_2021-11-03\ROUTE500_3-0__SHP_LAMB93_FXX_2021-11-03\ROUTE500\1_DONNEES_LIVRAISON_2022-01-00175\R500_3-0_SHP_LAMB93_FXX-ED211\RESEAU_ROUTIER\TRONCON_ROUTE.shp")

region = region.to_crs("epsg:23032")
road = road.to_crs("epsg:23032")

intersected = gpd.overlay(df1=region, df2=road, how="intersection", keep_geom_type=False)
# intersected.geometry.isna().any()
# False

intersected["roadlength"] = intersected.geometry.length

result = intersected.groupby("dep")["roadlength"].sum().div(1000).round(0).astype(int).reset_index()
result = result.rename(columns={"roadlength":"roadlength_km"})

# result.head()
#   dep  roadlength_km
# 0  01           9713
# 1  02           8936
# 2  03          10317
# 3  04           3412
# 4  05           2577