使用GEOTools为shp文件写入数据

在上一篇我们已经建立了一个空的shp文件,接着我们为这个空文件装入数据,这个功能主要用到SimplefeatureStore.addFeatures()这个方法,传入的属性是Transaction transaction = new DefaultTransaction(null),这里的null是可以设置string类型的值的,这个值有什么影响还不清楚,求助大神!

以下是装载数据的代码:
      File SHPfilepath=new File("E:\\workspace");
      File SHPfile = JFileDataStoreChooser.showOpenFile("shp", SHPfilepath, null);
          if (SHPfile == null) 
          {
              return;
          }
         FileDataStore store = FileDataStoreFinder.getDataStore(SHPfile);
         String typeName = store.getTypeNames()[0];
         JOptionPane.showMessageDialog(null, "This is the typeName="+typeName);
        SimpleFeatureSource featureSource = store.getFeatureSource(typeName);
      SimpleFeatureStore SimplefeatureStore = (SimpleFeatureStore) featureSource;
      SimpleFeatureCollection SimpleFeaturecollection = new ListFeatureCollection(TYPE, features);
       
      Transaction transaction = new DefaultTransaction(null);  
      SimplefeatureStore.setTransaction(transaction);
            try 
            {
            SimplefeatureStore.addFeatures(SimpleFeaturecollection);
                transaction.commit();
                JOptionPane.showMessageDialog(null, "The points have been adden into shipfile!");
            } 
            catch (Exception problem)
            {
                problem.printStackTrace();
                transaction.rollback();
            } 
            finally 
            {
                transaction.close();
            }
            System.exit(0);   
   }

你可能感兴趣的:(使用GEOTools为shp文件写入数据)