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

标签 java geotools

我正在使用 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/

相关文章:

java - 交叉点类型的成员

eclipse - 如何在 Maven 中解析 'Offline/Missing artifact org.geotools:gt-shapefile:jar:17-SNAPSHOT'?

java - 从 SpatialIndexFeatureCollection 渲染要素时抛出 UnsupportedOperationException

xcode - 如何配置 Xcode 以使用 Maven

java - 我需要Docker容器中的任何服务器来运行Java应用程序吗?

java - 在创建应用程序 bean 之前初始化 Spring Batch DB

java - 从另一个 Android 应用程序下载并安装 Android 应用程序

java - 使用 Geotools 来自数据库的多点

Java 地理工具 : how to find distance from a point to closest polygon in shape file

java - 计算 jar 文件的代码指标