• Geotools实现shape文件的写入


            众所周知Geotools作为开源的Java GIS三方库,已经成为GIS服务器端的主流开源库,其功能非常强大,涉及到GIS业务的方方面面,其中就包括GIS数据的读写,今天小编就借助Geotools来实现shape数据的写入。

          Geotools对于shape数据写入,主要提供了SimpleFeatureStore和FeatureWriter两个主要操作类,下面小编就根据这两个类实现shape数据的写入,废话不多说,直接上代码:

    复制代码
    import org.geotools.data.*;
    import org.geotools.data.shapefile.ShapefileDataStore;
    import org.geotools.data.shapefile.ShapefileDataStoreFactory;
    import org.geotools.data.simple.SimpleFeatureCollection;
    import org.geotools.data.simple.SimpleFeatureIterator;
    import org.geotools.data.simple.SimpleFeatureSource;
    import org.geotools.data.simple.SimpleFeatureStore;
    import org.opengis.feature.simple.SimpleFeature;
    
    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.nio.charset.Charset;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    
    public class ShapwWriterTest {
    
        public static void main(String[] args) throws IOException {
            File file = new File("D:\\data\\line_sheng.shp");
            ShapefileDataStore shapefileDataStore = new ShapefileDataStore(file.toURI().toURL());
            SimpleFeatureSource simpleFeatureSource = shapefileDataStore.getFeatureSource();
            int count = simpleFeatureSource.getFeatures().size();
            for(int i = 0;i<2; i++){
                //分批插入(没啥逻辑,主要是验证多次写入同一个shp)
                Query query = createQuery(i*(count / 2),count / 2);
                SimpleFeatureCollection simpleFeatureCollection = simpleFeatureSource.getFeatures(query);
                addFeature2Shp(simpleFeatureCollection,"D:\\data\\line_sheng_1.shp");
            }
        }
    
    
        /**
         * 将simplefearurecollection写入目标shape
         * @param simpleFeatureCollection
         * @param filePath
         * @throws IOException
         */
        public static void addFeature2Shp(SimpleFeatureCollection simpleFeatureCollection, String filePath) throws IOException {
            File file = new File(filePath);
            ShapefileDataStore shapefileDataStore = null;
            if (file.exists()){
                shapefileDataStore = (ShapefileDataStore) DataStoreFinder.getDataStore(Collections.singletonMap("url",file.toURI().toURL()));
            }else{
                ShapefileDataStoreFactory shapefileDataStoreFactory = new ShapefileDataStoreFactory();
                shapefileDataStore = (ShapefileDataStore) shapefileDataStoreFactory.createNewDataStore(Collections.singletonMap("url",file.toURI().toURL()));
                shapefileDataStore.setCharset(Charset.defaultCharset());
                shapefileDataStore.createSchema(simpleFeatureCollection.getSchema());
            }
            //获取simplefeaturestore
            writerFeature(simpleFeatureCollection, shapefileDataStore);
            //writerFeature1(simpleFeatureCollection,shapefileDataStore);
        }
    
        /**
         * 使用SimpleFeatureStore写入shape文件
         * @param simpleFeatureCollection
         * @param shapefileDataStore
         * @throws IOException
         */
        private static void writerFeature(SimpleFeatureCollection simpleFeatureCollection, ShapefileDataStore shapefileDataStore) throws IOException {
            SimpleFeatureStore simpleFeatureStore = (SimpleFeatureStore) shapefileDataStore.getFeatureSource(shapefileDataStore.getTypeNames()[0]);
            Transaction transaction = new DefaultTransaction("create");
            simpleFeatureStore.setTransaction(transaction);
            try {
                simpleFeatureStore.addFeatures(simpleFeatureCollection);
                transaction.commit();
            } catch (Exception e) {
                transaction.rollback();
            } finally {
                transaction.close();
            }
        }
    
        /**
         * 使用FeatureWriter来写feature
         * @param simpleFeatureCollection
         * @param shapefileDataStore
         * @throws IOException
         */
        private static void writerFeature1(SimpleFeatureCollection simpleFeatureCollection, ShapefileDataStore shapefileDataStore) throws IOException {
            FeatureWriter featureWriter = shapefileDataStore.getFeatureWriterAppend(Transaction.AUTO_COMMIT);
            SimpleFeatureIterator simpleFeatureIterator = simpleFeatureCollection.features();
            while(simpleFeatureIterator.hasNext()){
                SimpleFeature simpleFeature = simpleFeatureIterator.next();
                SimpleFeature simpleFeature1 = (SimpleFeature) featureWriter.next();
                simpleFeature1.setAttributes(simpleFeature.getAttributes());
            }
            featureWriter.write();
            featureWriter.close();
            simpleFeatureIterator.close();
        }
    
    
        private static Query createQuery(int startIndex,int queryCount){
            Query query = new Query();
            query.setStartIndex(startIndex);
            query.setMaxFeatures(queryCount);
            return query;
        }
    
        /**
         * 总结geotools 读取shape的几种方式
         */
        private static void testReaderShape(String filePath) throws IOException {
            //第一种方式
            ShapefileDataStore shapefileDataStore = new ShapefileDataStore(new File(filePath).toURI().toURL());
            /**
             * 使用上述这种方式读shape的话,其中的很多参数都是默认的,最主要的是它的编码是StandardCharsets.ISO_8859_1
             * 因此我们需要单独设置下
             */
            shapefileDataStore.setCharset(Charset.forName("UTF-8"));
    
            //第二种ShapefileDataStoreFactory
            ShapefileDataStoreFactory shapefileDataStoreFactory = new ShapefileDataStoreFactory();
            Map paramMap = new HashMap<>();
            /**
             * 通常有那些参数,我们可以通过下面的这个函数去查看,这里面
             */
            shapefileDataStoreFactory.createNewDataStore(paramMap);
            //第三种方式,这种方式可适用于各种基于SPI模式的文件读写
            DataStoreFinder.getDataStore(paramMap);
        }
    
    }
    复制代码

     

    好了,今天关于Geotools写入shape的代码就分享到这里,而关于shape文件的操作,还有很多内容,其中最主要的过滤(Filter)后续也会出个专题来记录下,毕竟这里的东西很多。

     

  • 相关阅读:
    教你把mov格式的视频转换mp4
    1.2Python 三方库的安装以 pandas 为例_python量化实用版教程(初级)
    Redis学习笔记(下):持久化RDB、AOF+主从复制(薪火相传,反客为主,一主多从,哨兵模式)+Redis集群
    serverless全栈全场景云开发概要
    基于单片机的智能数字电子秤proteus仿真设计
    国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
    论坛回顾|用社区和开发者工具驱动软件供应链安全治理——章华鹏
    超低延迟直播技术路线,h265的无奈选择
    编译VTK静态库
    Linux学习之vim正常模式和插入模式
  • 原文地址:https://www.cnblogs.com/share-gis/p/17644550.html