Turfjs / turf-line-slice-at-intersection

Segment a LineString by Features
6 stars 3 forks source link

Problems when one segment of the LineString fully intersects the polygon #2

Open mileswwatkins opened 8 years ago

mileswwatkins commented 8 years ago

When one consecutive pair of nodes in a LineString passes all the way through a Polygon, it is still only clipped once, instead of twice.

var line = turfLinestring([[-1,-1], [5,5]]);
var poly = turfPolygon([[[0,0], [0,2], [1,2], [1,0], [0,0]]]);
var result = turfLineSliceAtIntersection(line, poly);
console.log(JSON.stringify(result));
// '{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-1,-1],[1,1]]},"properties":{}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[1,1],[5,5]]},"properties":{}}]}'

A similar error occurs in cases with concave polygons, where a single line segment passes through the polygon multiple times. Interestingly, in this case the correct number of segments are output, but many of them overlap.

var line = turfLinestring([[0,-0.5], [3.5,3]]);
var poly = turfPolygon([[[0,0], [0,2], [1,2], [2,2], [2,1], [1,1], [1,0], [0,0]]]);
var result = turfLineSliceAtIntersection(line, poly);
console.log(JSON.stringify(result));
// {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"LineString","coordinates":[[0,-0.5],[2,1.5]]},"properties":{}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[2,1.5],[1.5,1]]},"properties":{}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[1.5,1],[1,0.5]]},"properties":{}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[1,0.5],[0.5,0]]},"properties":{}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[0.5,0],[3.5,3]]},"properties":{}}]}
mezgaAladar commented 7 years ago

Hello,

I had the same error like your second problem. In my observation it is works in given cases, depends on the direction of lines and the "clockwiseness" of the polygon. I rewrote the testLineAndRing function within the turf module, and it is works for the wrong lines, but now it is not works with the lines which were good before. You can find my solution in the 3. issue thread.