6

I have a linestring in shapely which I'm converting into buffer.

>>> from shapely.geometry import LineString
>>> line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)])
>>> dilated = line.buffer(0.5)

Now I'm trying to convert the dilated linestring to the original linestring. So I tried doing this:

>>> original = dilated.buffer(0.0)

But it's returning a polygon instead of a linestring. How can I get the original linestring from the polygon in shapely ?

7

I think you are misunderstanding a little what the buffer operation is actually doing. When you buffer a geometry, you create a polygon around an existing geometry with an edge at a specified distance from the original geometry. In the case of a point, this results in a circular polygon with radius equal to the distance of the buffer. For lines and polygons this creates more complicated shapes, but with the same principle.

The illustration on the ArcGIS documentation shows this quite nicely: http://desktop.arcgis.com/en/arcmap/10.3/tools/analysis-toolbox/buffer.htm

Your "dilated" geometry is a polygon with an edge that is 0.5 units away from the original line at all positions. When you buffer by 0.0 units what you are doing is saying: create a polygon with an edge exactly 0 units away from the dilated. The result is the same as the dilated polygon, NOT the original line string.

You can buffer a polygon with a negative distance, but this still always returns a polygon - at the point it would return your original line, it actually collapses to an empty polygon.

In answer to your actual question - how do you get the original line back - there isn't an easy way. Certainly Shapely provides no methods to do this.

|improve this answer|||||
  • It'd be nice to have a way to get the collapsed LineString or Point when calling .buffer() with negative distances. It would serve as central line, I think. – jjmontes Mar 19 at 2:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.