
Visualizing the result should look something like the following image:īringing this functionality over to TurfJS Keep_polys = # remaining polygons are the split polys of original shape union ( input_l ) # use polygonize geos operator and filter out poygons ouside of origina input polygon ]) # union the exterior lines of the polygon with the dividing linestring Or, we can re-write it ourselves using the underlying polygonize method like so: from shapely.geometry import LineString, MultiPolygon, Polygon, Point from shapely.ops import polygonize # make a complex geometry We can see those steps employed in the _split_polygon_with_line method here. Iterate through all the resulting polygons and keep only the ones that have a point inside the original polygon.Use the polgyonize method to create polygons from the negative space in the union-ed lines.Take the slice line and the polygon exterior and union the two together (this will catch all those intersecting points).Take the polygon and get the exterior boundary of it as a line.This is similar to my triangle method, but less prone to error since the triangles I was generating needed to be trimmed and fit back against the original polygon.įor shapely, the steps for split in this case were as follows: This method takes a multiline string and creates polygons from each of the shapes. Shapely achieves the slicing by employing a method called polygonize. I ended up making a crude site to demonstrate this functionality (as is shown above). Having it run in TurfJS would be advantageous because I could leverage interactive drawing tools in MapboxGL and allow a user to draw the line string that they wanted to use to cut the polygon. There did not appear to be a method that was the same in TurfJS - so could I learn from the algorithm in Shapely and re-implement the slice method in TurfJS?.Learn how the method worked in Shapely (what was the underlying algorithm).Learning about this method lead me to want to do two things: 😐 /nUTIiCkNpi- Kuan Butts May 31, 2020Ībove: A Twitter user pointing out the split() functionality that is available in Shapely now. For some reason, I'd always been under the impression it did (multi)linestrings split by points, but not polygons. Oh wow that's embarrassing! Thanks for pointing that out. In response to this proposal, a Twitter user pointed out that Shapely in fact already has a method called split() and that it supports splitting a polygon with a line string. Ended up going down a rabbit hole but got it to work w/ triangulation of the polygon - surprisingly tricky! /xGTVcSYWNu- Kuan Butts May 30, 2020Ībove: My tweet in which I first put out the concept. Wanted to split a polygon by a linestring and noticed that there's no "out of the box" method w/ shapely/turf/other popular libs. My initial exploration involved triangulating the polygon based on all intersecting points on the polygon exterior with the line string and then re-assembling from there. I recently wanted to explore if it was possible to divide a polygon into two parts by slicing it with a line.
Gpsmapedit split polygon update#
I will update later if I am able to determine a work-around. Turf v6 has introduced breaking changes that do not allow the below solution to work. Note that the method/solution shown below works with Turf version 5.1.6 and prior. Slicing polygons with linestrings Jul 7, 2020
