ArcPy脚本——批量Shp合并脚本

以下代码是使用ArcToolBoxs工具运行的,如果需要直接在脚本中运行,修改接受参数方式即可。

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# Created on: 2018-08-18 
#   (generated by GL:qq,695396984)
# Description: 
# ---------------------------------------------------------------------------
#shp文件夹
workspace = arcpy.GetParameterAsText(0)
#输出文件夹
output_folder = arcpy.GetParameterAsText(1)
arcpy.env.workspace = workspace
Dict = {}
 
#"walk" through subfolders and below, and drop all contents into the empty dictionary
#unique shapefile names as keys, and with their file path as values.
for root, dirs, files in os.walk(workspace):
    for dir in dirs:
        arcpy.env.workspace = os.path.join(root,dir)
        for fc in arcpy.ListFeatureClasses():
            if not fc in Dict:
                Dict[fc] = []
                Dict[fc].append(os.path.join(root,dir,fc))
            else:
                Dict[fc].append(os.path.join(root,dir,fc))
 
#loop through each key and merge them together
for key in Dict:
    output = os.path.join(output_folder,key[:-4]) + ''
    arcpy.Merge_management(Dict[key], output)
    print output + " created"
  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我了解了你的问题。你想要编写一个 ArcGIS Python 脚本,能够根据要素类(shp)的一个字段内容,将这个要素类拆分成多个要素类,对吗? 那么,你可以按照以下步骤来编写这个脚本: 1. 首先,导入必要的 Python 模块和 ArcGIS 模块,包括 arcpy 和 os 模块。 ``` import arcpy import os ``` 2. 定义输入要素类的路径和字段名称。 ``` in_fc = r"path/to/input/feature/class.shp" split_field = "field_name" ``` 3. 使用 arcpy.da.SearchCursor 函数遍历要素类中的每个要素,并根据指定字段的值创建一个字典,其中键为字段值,值为要素类中包含该字段值的所有要素的列表。 ``` split_dict = {} with arcpy.da.SearchCursor(in_fc, [split_field]) as cursor: for row in cursor: if row[0] not in split_dict: split_dict[row[0]] = [] split_dict[row[0]].append(row) ``` 4. 遍历字典中的每个键值对,使用 arcpy.CopyFeatures_management 函数将包含每个键值对所对应字段值的所有要素复制到一个新的要素类中。 ``` out_folder = r"path/to/output/folder" for value, rows in split_dict.items(): out_name = "split_" + str(value) + ".shp" out_path = os.path.join(out_folder, out_name) arcpy.CopyFeatures_management(rows, out_path) ``` 5. 完整的代码如下所示: ``` import arcpy import os in_fc = r"path/to/input/feature/class.shp" split_field = "field_name" out_folder = r"path/to/output/folder" split_dict = {} with arcpy.da.SearchCursor(in_fc, [split_field]) as cursor: for row in cursor: if row[0] not in split_dict: split_dict[row[0]] = [] split_dict[row[0]].append(row) for value, rows in split_dict.items(): out_name = "split_" + str(value) + ".shp" out_path = os.path.join(out_folder, out_name) arcpy.CopyFeatures_management(rows, out_path) ``` 这个脚本将会根据输入要素类的指定字段值,将要素类拆分成多个要素类,并将这些要素类保存到指定的输出文件夹中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值