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

How to split polygon with a line? #139

Closed
ganisback opened this issue Feb 3, 2019 · 4 comments · Fixed by #200
Closed

How to split polygon with a line? #139

ganisback opened this issue Feb 3, 2019 · 4 comments · Fixed by #200

Comments

@ganisback
Copy link

Hi All, is there any way to split a polygon? very appreciated if have an example.

@piyushvaish
Copy link

Any updates on the same?

@macdrevx
Copy link
Member

I haven't used it personally, but a quick web search seems to suggest that the geos polygonize functionality might be able to solve this for you. Seems like the workflow for your specific case (polygon + line) might be something like:

  1. get boundary of polygon
  2. union the boundary and your line
  3. polygonize the result of the union to get split polygons

GEOSwift 5 has support for 1 (boundary) and 2 (union) but not 3 (polygonize). Let's mark this as a feature request and get it added to a future version of the library.

In the meantime, you might be able to accomplish what you want by using geos directly instead of GEOSwift. If you do, please share anything you learn to help us add support for polygonize to GEOSwift.

@macdrevx macdrevx changed the title How to dissolve polygon with a line? How to split polygon with a line? Aug 23, 2019
macdrevx added a commit that referenced this issue Mar 15, 2020
@macdrevx
Copy link
Member

@ganisback @piyushvaish if you still need this feature, I've just pushed a branch that exposes polygonize and enables this use case. Here's an example of how you can do it:

let polygon = try! GEOSwift.Polygon(exterior: GEOSwift.Polygon.LinearRing(points: [
    Point(x: 0, y: 0),
    Point(x: 1, y: 0),
    Point(x: 1, y: 1),
    Point(x: 0, y: 1),
    Point(x: 0, y: 0)]))
let line = try! LineString(points: [
    Point(x: -1, y: 2),
    Point(x: 2, y: -1)])

print(try! polygon.boundary().union(with: line).polygonize())

which results in

GeometryCollection(geometries: [
  Polygon(exterior: Polygon.LinearRing(
    points: [
      Point(x: 1.0, y: 0.0),
      Point(x: 0.0, y: 0.0),
      Point(x: 0.0, y: 1.0),
      Point(x: 1.0, y: 0.0)]),
    holes: [])),
  Polygon(exterior: Polygon.LinearRing(
    points: [
      Point(x: 0.0, y: 1.0),
      Point(x: 1.0, y: 1.0),
      Point(x: 1.0, y: 0.0),
      Point(x: 0.0, y: 1.0)]),
    holes: []))
])

(cleaned up for readability).

If you have a chance, give it a try and let me know how it works for your use case.

macdrevx added a commit that referenced this issue Mar 15, 2020
@macdrevx
Copy link
Member

This is now available in v6.2.0. Enjoy!

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

Successfully merging a pull request may close this issue.

3 participants