Calculate the centerline of a polygon
Calculate the centerline of a polygon.
Roads, rivers and similar linear structures are often represented by long and complex polygons. Since one of the most important attributes of a linear structure is its length, extracting that attribute from a polygon can prove to be more or less difficult.
You can download the package from PyPI:
$ pip install centerline
If you are installing these packages in the virtual environement, make sure you have all the necessary dependencies on your system. Furthermore, after installing GDAL locate the GDAL headers:
$ whereis gdal gdal: /usr/include/gdal /usr/share/gdal
and set the include path using the following environment variables:
$ export CPLUS_INCLUDE_PATH=/usr/include/gdal/ $ export C_INCLUDE_PATH=/usr/include/gdal/
After that, you can proceed to installing GDAL in the virtual environment.
It is important to note that the versions of GDAL installed globally and in the virtual environment should match!
For more info, visit Stack Exchange.
If you plan on using this package inside of your own code, just type:
>>> from centerline import Centerline
However, if you just want to convert a Shapefile full of polygons into a Shapefile full of centerlines, use the command line tool:
$ create_centerlines INPUT_PATH.shp OUTPUT_PATH.shp [BORDER_DENSITY]
The BORDER_DENSITY parameter is optional. If not specified, the default value is 0.5.
create_centerlines script is file format agnostic, meaning you should be able to work with all OGR’s vector file formats. Example:
$ create_centerlines input.shp output.geojson
When defining the density factor, one has to take into account the coordinate system defined in the Shapefile. The script was designed to handle metric coordinate systems, so the density factor is by default 0.5 (meters). For instance, if the value is set to 0.5 m, it basically places additional points on the border at the distance of 0.5 m from each other. If the user doesn’t define the value (see Usage), the script uses the default value. If the value is a negative number, it will be converted into a positive number.
It appears that the Voronoi function available in the SciPy module does not handle large coordinates very well. Since most of the coordinates are large numbers, a bounding box is needed to determine the minimal X and Y coordinates, i.e. the bottom left corner of the bounding box. These values are then used for coordinate reduction. Once the Voronoi diagram is created the coordinates are returned to their non-reduced form before creating LineStrings.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|centerline-0.3-py2.py3-none-any.whl (12.9 kB) Copy SHA256 Checksum SHA256||py2.py3||Wheel||Nov 26, 2017|
|centerline-0.3.tar.gz (129.2 kB) Copy SHA256 Checksum SHA256||–||Source||Nov 26, 2017|