Split a polygon by a line

3,899 views
Skip to first unread message

ivy

unread,
Jan 13, 2012, 9:20:17 AM1/13/12
to JSTS devs
Hi,

I've been trying to split a polygon with a line (or linestring).
Converting the linestring to a polygon and doing a difference
operation was not successful (no area). I've been looking at the Noder
and SegmentIntersector classes, but I hope someone has some example
code.

Could anyone point me in the right direction?

With kind regards,
Ivor

Björn

unread,
Jan 13, 2012, 9:34:49 AM1/13/12
to JSTS devs
You should be able to node the input linework and use that with
jsts.operation.overlay.PolygonBuilder

Check out JTS maillist archives for more hints or get back here if you
get stuck.

PS. Haven't tested this part of JSTS even if it is ported, so I'm not
sure if it works.

/Björn

Björn

unread,
Jan 13, 2012, 9:44:35 AM1/13/12
to JSTS devs
Ah! I was wrong there. PolygonBuilder is used internally when
calculating overlays.

What you need is the jsts.operation.polygonize.Polygonizer but I'm
afraid that it hasn't been ported yet with no ETA unless the work can
be funded.

/Björn

ivy

unread,
Jan 17, 2012, 7:36:29 AM1/17/12
to JSTS devs
Thanks for the response. For now, I buffer the splitline (one segment)
for a bit and do

[p1, p2] = polygon.difference(buffered_splitline_polygon)

For now, my problem is that the 'gap' introduced by the buffered
splitline should be attached to one of the resulting polygons p1 or
p2, and I plan to do that by either doing a p1 = p1.union( or
polygon.difference(p1)...

It might nog be efficient, but I hope it works for my use case.

Regards,
Ivor Bosloper

Carsten Eider

unread,
Mar 3, 2012, 3:41:55 PM3/3/12
to JSTS devs
I just tried to port my good plain java-code to javascript and tumled
into the same trap.

Any ideas when jsts.operation.polygonize.Polygonizer might be
available?
It would be a great feature!!!!

On 17 Jan., 13:36, ivy <ivorboslo...@gmail.com> wrote:

Carsten Eider

unread,
Mar 5, 2012, 1:58:13 AM3/5/12
to JSTS devs
Okay Björn,

I can understand what you mean. I looked into the java source and saw
what which parts are missing within your js-implementation.
How many hours do you think had to be funded? Whar does it mean in €?

Cheers Carsten

Björn

unread,
Apr 13, 2012, 9:31:59 AM4/13/12
to jsts...@googlegroups.com
FYI, an initial implementation of Polygonizer has landed in trunk at https://github.com/bjornharrtell/jsts.

Carsten Eider

unread,
Apr 16, 2012, 8:22:31 AM4/16/12
to jsts...@googlegroups.com
Hi Björn,

thank you for your work in improving jsts, but i found some errors in

jsts.geom.Geometry espacially in getInteriorPoint();

line 421 in
https://github.com/bjornharrtell/jsts/blob/master/src/jsts/geom/Geometry.js
is missing a "this"

var dim = getDimension();==> var dim = this.getDimension();

In the same method you refer to

intPt = new InteriorPointPoint(this);
intPt = new InteriorPointLine(this);
intPt = new InteriorPointArea(this);

but i cannot find any implmentation of jsts.algorithm.InteriorPoint*

Any time to fix this?


TIA Carsten

Björn

unread,
Apr 16, 2012, 2:00:24 PM4/16/12
to jsts...@googlegroups.com
Thanks for reporting the bug in Geometry.js, I've fixed that already. If possible, please use GitHub to report issues and code modifications in the future.

Currently I don't have the free time to port the interior point algorithms so that too would require funding. (note that the polygonizer work was funded)

However, you can increase the chance of the interior point algorithms getting ported with or without funding by:

1. Submitting an issue about it at GitHub
2. Supplying test cases

And of course there is nothing stopping you from porting the code yourself :)

/Björn

Felipe Cardozo

unread,
Jan 26, 2013, 7:01:14 PM1/26/13
to jsts...@googlegroups.com
Hi everyone,

I'm working on the exact same problem as Carsten (split a polygon by a line), and I still find the polygonizer a little hard for me. Could you provide a sample of code ?

Thanks in advance.

Björn

unread,
Jan 28, 2013, 10:39:42 AM1/28/13
to jsts...@googlegroups.com
Here is a simple example:

        var reader = new jsts.io.WKTReader();

    var a = reader.read('POLYGON((10 10, 100 10, 100 100, 10 100, 10 10))');
    var b = reader.read('LINESTRING(-5 15, 50 50, 55 60, 50 40, 130 50)');
    var union = a.getExteriorRing().union(b);

    var polygonizer = new jsts.operation.polygonize.Polygonizer();

    polygonizer.add(union);
    
    var map = new OpenLayers.Map('map', {
      maxExtent: new OpenLayers.Bounds(0, 0, 200, 200),
      maxResolution: 100,
      units: 'm',
      controls: [new OpenLayers.Control.MousePosition(), new OpenLayers.Control.Navigation()]
    });
    
    var layer = new OpenLayers.Layer.Vector('test', {
      isBaseLayer: true
    });
    map.addLayer(layer);
    
    var parser = new jsts.io.OpenLayersParser();
    
    var polygons = polygonizer.getPolygons();
    for (var i = polygons.iterator(); i.hasNext();) {
        var polygon = i.next();
        var feature = new OpenLayers.Feature.Vector(parser.write(polygon), null, { strokeColor: 'gray', strokeWidth: 5, fillColor: 'white'});
        layer.addFeatures([feature]);
    }
    
    var feature = new OpenLayers.Feature.Vector(parser.write(b), null, { strokeColor: 'black', strokeWidth: 1, fillColor: 'red'});
    layer.addFeatures([feature]);
    
    map.zoomToMaxExtent();
    
    var control = new OpenLayers.Control.ModifyFeature(layer);
    map.addControl(control);
    control.activate();

Note that I'm creating a union of the linework. This is because Polygonizer needs "noded" input to create expected output. For more complex cases you might need to use a noder implementation instead of union.

/Björn

Felipe Cardozo

unread,
Jan 28, 2013, 1:47:16 PM1/28/13
to jsts...@googlegroups.com
Hi Björn,

thanks a lot for your response ! So far, it's exactly what I needed.

gpetricek .

unread,
Aug 28, 2013, 2:55:06 AM8/28/13
to jsts...@googlegroups.com
Thank you Bjorn. I was looking for that solution couple of days. 

Great job. 

turc...@gmail.com

unread,
Aug 11, 2014, 6:59:23 AM8/11/14
to jsts...@googlegroups.com
Thank you, your code works very well but not for features with holes inside because it gets the exterior ring and holes are ignored.

Ossi Rönnberg

unread,
Apr 11, 2016, 6:43:08 AM4/11/16
to JSTS devs
Any ideas how to get this work with interior rings as well?

Ziliang Zhao

unread,
May 3, 2016, 2:16:13 PM5/3/16
to JSTS devs
Thanks Björn for the solution.  Currently, the first and last points have to be outside the polygon.  If they are on polygon segments it won't work.  Any simple workaround?  Thanks.

Ignacio de la Torre

unread,
Sep 6, 2017, 9:05:48 AM9/6/17
to JSTS devs, ossi.r...@simosol.fi
Hey! Did you manage to make it work with interior rings as well?

Thanks!
Reply all
Reply to author
Forward
0 new messages