rowanwins/polygon-splitter

Bug with handling for lines with only a single segment when encountering holes

Closed this issue · 6 comments

Holes are defined in geojson as clockwise linestrings. If i create a simple square with a square hole in the centre i.e. a Polygon with 2 rings one anticlockwise boundary and one clockwise hole and split the polygon with a polyline that doesn't cross the hole but does cross the boundary, it returns an array of two polygons and the hole has disappeared.

If i then try again and split the polygon so that the polyline goes across the middle of the hole, it throws an exception. Cannot read property 'p' of undefined

I know you are using a method where you walk around the perimeter but I can't see how this will work with holes.

Can you share your geojson that you're using?

Polygon:
{ "type":"Polygon", "coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[2.5,2.5],[2.5,7.5],[7.5,7.5],[7.5,2.5],[2.5,2.5]]]}

Polyline:
{"type":"LineString","coordinates":[[5,11],[5,-1]]}

hopefully I'm doing something wrong :) those are exactly the objects I'm passing to the function

Hi @timrutter

I've managed to replicate. The issue is because the line only has two points, I added a midpoint and it works fine
debug 2021-07-07 17-31-46
I'll need to work out how to handle those lines with only a single segment.

The problem only occurs when there is a hole in the polygon

yes I can confirm putting a mid point in works. thanks for the rapid response to this by the way :)

There is the other case as well though and this isn't fixed with a mid point:

Polygon (same as above):
{ "type":"Polygon", "coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[2.5,2.5],[2.5,7.5],[7.5,7.5],[7.5,2.5],[2.5,2.5]]]}

Polyline (slighty to the right so it doesn't cut the hole:
{"type":"LineString","coordinates":[[8.5,11],[8.5,-1]]}

in this case the polygon is cut correctly but the hole disappears, I'll check I'm interpreting the result correctly in my code

Fixed in v0.3