我正在尝试读取WKT多边形(数十万个)并将它们组合成更大的“包含”多边形以减少数据大小。为了简洁起见,我省略了我将使用的循环,因此这两个多边形应该作为一个例子。
我从未使用过JTS,所以我天真的做法就是:
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();
}
有几个问题:
除了问题:如果我想要联合的一些多边形是分离的,那会导致多边形吗?那很好,只是好奇。
谢谢!
答案 0 :(得分:1)
这对我有用:
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();
}