运行import arcpy # 定义输入图层和输出图层 input_layer = arcpy.GetParameterAsText(0) output_layer = arcpy.GetParameterAsText(1) # 定义合并条件 merge_field = arcpy.GetParameterAsText(2) # 要合并的字段名 merge_gap = arcpy.GetParameterAsText(3) # 相邻图斑面积差距 # 定义面积筛选条件 selection_field = arcpy.GetParameterAsText(4) # 用于筛选的字段名 selection_value = arcpy.GetParameterAsText(5) # 筛选条件 selection_area = float(arcpy.GetParameterAsText(6)) # 面积筛选阈值 # 进行面积筛选 arcpy.MakeFeatureLayer_management(input_layer, "layer", "{}='{}' AND SHAPE_AREA > {}".format(selection_field, selection_value, selection_area)) # 寻找相邻图斑 arcpy.PolygonNeighbors_analysis("layer", "neighbors", ["FID"]) # 定义合并列表 merge_list = [] # 进行合并 with arcpy.da.SearchCursor("neighbors", ['src_FID', 'nbr_FID', 'src_{}'.format(merge_field)]) as cursor: for row in cursor: if row[0] < row[1]: src_geom = arcpy.da.SearchCursor("layer", ["SHAPE@"], "FID={}".format(row[0])).next()[0] nbr_geom = arcpy.da.SearchCursor("layer", ["SHAPE@"], "FID={}".format(row[1])).next()[0] if src_geom.area < nbr_geom.area: src_geom, nbr_geom = nbr_geom, src_geom if src_geom.area - nbr_geom.area > merge_gap: merge_list.append(row[0]) merge_list.append(row[1]) arcpy.management.Dissolve("layer", output_layer, "{}".format(merge_field), None, "MULTI_PART", "DISSOLVE_LINES") # 删除被合并的图斑 with arcpy.da.UpdateCursor(output_layer, ['FID']) as cursor: for row in cursor: if row[0] in merge_list: cursor.deleteRow()时报错Traceback (most recent call last): File "D:\实验YY\新建文件夹 (2)\批量合并小图斑.py", line 13, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\analysis.py", line 984, in PolygonNeighbors raise e ExecuteError: 执行失败。参数无效。 ERROR 000732: 输入要素: 数据集 layer 不存在或不受支持 执行(PolygonNeighbors)失败。 执行(批量合并小图斑)失败。请改正代码
时间: 2023-06-27 11:04:21 浏览: 179
看起来你的代码中出错的地方在于第11行,你使用了一个不存在的数据集 "layer"。这可能是因为你在第10行中定义了一个名为 "layer" 的 feature layer,但是在第11行中你却使用了 "layer" 来作为输入数据集,这会导致程序无法找到相应的数据集。你可以尝试将第11行中的 "layer" 替换为 input_layer,这样应该就可以解决问题了。修改后的代码如下:
```
import arcpy
# 定义输入图层和输出图层
input_layer = arcpy.GetParameterAsText(0)
output_layer = arcpy.GetParameterAsText(1)
# 定义合并条件
merge_field = arcpy.GetParameterAsText(2) # 要合并的字段名
merge_gap = arcpy.GetParameterAsText(3) # 相邻图斑面积差距
# 定义面积筛选条件
selection_field = arcpy.GetParameterAsText(4) # 用于筛选的字段名
selection_value = arcpy.GetParameterAsText(5) # 筛选条件
selection_area = float(arcpy.GetParameterAsText(6)) # 面积筛选阈值
# 进行面积筛选
arcpy.MakeFeatureLayer_management(input_layer, "layer", "{}='{}' AND SHAPE_AREA > {}".format(selection_field, selection_value, selection_area))
# 寻找相邻图斑
arcpy.PolygonNeighbors_analysis("layer", "neighbors", ["FID"])
# 定义合并列表
merge_list = []
# 进行合并
with arcpy.da.SearchCursor("neighbors", ['src_FID', 'nbr_FID', 'src_{}'.format(merge_field)]) as cursor:
for row in cursor:
if row[0] < row[1]:
src_geom = arcpy.da.SearchCursor("layer", ["SHAPE@"], "FID={}".format(row[0])).next()[0]
nbr_geom = arcpy.da.SearchCursor("layer", ["SHAPE@"], "FID={}".format(row[1])).next()[0]
if src_geom.area < nbr_geom.area:
src_geom, nbr_geom = nbr_geom, src_geom
if src_geom.area - nbr_geom.area > merge_gap:
merge_list.append(row[0])
merge_list.append(row[1])
arcpy.management.Dissolve("layer", output_layer, "{}".format(merge_field), None, "MULTI_PART", "DISSOLVE_LINES")
# 删除被合并的图斑
with arcpy.da.UpdateCursor(output_layer, ['FID']) as cursor:
for row in cursor:
if row[0] in merge_list:
cursor.deleteRow()
```
阅读全文