我如何在 GeoTools 中绘制一条围绕单层中所有多边形的线?

How can i draw a line surrounding all the polygons in a single layer in GeoTools?

我在 Java 中使用打开 shapefile 的 GeoTools (v.22) 库编写了一些代码,获取其中的所有多边形并根据与每个多边形关联的值将它们划分到不同的 FeatureCollections 中; 然后它为每个 FeatureCollection 创建一个图层并将其添加到地图中,从而导致所有多边形都被黑色笔划与其他多边形分开。

如何在同一层中围绕所有多边形绘制另一条彩色线?

您正在寻找 Concave Hull of your features. So first you need to get a GeometryCollection of your features. You can then call Eric Grosso's Concave Hull implementation

所以像这样:

    File f = new File("/home/ian/Data/states/states.shp");
    FileDataStore ds = FileDataStoreFinder.getDataStore(f);

    Filter filter = ECQL.toFilter("strEndsWith(\"STATE_NAME\",'a')=true");
    SimpleFeatureCollection collection = ds.getFeatureSource().getFeatures(filter);
    ArrayList<Geometry> geoms = new ArrayList<>();
    try (SimpleFeatureIterator it = collection.features()) {
      while (it.hasNext()) {
        SimpleFeature feature = it.next();
        Geometry geom = (Geometry) feature.getDefaultGeometry();

        geoms.add(geom);
      }
    }
    GeometryFactory gf = new GeometryFactory();
    GeometryCollection gc = gf.createGeometryCollection(geoms.toArray(new Geometry[] {}));
    gc = (GeometryCollection) Densifier.densify(gc, 5);
    double threshold = 10;
    ConcaveHull ch = new ConcaveHull(gc, threshold);
    Geometry concaveHull = ch.getConcaveHull();
    System.out.println(gc);
    System.out.println(concaveHull);

在这种情况下生成以下地图:

而阈值 1 给出: