2017-03-27 3 views
0

geotoolsを使用してhttp://www.naturalearthdata.com/downloads/10m-cultural-vectors/10m-admin-0-countries/からダウンロードした.shpファイルを開こうとしています。私のコードは次のようになります。Javaで.shpファイルを開く

File file = new File("ne_10m_admin_0_countries.shp"); 
try{ 

     Map<String, URL> map = new HashMap<String, URL>(); 
     map.put("url", file.toURI().toURL()); 

     DataStore dataStore = DataStoreFinder.getDataStore(map); 

     System.out.println(DataStoreFinder.getDataStore(map)); 
     System.out.println(map); 

     SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]);   
     SimpleFeatureCollection collection = featureSource.getFeatures(); 

     ReferencedEnvelope env = collection.getBounds(); 
     double left = env.getMinX(); 
     double right = env.getMaxX(); 
     double top = env.getMaxY(); 
     double bottom = env.getMinY(); 
     System.out.println(left+" "+right); 
    }catch(Exception e){ 
     System.out.println("ahoj" +e.getMessage()); 
    } 

DataStoreFinder.getDataStore()メソッドはnullを返します。これは問題です。私は間違って何をしているのですか?

国境の座標を取得するために、このシェイプファイルを開いて使用します。

問題は、他に必要なjarファイルが含まれていないことでした。それは今働いている。

答えて

0

クラスパスにgt-shapefile.jarがありますか?

GeoToolsは、SPIルックアップを使用して、指定したパラメータマップに基づいてデータストアを検索します。

Iterator<DataStoreFactorySpi> it = DataStoreFinder.getAllDataStores(); 
    while (it.hasNext()) { 
    DataStoreFactorySpi fac = it.next(); 
    System.out.println(fac.getDisplayName()); 
    } 

それともしか、直接ShapefileDataStoreを作成するシェープファイルを読みたい場合:別の可能なチェックは、状況を確認するにはDataStoreFinder.getAllDatastores()を呼び出すことです。

また、ウィンドウとパスをスペースで処理するため、file.toUri().toUrl()よりDataUtilities.fileToURL(file)を使用することをお勧めします。

関連する問題