geotools,DefaultFeatureCollection添加到MapContent时出错

时间:2015-02-08 19:54:54

标签: 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();
    }
}

}

1 个答案:

答案 0 :(得分:1)

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

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

一切都会好起来的。