Anthony
Anthony

Reputation: 4780

Split a Polygon with a LineString in JTS

I have a polygon and a line segment which has its endpoints on two sides of the polygon. What is the easiest way to split the polygon into two polygons. (I am using the jts package).

I have tried using polygonizer but I don't think that I am using it correctly because it doesn't seem to be working.

Thanks,

Upvotes: 2

Views: 5690

Answers (2)

stryeko
stryeko

Reputation: 121

Late answer but perhaps someone wants to know this too.

Assuming you have following geometry:
GEOMETRYCOLLECTION (POLYGON ((100 150, 100 340, 350 340, 350 150, 100 150)), LINESTRING (220 340, 220 150))

You could try line merge this geometry collection, result should be:
MULTILINESTRING ((220 340, 220 150), (100 150, 100 340, 350 340, 350 150, 100 150))

From here do an unary union, result is:
MULTILINESTRING ((220 340, 220 150), (100 150, 100 340, 220 340), (220 340, 350 340, 350 150, 220 150), (220 150, 100 150))

Finally you can use the polygonizer and get the two polygons:
GEOMETRYCOLLECTION (POLYGON ((220 150, 220 340, 350 340, 350 150, 220 150)), POLYGON ((100 150, 100 340, 220 340, 220 150, 100 150)))

So to get the polygonizer working you have to give him single linestrings as input and not a whole polygon.

Tested in JTS testbuilder

Upvotes: 4

Jim
Jim

Reputation: 11

I have done similar things by making the line segement part of a very large polygon and then intersect the two polygons. You can make the large polygon by adding segements to each end of the segement until you are outside the bounds of the polygon, then add two segements at 90 degrees that go beyond the bounds of the original polygon, and then link the last two segments with a final segement to make a big polygon that splits the original poylgon along the original segement and surrounds the rest of the original polygon. It's rather a pain but works. Jim

Upvotes: 1

Related Questions