Turf: Question about additional functionality related to lineToPolygon function

Created on 5 Apr 2020  路  7Comments  路  Source: Turfjs/turf

Hi guys,
Thank you for this awesome library!

I would like to ask you one question regarding converting line to polygon. I'm using lineToPolygon function and it works like a charm. But I need more functionality.

For example, I have one complex line, something like this, which was converted to polygon via lineToPolygon function:

photo_2020-04-05_11-01-21

There is a lot of lines and shapes within polygon, which are part of this polygon. And I need to reduce it to avoid drawing unnecessary lines and make a correct searching inside it.

So the question is:
Does Turf have functionality to detect crossed lines within polygon, remove these lines and make polygon like on the next screenshot?

photo_2020-04-05_11-01-28

Thanks

new-feature

Most helpful comment

Brilliant solution @stebogit , so elegant!

All 7 comments

Hi @alexeyvax

Turf doesn't support anything like that currently and we'd probably need a new module to do so.
The algorithm would need to identify intersection points and then how to reconstruct a contour made of the outer segments.

Cheers

@alexeyvax if I understand correctly what you want to do, I think you could do something like this, where I destructured the line into points and generated a concave polygon out of them:

function toPoints (features) {
  const points = [];
  turf.coordEach(features, function (currentCoord) {
    points.push(turf.point(turf.getCoords(currentCoord)));
  }, {excludeWrapCoord: true});
  return turf.featureCollection(points);
}

const hull = turf.concave(toPoints(data));

Screen Shot 2020-04-05 at 9 49 05 AM

Screen Shot 2020-04-05 at 9 48 45 AM

Brilliant solution @stebogit , so elegant!

Many tanks guys @rowanwins and @stebogit for your replies.

Yep, that's definitely what I'm looking for!
Many thanks for you solution @stebogit, it works like a charm.

And can I ask you one more question?

Your function toPoints works great but call each time when polygon was drawn.
For example, if user drawn valid polygon(without crossed lines inside polygon), like this one:

photo_2020-04-11_08-51-57

it becomes to:

photo_2020-04-11_08-52-00

Is there some function which can check that the polygon has crossed lines inside? If yes - should call toPoints, if no - should not cal it.

I have some guesses about it:

  • First one, separate exist polygon to multiple polygons via multiPolygon or polygonize. if these functions will return few polygons, it's obvious that polygon have crossed lines inside.
  • Second one, use booleanContains, to check that polygon contain something.

But I'm not sure that all are work fine here.

Maybe you have solution for this? Please share with me.

Thanks in advance.

I can't think of anything generic enough to apply to any possible case.
However I'd suggest you to play a bit with the maxEdge option of @turf/concave to see if you can find a good value to allow the algorithm to automatically define the U shape polygon in your example.

Alternatively you can try concaveman (which I thought was what is running under the hood of the turf module, but it isn't).

You could use shamos-hoey to check for self-intersections first before applying the concave function

Thank you guys @stebogit and @rowanwins . shamos-hoey is what I'm looking for!
You are both stars! :)

Cheers!

Was this page helpful?
0 / 5 - 0 ratings