locationtech / jts

The JTS Topology Suite is a Java library for creating and manipulating vector geometry.
Other
1.9k stars 437 forks source link

Dont understand Polygonizer logic #1056

Closed xommmax closed 1 month ago

xommmax commented 1 month ago

Can someone tell me, why in case I use following multistring ( (0 0, 10 0), (10 0, 10 10), (10 10, 0 10), (0 10, 0 0), )

image

then Polygonizer.polygonize returns [POLYGON ((10 0, 0 0, 0 10, 10 10, 10 0))]

But when I use something like this: ( (0 0, 10 0), (10 0, 10 10), (10 10, 0 10), (0 10, 0 -1), )

image

it doesnt return polygon.

I use the following code from example project:

    lines.add(rdr.read("LINESTRING (0 0 , 10 0)"));
    lines.add(rdr.read("LINESTRING (10 0 , 10 10)"));
    lines.add(rdr.read("LINESTRING (10 10 , 0 10)"));
    lines.add(rdr.read("LINESTRING (0 10 , 0 0)"));

    Polygonizer polygonizer = new Polygonizer();
    polygonizer.add(lines);
    Collection polys = polygonizer.getPolygons();

    System.out.println("Polygons formed (" + polys.size() + "):");
    System.out.println(polys);

It seems like polygonizer doesnt count intersection points

xommmax commented 1 month ago

I think that is wrong lib for my purposes

FObermaier commented 1 month ago

For the record, JTS does not give you the desired result because your input set is not fully noded in your failure case. You can achieve this by unioning your input to the polygonizer:

    LineString[] lines = new LineString[4];
    lines[0] = (LineString) read("LINESTRING (0 0 , 10 0)");
    lines[1] = (LineString) read("LINESTRING (10 0 , 10 10)");
    lines[2] = (LineString) read("LINESTRING (10 10 , 0 10)");
    lines[3] = (LineString) read("LINESTRING (0 10 , 0 -1)");

    Geometry geom;
    geom = getGeometryFactory().createMultiLineString(lines);
    System.out.println(geom);
    geom = geom.union();
    System.out.println(geom);

    Polygonizer polygonizer = new Polygonizer();
    polygonizer.add(geom);
    Collection polys = polygonizer.getPolygons();

    System.out.println("Polygons formed (" + polys.size() + "):");
    System.out.println(polys);```

This gives you:

MULTILINESTRING ((0 0, 10 0), (10 0, 10 10), (10 10, 0 10), (0 10, 0 -1))
MULTILINESTRING ((0 0, 10 0), (10 0, 10 10), (10 10, 0 10), (0 10, 0 0), (0 0, 0 -1))
Polygons formed (1):
[POLYGON ((10 10, 10 0, 0 0, 0 10, 10 10))]