2012-04-06 30 views
2

編輯:有關信息:使用StyleLab示例創建樣式,它將顯示您想要的樣式。使用GeoTools在JMapFrame中顯示POSTGIS數據

我試圖顯示與GeoTools POSTGIS數據,我做了例子: http://docs.geotools.org/stable/userguide/examples/: 隨着QueryLab我可以顯示POSTGIS數據的標籤 隨着快速入門我可以顯示地圖shape文件的(.SHP )

但我並沒有在混合這些源代碼,以顯示我的PostGIS DATAS

關於錯誤信息的地圖便成功,它可能來自沒有風格定義的。儘管如此,它適用於shapefile,所以我不明白。此外,我不知道如何創建適當的風格來解決這個問題。

如何將POSTGIS幾何圖形顯示到地圖中? 有誰知道如何解決這個問題或有任何想法?

這裏我的源代碼和消息錯誤:

package org.geotools.tuto; 

import java.io.IOException; 
import java.util.HashMap; 
import java.util.Map; 

import javax.swing.JFrame; 
import javax.swing.WindowConstants; 

import org.geotools.data.DataStore; 
import org.geotools.data.DataStoreFinder; 
import org.geotools.data.FeatureSource; 
import org.geotools.data.Query; 
import org.geotools.map.*; 
import org.geotools.swing.JMapPane; 

public class test { 
    public test() throws IOException{ 
     Map params = new HashMap(); 
     params.put("dbtype", "postgis"); //must be postgis 
     //the name or ip address of the machine running PostGIS 
     params.put("host", "localhost"); 
     //the port that PostGIS is running on (generally 5432) 
     params.put("port", new Integer(5432)); 
     //the name of the database to connect to. 
     params.put("database", "***"); 
     params.put("user", "***");   //the user to connect with 
     params.put("passwd", "***");    //the password of the user. 

     FeatureSource fsBC = null; 
     DataStore pgDatastore; 
     try { 
      pgDatastore = DataStoreFinder.getDataStore(params); 

      fsBC = pgDatastore.getFeatureSource("pumas_sections"); 
      System.out.println("bc count: " + fsBC.getCount(Query.ALL)); 
      } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     MapContext map = new DefaultMapContext(); 
     map.setTitle("Quickstart"); 
     map.addLayer(fsBC, null); 

     //... 
    } 

    public static void main(String[] args) throws Exception { 
      test t = new test(); 
    } 
} 

錯誤:

Exception in thread "main" java.lang.UnsupportedOperationException: No 
style method for com.vividsolutions.jts.geom.Geometry 
     at org.geotools.styling.SLD.createSimpleStyle(SLD.java:1967) 
     at org.geotools.styling.SLD.createSimpleStyle(SLD.java:1923) 
     at org.geotools.map.DefaultMapContext.checkStyle(DefaultMapContext.java:389) 
     at org.geotools.map.DefaultMapContext.addLayer(DefaultMapContext.java:222) 
     at org.geotools.tuto.test.<init>(test.java:45) 
     at org.geotools.tuto.test.main(test.java:52) 

回答

1

我曾嘗試你的代碼,它的工作。 我使用了geotools 10,得到fsBC之後,我用了MapContent。

Style style = SLD.createSimpleStyle(fsBC.getSchema()); 
Layer layer = new FeatureLayer(fsBC, style); 
MapContent map =new MapContent(); 
map.addLayer(layer); 

希望這個有用。