I am using Python to obtain the central line of a polygon (blue line) which represents the contour of a road (red line). To do so, I have used the Centerline library as shown below:
from centerline.geometry import Centerline
import geopandas as gpd
# Load the polygon representing the road contour as a GeoDataFrame
shapefile_path = 'contorno_alpha02_suavizado025_cc.shp'
gdf = gpd.read_file(shapefile_path)
# Calculates central line of the polygon
gdf['centerline'] = gdf['geometry'].apply(
lambda x: calculate_centerline(x, {"id": 1, "name": "polygon", "valid": True}))
# New GeoDataFrame to store central lines
centerlines_gdf = gpd.GeoDataFrame(geometry=gdf['centerline'])
# Save central lines in a new shapefile.
output_shapefile_path = 'centerlines_shapefile_025_CC.shp'
centerlines_gdf.to_file(output_shapefile_path)
The code works and gives a central line, but it has several branches that are not desirable, as they are not part of the road axis. The branches appear mainly in the curved parts of the polygon (see attached images). Is there any way to removes those "branches" with Python (ideally) or in any GIS software?