How can I get center-line of a complex shaped polygon

Viewed 4940

Given coordinates of a polygon and I want to write a function that returns me a centerline coordinates of that polygon. Centerline is actually a polyline.

This image explains my question more clearly

1 Answers

You could try uisng the centerline library. It supports multipolygons and complex polygons as input (basically any input that shapely supports), and outputs Centerline objects which are MultiLineStrings.

From what you've described, you should create a Polygon or a MultiPolygon object with shapely using the given points as input, and pass the (multi)polygon object as input to the Centerline class. Once you have the Centerline objects, you can perform any operations you would perform on a MultiLineString object. An example is available in the README.md file:

>>> 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)
>>> centerline.id == 1
True
>>> centerline.name
'polygon'
>>> centerline.geoms
<shapely.geometry.base.GeometrySequence object at 0x7f7d24116210>

There is also a command line script for converting files. If the file is supported by GDAL/OGR, the script should be able to convert it.

Related