在Geotools中为现有图层添加功能

时间:2014-11-12 08:24:20

标签: java geotools

我在Geotools MapPane中添加了一个图层,包含各种WKT String功能。图层显示正确。现在,只需点击UI上的按钮,我就想在此图层中添加另一个功能。

最初,我创建图层并使用以下内容显示它:

List<SimpleFeature> list  = featuresFromWkt(wktString, name); //method name explains what it does..
ListFeatureCollection col = new ListFeatureCollection(getFeatureType(), list); 
Color color = Color.BLUE;
Style style = SLD.createPolygonStyle(color, color.brighter(), 0.5f);
FeatureLayer layer = new FeatureLayer(col, style);
layer.setVisible(true);
layer.setTitle(name);
mapContent.layers().add(layer)

现在,为了向图层添加内容,我使用以下内容从内容中获取图层:

FeatureLayer layer = (FeatureLayer) mapContent.layers().get(0);

我现在可以使用layer.getSimpleFeatureSource()来检索源代码,但似乎没有办法将此源代码转换回我可以调用.addFeature(someNewFeature)来向地图添加内容。

我可以保留对ListFeatureCollection的引用并使用它来添加新功能,但我更愿意只使用mapConent.layers.get(layerNumber)方法,然后使用该层添加内容。

到目前为止,我看到的所有样本都添加了另一个具有新功能的图层。我不想添加另一个图层,而只是向现有图层添加内容,其内容位于内存中。我在这里错过了什么吗?有没有我不知道的GeoTools课程?

1 个答案:

答案 0 :(得分:2)

看看http://osgeo-org.1560.x6.nabble.com/Adding-geometry-to-layer-td4324729.html。这听起来与您的问题非常相似,不久前在GeoTools邮件列表中被问过。

基本想法,以防上面链接的邮件列表存档在某个时间点变得不可用:

  • 创建包含您的功能的功能集(最初可能为空)
  • 将此要素集添加到地图图层
  • 稍后在功能集中添加功能。

根据邮件列表上的声明:&#34;当您向集合中添加要素时,地图窗格应自动重新绘制(有一系列事件从要素集合发送到地图图层到图层列表到地图窗格)&#34;

相关问题