Split Polygon by Multiple LineSegments

474 Views Asked by At

I am trying to split a LinearRing into multiple LinearRings using multiple lines. The expected result is a collection of LinearRings (as in the attached image). Expected result is here attached in the image. (https://i.stack.imgur.com/Kuzr8.jpg) I am not understanding how to call the recursive function. Below is the code I tried.

If it was one single line which was cutting the polygon, I could have done that. But the problem here is that we have multiple linesstrings cutting the polygon, into multiple 'pieces'. Also, I know that each time if the current line is cutting and making two pieces of the current polygon, the current polygon should be removed from the final result. This seems a bit complicated for me.

The Main Function:

static void Main(string[] args)
{
    Coordinate c22 = new Coordinate(2889.0, 1277.0);
    Coordinate c23 = new Coordinate(2894.0, 1288.0);
    Coordinate c24 = new Coordinate(2901.0, 1284.0);
    Coordinate c25 = new Coordinate(2909.0, 1289.0);
    Coordinate c26 = new Coordinate(2916.0, 1281.0);
    Coordinate c27 = new Coordinate(2912.0, 1270.0);
    Coordinate c28 = new Coordinate(2906.0, 1275.0);
    Coordinate c29 = new Coordinate(2898.0, 1273.0);
    LinearRing outerBoundary = new LinearRing(new Coordinate[] { c22, c23, c24, c25, c26, c27, c28, c29, c22 });
    List<LineString> lines = new List<LineString>();
    Coordinate p30 = new Coordinate(2892.0, 1270.0);
    Coordinate p31 = new Coordinate(2900.0, 1294.0);
    LineString ln1 = new LineString(new Coordinate[] { p30, p31 });
    lines.Add(ln1);
    Coordinate p32 = new Coordinate(2909.0, 1268.0);
    Coordinate p33 = new Coordinate(2907.0, 1294.0);
    LineString ln2 = new LineString(new Coordinate[] {p32, p33});
    lines.Add(ln2);
    Coordinate p34 = new Coordinate(2886.0, 1286.0);
    Coordinate p35 = new Coordinate(2922.0, 1278.0);
    LineString ln3 = new LineString(new Coordinate[] {p34, p35});
    lines.Add(ln3);
    Coordinate p36 = new Coordinate(2883.0, 1281.0);
    Coordinate p37 = new Coordinate(2923.0, 1273.0);
    LineString ln4 = new LineString(new Coordinate[] {p36, p37});
    lines.Add(ln4);
    Geometry polygons = CookieCutter(outerBoundary, new List<LineString>() { ln1, ln2, ln3, ln4 });
}

The Recursive function. This is where I am stuck;

public static Geometry CookieCutter(LinearRing polygon, List<LineString> cuttingEdges, Geometry pieces = null)
{
    if (pieces == null) { pieces = Polygonize(polygon.Union(cuttingEdges[0])); }
    foreach (var cuttingEdge in cuttingEdges)
    {
        for (int i = 0; i < pieces.NumGeometries; i++)
        {
            pieces = pieces.GetGeometryN(i);
            Geometry newPieces = Polygonize(polygon.Union(cuttingEdge));
            // I know somewhere here it should call CookieCutter on new pieces
            // But how to...?
        }
    }
}

Here is the function to polygonize;

public static Geometry Polygonize(Geometry geometry)
{
    var lines = LineStringExtracter.GetLines(geometry);
    var polygonizer = new Polygonizer(false);
    polygonizer.Add(lines);
    var polys = new List<Geometry>(polygonizer.GetPolygons());
    var polyArray = GeometryFactory.ToGeometryArray(polys);
    return geometry.Factory.BuildGeometry(polyArray);
}
1

There are 1 best solutions below

0
On

Ohh... the solution was simple. I was trying to complicate it unnecessarily. There was no need of any recursion. I resolved it like this;

public static Geometry CookieCutter(
            Geometry polygon, 
            List<LineString> cuttingEdges
        )
        {

            MultiLineString mls = new MultiLineString(cuttingEdges.ToArray());
            var union = polygon.Union(mls);
            var polygons = Polygonize(union);
            return polygons;
        }