Java GeoTools实践-合并(Union)featureCollection(成功)两个featureCollection取交(intersection)(失败)

本文介绍了使用Geotools库的UnionFeatureCollection和IntersectionFeatureCollection进行Shapefile数据合并和求交的操作,探讨了如何解决性能瓶颈和结果不正确的问题,以及一种低效但可行的笨拙方法。

项目需要对多个shp文件做合并和求交,而geotools常用的几何工具都是针对单个Geometry的,若手动获取shp里的要素集合并遍历每个要素的geometry,可能效率会很低,所以找到了geotools的process工具,测试了一下

合并两个shp文件

用到的工具
UnionFeatureCollection
需要的依赖

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-process</artifactId>
            <version>${
   
   geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-process-feature</artifactId>
            <version>${
   
   geotools.version}</version>
        </dependency>

代码

    // ShapeFileReaderUtils和ShapeFileWriterUtils是自己定义的,内容见下面地址
    // https://blog.youkuaiyun.com/Neuromancerr/article/details/119981046
    public static void mergeTest() throws Exception {
   
   
        // 待合并的两个文件地址
        String pathHead = "D:\\mapresources\\test\\";
        String file1 = pathHead + "test1.shp";
        String file2 = pathHead + "test2.shp";
        // 获取要素集
        ContentFeatureCollection features1 = ShapeFileReaderUtils.getFeatureSourceFromShapeFile(file1).getFeatures();
        ContentFeatureCollection features2 = ShapeFileReaderUtils.getFeatureSourceFromShapeFile(file2).getFeatures();
        // 定义输出路径
        String outPut = "D:\\mapresources\\test\\testOut.shp";
        // 初始化
        UnionFeatureCollection collection2 = new UnionFeatureCollection();
        // 执行合并
        SimpleFeatureCollection result = collection2.execute(
                features1,
                features2
        );
        // 输出结果生成shp文件
        ShapeFileWriterUtils.buildShp(result, outPut)
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值