使用GEOTools为shp文件写入数据

本文档介绍了如何利用GEOTools库将数据写入shp文件。通过调用SimpleFeatureStore.addFeatures()方法并在Transaction中设置参数,实现数据装载。在示例代码中,创建了DefaultTransaction并进行数据添加、事务提交等操作。当出现异常时,进行回滚以确保数据一致性。
在上一篇我们已经建立了一个空的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);   
   }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值