gpt4 book ai didi

java - geotools、DefaultFeatureCollection 添加到 MapContent 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:30 25 4
gpt4 key购买 nike

我正在使用 geotools,但我的测试类不起作用。我只想在 Mapcontent 上显示单个 Linestring,但是当我添加 DefaultFeatureCollection 时,我只是收到错误。

这是我的简单代码;

public class Test2 extends JFrame {
Test2() throws ParseException {
super();
Dimension d = new Dimension(400, 400);
setMinimumSize(d);

MapContent map = new MapContent();
JMapPane mapPane = new JMapPane(map);

add(mapPane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);

WKTReader wktReader = new WKTReader();

SimpleFeatureTypeBuilder sfTypeBuilder = new SimpleFeatureTypeBuilder();

sfTypeBuilder.setName("testType");
sfTypeBuilder.setCRS(null);
sfTypeBuilder.add("pointProperty", LineString.class);

SimpleFeatureType featureType = sfTypeBuilder.buildFeatureType();

SimpleFeatureBuilder sfBuilder = new SimpleFeatureBuilder(featureType);

sfBuilder.add(wktReader.read("LINESTRING (20 10, 20 30, 50 30)"));
SimpleFeature feature = sfBuilder.buildFeature("id1");

DefaultFeatureCollection testColletion = new DefaultFeatureCollection(
null, null);
testColletion.add(feature);
System.out.println(testColletion.isEmpty());



StyleBuilder sb = new StyleBuilder();
org.geotools.styling.Style style = sb.createStyle();
Mark mark = sb.createMark(StyleBuilder.MARK_CIRCLE, Color.RED,
Color.BLACK, 1);
PointSymbolizer pointSymb = sb.createPointSymbolizer(sb.createGraphic(
null, mark, null, 1, 15, 0));
FeatureTypeStyle fts = sb
.createFeatureTypeStyle((Symbolizer) pointSymb);
style.featureTypeStyles().add(fts);
org.geotools.styling.Font font = sb
.createFont("Arial", false, true, 12);
TextSymbolizer ts = sb.createTextSymbolizer(Color.BLACK, font, "name");
fts.rules().get(0).symbolizers().add(ts);

// FeatureLayer der Karte hinzufügen

Layer layer1 = new FeatureLayer(testColletion, style);


// Error when i add the Layer to the Map and run the Main //Method)
map.addLayer(layer1);
System.out.println(map.layers());


}

public static void main(String[] args) {
try {
Test2 t = new Test2();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

最佳答案

错误 - Could not find 'name' in the FeatureType (http://www.opengis.net/gml:testType), available attribute are: [pointProperty] 实际上是非常 self 解释的。您在样式中使用了特征中不存在的属性。因此,要么为该点添加名称,要么注释掉添加文本符号的行:

// TextSymbolizer ts = sb.createTextSymbolizer(Color.BLACK, font, "name");
// fts.rules().get(0).symbolizers().add(ts);

一切都会正常进行。

关于java - geotools、DefaultFeatureCollection 添加到 MapContent 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28398654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com