I am trying to read WKT polygons (hundreds of thousands of them) and combine them to larger, "containing" polygons to reduce data size. I've left out the loop I would use, for brevity, so the two polygons should serve as an example.
I have never worked with JTS, so my naive approach is this:
static Geometry combineIntoOneGeometry()
{
WKTReader wkt = new WKTReader();
Geometry[] geometries;
try
{
Geometry polygon1 = (Geometry) wkt.read("...");
Geometry polygon2 = (Geometry) wkt.read("...");
geometries = new Geometry[] { }; //add them here ?
geometries.add(polygon1, polygon2); //add doesn't exist, of course...
}
catch (ParseException e)
{
e.printStackTrace();
}
GeometryCollection gc = new GeometryFactory().createGeometryCollection(geometries); //can't instantiate GeometryFactory
return gc.union();
}
There are several problems:
Aside question: if some of the polygons I am looking to union are disjunct, would that result in a multipolygon ? That would be fine, just curious.
Thanks !
This works for me:
static Geometry combineIntoOneGeometry()
{
WKTReader wkt = new WKTReader();
GeometryFactory geoFac = new GeometryFactory();
ArrayList<Geometry> geometries = new ArrayList<>();
try
{
Geometry polygon1 = wkt.read("POLYGON ((...))");
Geometry polygon2 = wkt.read("POLYGON ((...))");
Geometry polygon3 = wkt.read("POLYGON ((...))");
Geometry polygon4 = wkt.read("POLYGON ((...))");
geometries.add(polygon1);
geometries.add(polygon2);
geometries.add(polygon3);
geometries.add(polygon4);
}
catch (ParseException e)
{
e.printStackTrace();
}
GeometryCollection geometryCollection = (GeometryCollection) geoFac.buildGeometry(geometries);
return geometryCollection.union();
}