2015-02-08 86 views
1

我正在使用geotools,但我的測試類不起作用。我只想在Mapcontent上顯示一個單行字符串,但是當我添加我的DefaultFeatureCollection時,我只是得到錯誤。geotools,DefaultFeatureCollection添加到MapContent時出錯

這是我簡單的代碼;

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

} 
+0

我無法顯示任何內容到地圖....我不知道如何處理像SimpleFeatureCollection另一個集合,因爲它的接口...也許我需要一個SimpleFeatureCollection來添加我的Linestring到地圖... – 2015-02-08 19:55:59

+1

你會得到什麼樣的錯誤?當您顯示錯誤時,您可能會得到更多/更好的答案,而不是期望每個人都能夠首先運行您的代碼。 – cello 2015-02-08 20:16:04

回答

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); 

而且一切都會正常工作。