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<String,?> paramMap = new HashMap<>();
        /**
         * 通常有那些参数,我们可以通过下面的这个函数去查看,这里面
         */
        shapefileDataStoreFactory.createNewDataStore(paramMap);
        //第三种方式,这种方式可适用于各种基于SPI模式的文件读写
        DataStoreFinder.getDataStore(paramMap);
    }

}

 

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

 

posted @ 2023-08-20 20:44  开放GIS  阅读(827)  评论(0编辑  收藏  举报