在arcpy中我们通常需要分区统计来计算值而且分区统计的时候需要统计多个栅格的情况。因而我写了这个代码以期待给大家进行分享。# 分区统计到表# 裁剪到指定区域importosimportsubprocess#路径shpfilerC:\Users\DELL\Desktop\内蒙生态系统评估FIN\内蒙生态系统评估\data\生态退化\26植被分区\26内蒙植被分区.shpoutput_file_dbfrC:\Users\DELL\Desktop\内蒙生态系统评估FIN\内蒙生态系统评估\data\生态退化\zone_dbfoutput_file_excrC:\Users\DELL\Desktop\内蒙生态系统评估FIN\内蒙生态系统评估\data\生态退化\zone_excelos.makedirs(output_file_dbf,exist_okTrue)os.makedirs(output_file_exc,exist_okTrue)# to sum excelto_sumfilerC:\Users\DELL\Desktop\内蒙生态系统评估FIN\内蒙生态系统评估\data\生态退化\merged_output.xlsx# 栅格数据路径env_workspacerC:\Users\DELL\Desktop\内蒙生态系统评估FIN\内蒙生态系统评估\data\生态退化\内蒙植被覆盖度mask下面是具体的代码情况。importpandasaspdfromarcpyimportenvimportarcpyimportosfromarcpy.saimport*importsys# # 解析命令行参数# output_file_dbf sys.argv[1]# output_file_exc sys.argv[2]# to_sumfile sys.argv[3]# env_workspace sys.argv[4]# shpfile sys.argv[5]# 设置默认值如果没有给值的话# if len(sys.argv) 7:# zoneField WYZ # 默认分区字段# else:# zoneField sys.argv[6]zoneFieldWYZ# 默认分区字段## 参数设置# 要提取的列索引 # 注意索引从0开始col_indices[1,5]# 分区的类型zone_type1#### 栅格数据路径env_workspaceenv_workspace#E:\BaiduSyncdisk\西安生态修复论文\zone_analyse\workspace\zone_analyse\ES\indicatorenv.workspaceenv_workspace rasterlistarcpy.ListRasters(*,TIF)# 允许重复写入arcpy.gp.overwriteOutputTruedefzone_stat(inZoneData,zoneField,inValueRaster,outTable):# Check out the ArcGIS Spatial Analyst extension licensearcpy.CheckOutExtension(Spatial)# Execute ZonalStatisticsAsTableZonalStatisticsAsTable(inZoneData,zoneField,inValueRaster,outTable,DATA,ALL)#,percentile_values 90forrasterinrasterlist:save_pathos.path.join(output_file_dbf,os.path.basename(raster))# E:\BaiduSyncdisk\西安生态修复论文\zone_analyse\workspace\zone_analyse\ES\region_zone# 统计栅格数据在矢量区域内的统计值zone_stat(shpfile,zoneField,raster,save_path)print(save_path)# 这块有个伴随生成的东西我不清楚如何删除它。后续可以弄明白。print(Finshed)#def to excelenv.workspaceoutput_file_dbf excel_filearcpy.ListTables(*,ALL)numlen(excel_file)foriinrange(0,num):env.workspaceoutput_file_dbf excel_filearcpy.ListTables(*,ALL)# Creat folder and name fileoutput_fileoutput_file_excifos.path.lexists(output_file)False:# 判断系统是否存在该指定路径若不存在则创建该路径与子文件夹os.makedirs(output_file)Save_nameexcel_file[i][:-4].xlssave_pathos.path.join(output_file,Save_name)# Export datearcpy.TableToExcel_conversion(excel_file[i],save_path,NAME,CODE)#arcpy_table_excel.table_to_excel(excel_file[i], save_path)print(Save_name)print(finished)print(num)# 设置文件夹路径folder_pathoutput_file_exc# 替换为你自己的路径output_fileto_sumfile# 存放每个文件提取后的 DataFramemerged_columns[]# 遍历文件夹中所有 .xls 文件forfilenameinos.listdir(folder_path):iffilename.endswith(.xls):file_pathos.path.join(folder_path,filename)try:dfpd.read_excel(file_path,enginexlrd)selecteddf.iloc[:,col_indices]# 获取原始列名original_colnamesdf.columns[col_indices]# 重命名文件名_原始列名new_colnames[f{filename}_{col}forcolinoriginal_colnames]selected.columnsnew_colnames# 重置索引以确保行对齐可选selectedselected.reset_index(dropTrue)merged_columns.append(selected)exceptExceptionase:print(f处理文件{filename}出错{e})# 横向合并所有 DataFrameifmerged_columns:final_dfpd.concat(merged_columns,axis1)final_df.to_excel(output_file,indexFalse)print(f成功保存为{output_file})else:print(未找到可处理的数据。)