Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut a polygon with two lines in Shapely

Tags:

python

shapely

I am trying to cut a shapely.geometry.Polygon instance in two parts with two lines. For example, in the code below, polygon is a ring and if we cut it with line1 and line2 we should get two partial rings, one w/ 270 degrees and one with 90 degrees. Would there be a clean way to do this?

from shapely.geometry import Point, LineString, Polygon

polygon = Point(0, 0).buffer(2).difference(Point(0, 0).buffer(1))
line1 = LineString([(0, 0), (3, 3)])
line2 = LineString([(0, 0), (3, -3)])
like image 343
Yuxiang Wang Avatar asked Sep 05 '16 22:09

Yuxiang Wang


People also ask

What does buffer do in shapely?

buffer(0.2) , shapely will buffer the coordinates in polygon by 0.2 units. In your case, these are not meters, but degrees.

How do you convert Linestring to polygons?

geometry. Polygon to simply convert to line string to a polygon. It will connect the first and last coordinates. Try Polygon([(0, 0), (1, 1), (1, 2), (0, 1)]) or Polygon(s1) to produce POLYGON ((0 0, 1 1, 1 2, 0 1, 0 0)).


1 Answers

There is a function to split one geometry by another in Shapely as of version 1.6.0 (Aug 2017), so there is no need to roll your own anymore. See the docs for: shapely.ops.split(geom, splitter)

Note that the older answers on this thread were written before the splitting function was in Shapely - they are now effectively obsolete.

like image 108
Keith Ma Avatar answered Sep 20 '22 18:09

Keith Ma