Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about additional functionality related to lineToPolygon function #1881

Closed
alexeyvax opened this issue Apr 5, 2020 · 7 comments
Closed

Comments

@alexeyvax
Copy link

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

@rowanwins
Copy link
Member

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

@stebogit
Copy link
Collaborator

stebogit commented Apr 5, 2020

@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

@rowanwins
Copy link
Member

Brilliant solution @stebogit , so elegant!

@alexeyvax
Copy link
Author

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.

@stebogit
Copy link
Collaborator

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).

@rowanwins
Copy link
Member

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

@alexeyvax
Copy link
Author

alexeyvax commented Apr 15, 2020

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

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants