arcpy_intersect&merge

主要想实现两条直线(其中一个线shp是固定的,另一个线shp是多个)之间交点的批量处理,并对结果进行整合,方便后续加载数据和增加经纬度坐标信息。

1 . 利用arcpy 中Intersect_analysis函数实现两直线相交功能,具体代码:
输入数据:
smallshape 为需要进行相交处理的多个线shp;
region 为某个固定的线shp.
目的:将smallshape 中每个线shp与region 进行intersect处理,得到交点shp.

import arcpy,os,datetime

from arcpy.sa import *

import pandas as pd

import re

from arcpy import env

path = r"存放需要处理的shp文件路径"

arcpy.env.workspace = path

arcpy.env.overwriteOutput = True

print("strart")

smallshapes  = [f for f in arcpy.ListFeatureClasses() if f.split('.')[0].isdigit()]#List a:

print(smallshapes)

#infeature = ["big.shp", "small1.shp"]

join_attributes = "ALL"

#cluster_tolerance = "-1 Unknown"

output_type = "POINT"

region = r"固定的线shp存放路径" #一个固定的shp

i = 0

for smallshape in smallshapes:

    #outfeature = r"输出文件路径" + "{0}.shp".format(i)

    outfeature = r"输出文件路径名称\intersect_" + str(i) #该程序输出路径命名为intersect_1,intersect_2,如果有其它要求,可以修改代码。

    print(smallshape)

    arcpy.Intersect_analysis([smallshape, region], outfeature, join_attributes, "", output_type)

    i+=1

print("end")

参考资料:
Using ArcPy with Intersect and loop:https://gis.stackexchange.com/questions/358430/using-arcpy-with-intersect-and-loop

ListFeatureClasses:https://desktop.arcgis.com/zh-cn/arcmap/latest/analyze/arcpy-functions/listfeatureclasses.htm

利用Intersect_analysis进行分区统计:https://zhuanlan.zhihu.com/p/61445743

相交 (分析):https://pro.arcgis.com/zh-cn/pro-app/latest/tool-reference/analysis/intersect.htm

2 . 将上面的结果整合merge成一个shp
注意这个步骤,需要控制输入点shp的个数,如果数量超出设定的范围,就会报错:
在这里插入图片描述
但是没有找到更好的方法,只能暂时分批次整合(每次小于1000进行操作)。

具体代码:

import arcpy,os

out_path =r"整合后文件路径" #整合后的文件路径

arcpy.env.workspace = r"需要整合的文件路径" #需要整合的shp路径

shplist = arcpy.ListFeatureClasses('*.shp')

print(shplist)

arcpy.Merge_management(shplist,os.path.join(out_path,'Merged_intersection.shp'))

print("done")

参考资料:
Simple Merge of Shapefiles in a single folder:https://community.esri.com/t5/python-questions/simple-merge-of-shapefiles-in-a-single-folder/td-p/337113

arcpy批量合并shp:https://blog.csdn.net/imxiezy/article/details/88605946

arcgis merge:https://desktop.arcgis.com/zh-cn/arcmap/10.3/tools/data-management-toolbox/merge.htm

-- coding: utf-8 -- import arcpy arcpy.env.overwriteOutput = True # 输入参数 input_feature_class = arcpy.GetParameterAsText(0) # 输入要素类 join_feature_class = arcpy.GetParameterAsText(1) # 连接要素类 output_feature_class = arcpy.GetParameterAsText(2) # 输出要素类 join_fields = arcpy.GetParameterAsText(3) # 连接字段 merge_fields = arcpy.GetParameterAsText(4) # 合并字段 min_area = arcpy.GetParameter(5) # 最小面积 # 创建空间连接 arcpy.SpatialJoin_analysis(input_feature_class, join_feature_class, output_feature_class, "JOIN_ONE_TO_MANY", "", "", "INTERSECT") # 创建输出要素类的字段列表 field_list = [f.name for f in arcpy.ListFields(output_feature_class)] # 创建合并字段的字典 merge_dict = {} # 遍历输出要素类中的要素 with arcpy.da.UpdateCursor(output_feature_class, field_list) as cursor: for row in cursor: # 如果要素面积大于最小面积,则进行合并 if row[0] > min_area: # 创建合并字段的键值 merge_key = tuple([row[field_list.index(f)] for f in merge_fields.split(";")]) # 如果合并字段的键值不存在,则添加到字典中 if merge_key not in merge_dict: merge_dict[merge_key] = [] # 将当前要素的连接字段值添加到字典中 join_values = [row[field_list.index(f)] for f in join_fields.split(";")] merge_dict[merge_key].append(join_values) # 删除不符合条件的要素 else: cursor.deleteRow() # 遍历合并字段的字典,将连接字段的值合并并更新到输出要素类中 with arcpy.da.UpdateCursor(output_feature_class, merge_fields.split(";")) as cursor: for row in cursor: merge_key = tuple(row) if merge_key in merge_dict: join_values = merge_dict[merge_key] join_values = ["/".join([str(v) for v in j]) for j in join_values] row = tuple(join_values) cursor.updateRow(row)运行错误:IndentationError: unexpected indent (空间连接.py, line 18) 执行(空间连接多对一)失败。请改正代码
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值