Geotools实现shape文件的写入功能

 更新时间:2023年08月21日 10:21:37   作者:开放GIS  
Geotools作为开源的Java GIS三方库,已经成为GIS服务器端的主流开源库,其功能非常强大,涉及到GIS业务的方方面面,其中就包括GIS数据的读写,今天小编就借助Geotools来实现shape数据的写入,需要的朋友可以参考下

装配GeoTools有两种方式,一种是配置maven工程的pom文件(配置方式参考官网),另一种是下载geotools的jar包到本地导入依赖。我采用的是下载jar的方式,下载路径:https://sourceforge.net/projects/geotools/files/

众所周知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)后续也会出个专题来记录下,毕竟这里的东西很多。

相关文章

  • springboot2.6.7集成springfox3.0.0的示例代码

    springboot2.6.7集成springfox3.0.0的示例代码

    这篇文章主要介绍了springboot2.6.7集成springfox3.0.0的示例代码,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-04-04
  • Spring Boot之内嵌tomcat版本升级操作示例

    Spring Boot之内嵌tomcat版本升级操作示例

    这篇文章主要为大家介绍了Spring Boot之内嵌tomcat版本升级操作示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • spring boot executable jar/war 原理解析

    spring boot executable jar/war 原理解析

    spring boot里其实不仅可以直接以 java -jar demo.jar的方式启动,还可以把jar/war变为一个可以执行的脚本来启动,比如./demo.jar,这篇文章主要介绍了spring boot executable jar/war 原理,需要的朋友可以参考下
    2023-02-02
  • 使用Spring开启@Async异步方式(javaconfig配置)

    使用Spring开启@Async异步方式(javaconfig配置)

    这篇文章主要介绍了使用Spring开启@Async异步方式(javaconfig配置),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Spring框架开发IOC两种创建工厂方法详解

    Spring框架开发IOC两种创建工厂方法详解

    这篇文章主要介绍了Spring框架IOC两种创建工厂方法详解,文中附含详细的代码示例分别对静态方法和实例方法创建工厂作了简要的分析
    2021-09-09
  • 利用过滤器修改response中的返回值

    利用过滤器修改response中的返回值

    文章介绍了如何通过继承HttpServletResponseWrapper并重写Response对象的方法来获取response中的返回值,同时还分享了如何配置过滤器
    2024-12-12
  • Spring源码解密之自定义标签与解析

    Spring源码解密之自定义标签与解析

    这篇文章主要给大家介绍了关于Spring源码解密之自定义标签与解析的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参借鉴,下面随着小编来一起学习学习吧。
    2018-01-01
  • SpringBoot学习之基于注解的缓存

    SpringBoot学习之基于注解的缓存

    spring boot对缓存支持非常灵活,我们可以使用默认的EhCache,也可以整合第三方的框架,只需配置即可,下面这篇文章主要给大家介绍了关于SpringBoot学习之基于注解缓存的相关资料,需要的朋友可以参考下
    2022-03-03
  • 浅析Java 并发编程中的synchronized

    浅析Java 并发编程中的synchronized

    这篇文章主要介绍了Java 并发编程中的synchronized的相关资料,帮助大家更好的理解和学习Java并发编程,感兴趣的朋友可以了解下
    2020-12-12
  • SpringBoot + MySQL 实现 SSL 连接配置

    SpringBoot + MySQL 实现 SSL 连接配置

    本文主要介绍了SpringBoot + MySQL 实现 SSL 连接配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2026-05-05

最新评论