我使用 GeoTools (v.22) 库在 Java 中编写了一些代码,该库打开一个 shapefile,获取其中的所有多边形,并根据与每个要素关联的值将它们划分为不同的要素集合; 然后,它为每个FeatureCollection创建一个图层并将其添加到 map 中,从而产生所有多边形,每个多边形都被黑色笔划与其他多边形分开。
如何绘制另一条围绕同一层中所有多边形的彩色线?
最佳答案
您正在寻找Concave Hull你的特点。因此,首先您需要获取特征的GeometryCollection
。然后您可以调用 Eric Grosso 的 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);
在本例中生成以下 map :
阈值 1 给出:
关于java - 如何在 GeoTools 中画一条围绕单个图层中所有多边形的线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55491034/