通过 JTS 中的 union 组合 WKT 几何

Combining WKT geometries via union in JTS

我正在尝试读取 WKT 多边形(数十万个)并将它们组合成更大的 "containing" 多边形以减少数据大小。为了简洁起见,我省略了我将使用的循环,所以这两个多边形应该作为示例。

我从来没有使用过 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();
}

存在几个问题:

  1. 我无法实例化 GeometryCollection
  2. GeometryCollection 似乎没有 accepting/adding 几何体的方法 - 我怎样才能 "populate" 带有几何体的 GeometryCollection?
  3. 无法添加 Geometries 数组,我还没有找到通过构造函数执行此操作的方法
  4. 我无法在 Geometry 上调用并集

旁白问题:如果我要合并的一些多边形是分离的,那会导致多多边形吗?没关系,只是好奇。

谢谢!

这对我有用:

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();
}