mapbox / lineclip

A very fast JavaScript polyline and polygon clipping library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

polygons that cross the bbox multiple times return joined results

tcql opened this issue · comments

screen shot 2015-09-17 at 6 05 36 pm

I would expect 2 triangles, but I get back a single polygon. Is this expected behavior with Sutherland-Hodgman?

https://gist.github.com/tcql/ab60a9fecd8e9691d0a7

Yes, this is expected behavior with Sutherland-Hodgeman. It produces degenerate edges. This is a price to pay for exceptional performance of the algorithm.

👍 cool. Just making sure. I'll move ahead on pulling together turf-clip, then ;)

@tcql awesome, looking forward to it!

Hey there, I got the very same issue but actually can not find a library or another algorithm which does solve this issues problem.

Is there any solution for this problem available or do you @mourner or @tcql know of any?
I am currently on a project where we want to visualize ship routes. For doing that we want to clip a world map containing all coastlines by a bounding box (depending on the envelope of the route). When lineclip produces degenerate edges we really run in to trouble when we want to draw the clipped polygon to the html canvas. E.g. like this is the clipped output when it cuts the same polygon multiple times:

image

And when we want to draw it (iterating over all features of the featureCollection)

image

Do you know any solution to this problem? We basically would need for each clipping a new feature should be created. Then we would know when we would need to "lift the pen and go to the new feature location and then start drawing again".

For any sort of help you would help our routing project big times!

@Andi-Lo the picture look more like you're trying to clip a polygon with a polyline clipping algorithm. Are you using lineclip.polygon?

@mourner yes I'm using lineclip.polygon

This is my clipping function:

let clipPolygon = function(fc, bbox) {
  let points;
  let polygon;
  bbox = turf.size(turf.square(bbox), 2);
  let clipped = turf.featureCollection([]);
  turf.meta.featureEach(fc, function(feature) {
    points = turf.meta.coordAll(feature);
    if(points.length > 0)
      polygon = lineclip.polygon(points, bbox);
    if(polygon.length > 0)
      clipped.features.push(turf.lineString(polygon));
  });
  return clipped;
};

Would be happy to provide more detailed information if needed!