Centerline Python Library
repository·master·Indexed 43 days ago
https://github.com/fitodic/centerlineA Python library for extracting the centerlines of linear structures, such as roads or rivers, from complex polygons using Voronoi diagrams. It provides a Python API via the Centerline class and a `create_centerlines` CLI tool for batch processing vector files. The library integrates with Shapely for geometry handling and GDAL for vector file support.
What's inside Centerline
- Centerline is a library designed to extract the centerline of linear structures (such as roads, rivers, and similar polygons) using Voronoi diagrams. This is particularly useful when the length of a linear structure is a critical attribute, as calculating length from complex polygons can be difficult.
Overview of centerline functionality
masterCenterline is a library designed to extract the centerline of linear structures (like roads and rivers) that are represented as complex polygons. It achieves this by utilizing Voronoi diagrams to compute the skeleton of the input polygon, making it easier to extract length and other linear attributes.Install centerline
masterYou can install
centerlinevia PyPI usingpip.To simplify the process, you can use the
[gdal]extra dependency, which attempts to install GDAL for you.Warning: Using
pip install centerline[gdal]can be error-prone becausepipmay attempt to install a version of GDAL that conflicts with the version you have installed system-wide. It is often safer to install the system GDAL andpython-GDALmanually first, then installcenterlinenormally.# Standard installation $ pip install centerline # Installation with GDAL extra (use with caution) $ pip install centerline[gdal]Install python-GDAL in a virtual environment
masterOnce the system-wide GDAL binary and environment variables are configured, install
python-GDALinto your virtual environment.Important: The version of
python-GDALshould match your system-wide GDAL version as closely as possible. If an exact match is unavailable, use the closest minor version.Example workflow:
- Check system version:
gdalinfo --version - Install matching version:
pip install GDAL==<version>
# Check system version $ gdalinfo --version # Output: GDAL 2.1.4, released 2017/06/23 # Install matching version in virtual environment $ pip install GDAL==2.1.3- Check system version:
Configure GDAL environment variables for python-GDAL
masterBefore installing the Python bindings (
python-GDAL), you must locate the GDAL headers and set theCPLUS_INCLUDE_PATHandC_INCLUDE_PATHenvironment variables to point to the GDAL include directory. This ensures the Python installation can find the necessary headers.# Locate GDAL headers $ whereis gdal # Example output: gdal: /usr/include/gdal /usr/share/gdal # Set include paths $ export CPLUS_INCLUDE_PATH=/usr/include/gdal/ $ export C_INCLUDE_PATH=/usr/include/gdal/Install the GDAL binary on Linux
masterTo use the
create_centerlinecommand-line tool, you must first install the GDAL library system-wide. On Fedora, you can install the GDAL library and necessary dependencies usingdnf.$ sudo dnf install gdal gdal-devel gcc-c++ redhat-rpm-configSet up centerline for development
masterTo contribute to
centerline, follow these steps:- Fork and clone the repository.
- Install the library in editable (develop) mode with all necessary extras (dev, gdal, lint, test, docs).
- Run the test suite using
toxto verify the environment.
# 1. Clone the repository $ git clone git@github.com:user/centerline.git # 2. Install in develop mode with extras $ pip install -e .[dev,gdal,lint,test,docs] # 3. Run tests $ toxHandle 'TooFewRidgesError' and low ridge counts
masterIf the
create_centerlinescript produces very few ridges (e.g., a single line or a single point), it is likely due to the polygon's structure or the chosen border density. In these cases, the Voronoi algorithm may fail to produce enough vertices that lie entirely within the polygon boundaries.When this occurs, the script raises a
TooFewRidgesError. The script is designed to handle this by skipping the problematic geometry and continuing to process the remaining geometries in your input.Manage centerline complexity and detail level
masterIf the generated centerline geometry is too complex or has too much detail, adjust the border density parameter in the
create_centerlinescript.Note that because the centerline is calculated using a Voronoi diagram, certain border density settings may cause occasional small branches to stick out from the main centerline. These branches can be manually removed using GIS software like QGIS.
Fix 'QH6214 qhull input error: not enough points to construct initial simplex'
masterThis error is caused by a unit mismatch between your input data and the
create_centerlinescript's default border density. For example, if your input data uses geographic coordinates (likeEPSG:4326in degrees) but the script expects metric units (meters), the calculation will fail.To resolve this, you can either:
- Specify the border density in degrees to match your input data.
- Transform your input data into a metric coordinate reference system (e.g.,
EPSG:3857) before running the script.
# Example: Specifying border density in degrees for EPSG:4326 input $ create_centerlines input.shp output.geojson 0.00001Use the Centerline class in Python
masterYou can use the
Centerlineclass directly in your Python code. To instantiate it, provide a geometric object of typeshapely.geometry.Polygonorshapely.geometry.MultiPolygon. You can also pass optional attributes as keyword arguments which will be stored as attributes on theCenterlineinstance.from shapely.geometry import Polygon from centerline.geometry import Centerline # Define a polygon polygon = Polygon([[0, 0], [0, 4], [4, 4], [4, 0]]) # Define optional attributes attributes = {"id": 1, "name": "polygon", "valid": True} # Instantiate the Centerline object centerline = Centerline(polygon, **attributes) # Access attributes and geometry print(centerline.id) # Output: 1 print(centerline.name) # Output: 'polygon' print(centerline.geometry) # Accesses the resulting centerline geometryIntegrate the Centerline class into Python workflows
masterFor programmatic access, use the
Centerlineclass fromcenterline.geometry. You can pass ashapely.geometry.Polygonand arbitrary keyword arguments which will be stored as attributes on the resultingCenterlineinstance. The centerline geometry is accessible via the.geometryattribute.from shapely.geometry import Polygon from centerline.geometry import Centerline polygon = Polygon([[0, 0], [0, 4], [4, 4], [4, 0]]) attributes = {"id": 1, "name": "polygon", "valid": True} centerline = Centerline(polygon, **attributes) # Accessing attributes print(centerline.id) # 1 print(centerline.name) # 'polygon' # Accessing the generated geometry print(centerline.geometry)