Arcpy案例:规划分区类型统计

98 阅读1分钟

需求说明

在城乡规划项目中,需要统计一个地区的规划分区代码(GHFQDM)、规划分区名称(GHFQMC)、面积(MJ)三个字段的信息:

代码实现

核心函数

def planning_area_statics(in_feature,code_field,name_field,square_field,out_file):
    """
    规划分区统计
    :param in_feature: 输入表格
    :param code_field: 分区代码字段
    :param name_field: 分区名称字段
    :param square_field: 面积字段
    :param out_file: 输出excel表格路径
    :return:
    """
    # 统计结果保存字典
    res_square_dict={}
    # 用于暂时保存要素属性表中的字段信息
    feature_table_mapping=[]
    table=arcpy.SearchCursor(in_feature)
    # 读取要素属性表
    for row in table:
        feature_table_mapping.append([row.getValue(code_field),row.getValue(name_field),row.getValue(square_field)])
    # 先添加字典键数据
    for row in feature_table_mapping:
        res_square_dict[row[0]]={
            "square":0,
            "name":row[1],
            "count":0,
        }
    # 统计规划分区面积和出现次数
    for row in feature_table_mapping:
        res_square_dict[row[0]]["square"]+=row[2]
        res_square_dict[row[0]]["count"]+=1
    write_keys=res_square_dict.keys()
    write_keys.sort()
    wb=xlwt.Workbook()
    sheet=wb.add_sheet("GHFQ",cell_overwrite_ok=True)
    # 添加结果表表头
    sheet.write(0,0,code_field)
    sheet.write(0,1,name_field)
    sheet.write(0,2,"Count")
    sheet.write(0,3,square_field)
    # 将结果写入excel表
    for i,item in enumerate(write_keys):
        sheet.write(i+1,0,item)
        sheet.write(i+1,1,res_square_dict[item]['name'])
        sheet.write(i+1,2,res_square_dict[item]['count'])
        sheet.write(i+1,3,res_square_dict[item]['square'])
    # 保存excel表
    wb.save(out_file)

参数获取代码

# 要素路径
feature_path=arcpy.GetParameterAsText(0)
# 规划分区代码字段
dm_field=arcpy.GetParameterAsText(1)
# 规划分区名称字段
mc_field=arcpy.GetParameterAsText(2)
# 面积字段
mj_field=arcpy.GetParameterAsText(3)
# excel表的保存路径
excel_path=arcpy.GetParameterAsText(4)

ArcGIS工具导入

参数设置

工具运行界面:

运行结果: