使用arcpy进行批量处理shp相交问题

查询一个shp与存在文件夹中多个shp是否相交,查询并输出结果

我用的是arcgis10.2,系统自动安装python2.7

anaconda2,可以与annacoda3同时安装。anaconda2使用清华镜像,具体安装过程,参考b站视频。【ArcGIS进阶教程】1 anaconda与arcpy模块的安装与python开发环境配置_哔哩哔哩_bilibili

安装完成后,使用jupyter运行,我的代码水平有限,只是给大家个参考。

# -*- coding: utf-8 -*-
#1
#不能删除4号测试非重叠无数据跑的最好的
#最终版本的数据
import arcpy
import os

def compare_shapefiles(main_shapefile, compare_folder):
    # 设置工作空间
    arcpy.env.workspace = compare_folder

    # 获取主 Shapefile 的空间参考信息
    main_desc = arcpy.Describe(main_shapefile)
    main_spatial_reference = main_desc.spatialReference

    # 获取比较文件夹中的所有 Shapefile
    compare_shapefiles = arcpy.ListFeatureClasses()

    # 统计变量
    total_files = len(compare_shapefiles)
    overlap_files = 0
    non_overlap_files = 0
    overlap_file_list = []
    non_overlap_file_list = []

    # 遍历比较文件夹中的每个 Shapefile
    for compare_shapefile in compare_shapefiles:
        # 获取比较文件的空间参考信息
        compare_desc = arcpy.Describe(compare_shapefile)
        compare_spatial_reference = compare_desc.spatialReference

        # 判断空间参考是否一致
        if main_spatial_reference.name == compare_spatial_reference.name:
            # 删除已存在的 in_memory\temp_intersect
            arcpy.management.Delete("in_memory/temp_intersect")

            # 进行空间关系分析
            arcpy.analysis.Intersect([main_shapefile, compare_shapefile], "in_memory/temp_intersect")

            # 获取重叠区域数量
            overlap_count = int(arcpy.management.GetCount("in_memory/temp_intersect").getOutput(0))

            # 判断是否存在重叠区域
            if overlap_count > 0:
                print(u"{} 与 {} 有重叠".format(main_desc.baseName, compare_desc.baseName))
                overlap_files += 1
                overlap_file_list.append(compare_desc.baseName)
            else:
                print(u"{} 与 {} 无重叠".format(main_desc.baseName, compare_desc.baseName))
                non_overlap_files += 1
                non_overlap_file_list.append(compare_desc.baseName)
        else:
            print(u"{} 与 {} 空间参考不一致".format(main_desc.baseName, compare_desc.baseName))

    # 输出统计信息
    print(u"\n总文件个数:{}".format(total_files))
    print(u"重叠文件个数:{}".format(overlap_files))
    print(u"无重叠文件个数:{}".format(non_overlap_files))

    print(u"\n重叠文件列表:")
    for overlap_file in overlap_file_list:
        print(overlap_file)

    print(u"\n无重叠文件列表:")
    for non_overlap_file in non_overlap_file_list:
        print(non_overlap_file)

    # 添加结束语
    print("\n程序运行结束。")

if __name__ == "__main__":
    main_shapefile = ur"E:\01-zyf\\shp\区域2.shp"#查询shp文件存放位置
    compare_folder = ur"E:\01-zyf\\01-原始数据\16-M1"#批量数据存放位置

    compare_shapefiles(main_shapefile, compare_folder)

#根据天气运行,因为有时候可以处理上百有时候就40个shp
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值