查询一个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 osdef 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