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

相关文章

  • Java并发编程学习之ThreadLocal源码详析

    Java并发编程学习之ThreadLocal源码详析

    这篇文章主要给大家介绍了关于Java并发编程学习之源码分析ThreadLocal的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-06-06
  • Java从入门到起飞之变量与运算符详解

    Java从入门到起飞之变量与运算符详解

    这篇文章主要介绍了Java编程语言中的关键字、标识符、变量、基本数据类型以及运算符等基本概念和用法,它涵盖了变量声明、赋值、类型转换、字符串操作以及运算符优先级等内容,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-03-03
  • Spring中单例和多例的深入理解

    Spring中单例和多例的深入理解

    这篇文章主要介绍了Spring中单例和多例的深入理解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • httpclient ConnectionHolder连接池连接保持源码解析

    httpclient ConnectionHolder连接池连接保持源码解析

    这篇文章主要为大家介绍了httpclient ConnectionHolder连接池连接保持源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • SpringBoot集成thymeleaf渲染html模板的步骤详解

    SpringBoot集成thymeleaf渲染html模板的步骤详解

    这篇文章主要给大家详细介绍了SpringBoot集成thymeleaf如何使实现html模板的渲染,文中有详细的代码示例,具有一定的参考价值,需要的朋友可以参考下
    2023-06-06
  • Java中的SuppressWarnings注解使用

    Java中的SuppressWarnings注解使用

    这篇文章主要介绍了Java中的SuppressWarnings注解使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • Java实现批量操作Excel的示例详解

    Java实现批量操作Excel的示例详解

    在操作Excel的场景中,通常会有一些针对Excel的批量操作,以GcExcel为例,为大家详细介绍一下Java是如何实现批量操作Excel的,需要的可以参考一下
    2023-07-07
  • SpringCloud中的Seata基本介绍与安装教程

    SpringCloud中的Seata基本介绍与安装教程

    Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务,这篇文章主要介绍了SpringCloud之Seata基本介绍与安装,需要的朋友可以参考下
    2024-01-01
  • Java实现任务管理器性能网络监控数据的方法详解

    Java实现任务管理器性能网络监控数据的方法详解

    在现代操作系统中,任务管理器是一个非常重要的工具,用于监控和管理计算机的运行状态,包括CPU使用率、内存占用等,对于开发者和系统管理员来说,了解这些性能数据有助于优化应用程序和系统性能,本文将介绍如何使用Java编写一个简单的程序来监控网络性能数据
    2025-01-01
  • 基于Java编写emoji表情处理工具类

    基于Java编写emoji表情处理工具类

    这篇文章主要为大家详细介绍了如何基于Java编写一个emoji表情处理工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-03-03

最新评论