2016-05-23 8 views
2

私は与えられたシェイプファイルにロードしたいプロジェクトを持っていて、新しいシェイプファイルに結果を書き込む前にあるサイズ以上のポリゴンを選びます。おそらく最も効率的ではないかもしれませんが、シェイプファイルを書くはずのポイントまで、すべてのコードを正常に実行できるコードがあります。エラーは発生しませんが、結果として得られるシェイプファイルには使用可能なデータがありません。私はできるだけ多くのチュートリアルに従ってきましたが、まだ空白になっています。Geotoolsのシェイプファイルを作成する

コードの最初のビットは、シェイプファイルで読み込んで、必要なポリゴンを取り出し、フィーチャコレクションに入れます。この部分は私が理解できる限りうまくいくようです。

public class ShapefileTest { 

    public static void main(String[] args) throws MalformedURLException, IOException, FactoryException, MismatchedDimensionException, TransformException, SchemaException { 

     File oldShp = new File("Old.shp"); 
     File newShp = new File("New.shp"); 

     //Get data from the original ShapeFile 
     Map<String, Object> map = new HashMap<String, Object>(); 
     map.put("url", oldShp.toURI().toURL()); 
     //Connect to the dataStore 
     DataStore dataStore = DataStoreFinder.getDataStore(map); 
     //Get the typeName from the dataStore 
     String typeName = dataStore.getTypeNames()[0]; 

     //Get the FeatureSource from the dataStore 
     FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName); 

     SimpleFeatureCollection collection = (SimpleFeatureCollection) source.getFeatures(); //Get all of the features - no filter   

     //Start creating the new Shapefile 
     final SimpleFeatureType TYPE = createFeatureType(); //Calls a method that builds the feature type - tested and works. 
     DefaultFeatureCollection newCollection = new DefaultFeatureCollection(); //To hold my new collection 

     try (FeatureIterator<SimpleFeature> features = collection.features()) { 
      while (features.hasNext()) { 
       SimpleFeature feature = features.next(); //Get next feature 
       SimpleFeatureBuilder fb = new SimpleFeatureBuilder(TYPE); //Create a new SimpleFeature based on the original 
       Integer level = (Integer) feature.getAttribute(1); //Get the level for this feature 
       MultiPolygon multiPoly = (MultiPolygon) feature.getDefaultGeometry(); //Get the geometry collection 

       //First count how many new polygons we will have 
       int numNewPoly = 0; 
       for (int i = 0; i < multiPoly.getNumGeometries(); i++) { 
        double area = getArea(multiPoly.getGeometryN(i)); 
        if (area > 20200) { 
         numNewPoly++; 
        } 
       } 

       //Now build an array of the larger polygons 
       Polygon[] polys = new Polygon[numNewPoly]; //Array of new geometies 

       int iPoly = 0; 
       for (int i = 0; i < multiPoly.getNumGeometries(); i++) { 
        double area = getArea(multiPoly.getGeometryN(i)); 
        if (area > 20200) { //Write the new data 
         polys[iPoly] = (Polygon) multiPoly.getGeometryN(i); 
         iPoly++; 
        } 
       } 

       GeometryFactory gf = new GeometryFactory();  //Create a geometry factory   
       MultiPolygon mp = new MultiPolygon(polys, gf); //Create the MultiPolygonyy 
       fb.add(mp); //Add the geometry collection to the feature builder 
       fb.add(level); 
       fb.add("dBA"); 

       SimpleFeature newFeature = SimpleFeatureBuilder.build(TYPE, new Object[]{mp, level,"dBA"}, null); 
       newCollection.add(newFeature); //Add it to the collection 
      } 

この時点では、正しく見えるコレクションがあります。正しい境界とすべてがあります。次のビットであれば、新しいシェイプファイルにコードを配置します。

  //Time to put together the new Shapefile     
      Map<String, Serializable> newMap = new HashMap<String, Serializable>(); 
      newMap.put("url", newShp.toURI().toURL()); 
      newMap.put("create spatial index", Boolean.TRUE); 

      DataStore newDataStore = DataStoreFinder.getDataStore(newMap); 
      newDataStore.createSchema(TYPE); 
      String newTypeName = newDataStore.getTypeNames()[0]; 

      SimpleFeatureStore fs = (SimpleFeatureStore) newDataStore.getFeatureSource(newTypeName);    

      Transaction t = new DefaultTransaction("add");  

      fs.setTransaction(t); 
      fs.addFeatures(newCollection); 

      t.commit(); 
      ReferencedEnvelope env = fs.getBounds(); 

     } 

    } 

FeatureStore fsの境界をチェックする最後のコードを入れて、nullに戻します。明らかに、新たに作成されたシェイプファイル(これは作成され、適切なサイズのもの)をロードすると、何も表示されません。

答えて

2

私はあなたがファイナライズ/ファイルを閉じる手順が不足していると思います。 t.commit行の後にこれを追加してください。

fs.close(); 

好都合別の方法として、あなたはシェープファイルデータストアdocsで述べたシェープファイルダンパーユーティリティを試してみるかもしれません。これを使うと、2行目または2行目のコードブロックを簡略化できます。

+0

フィーチャの境界がnullであるという事実は修正されません。機能自体は正しく作成されていません。トランザクションを終了しても、その問題は解決されませんでした。そして、Shapefileのデータストアをもっと詳しく調べていきます。ありがとう! –

+0

私は穴を持つMultiPolygonを作る単純なコードを試してみましたが、同じ問題を抱えています...私は明らかにその機能を作成する方法で何かが欠けています... –

2

このソリューションは実際に私が投稿したコードとは何の関係もなく、FeatureTypeの定義とは関係がありました。ポリゴンフィーチャタイプに "the_geom"を含めなかったので、ファイルに何も書き込まれませんでした。

関連する問題