ArcPy 批处理之 [ 批量采样栅格属性值构建箱型图(BoxPlot)的数据准备]
将单幅栅格影像的属性值参与到箱型图中的一个Box中,考虑像元数量过多,进行1%或者0.5%采样,此处额外统计最小值、最大值、均值和中位数。
ArcPy 代码(此处为3.x 版本,与Python2.7 存在差异):
#encoding:utf-8 import os import arcpy from arcpy import env from arcpy.sa import * import numpy as np import pandas as pd arcpy.env.overwriteOutput=1 inPath = r'D:\Data\gdb.gdb' outPath= r'D:\Data\\' out_file = "result.xlsx" arcpy.env.workspace = inPath files = arcpy.ListRasters() num=len(files) def calculate_percentiles(array): percentiles = np.nanpercentile(array, range(101)) max_val = np.nanmax(array) min_val = np.nanmin(array) median = np.nanmedian(array) mean = np.nanmean(array) return np.concatenate(([min_val, median, max_val, mean], percentiles)) result_df = pd.DataFrame(columns=['Array', 'Min', 'Median', 'Max', 'Mean'] + [f'pct_{i}%' for i in range(101)]) for i in range(num): np_array = arcpy.RasterToNumPyArray(Raster(files[i]), nodata_to_value=np.nan) percentiles = calculate_percentiles(np_array) result_df.loc[i] = [files[i]] + list(percentiles) print(files[i]+" ok") result_df.to_excel(outPath + out_file, index=False) print("OKK")
-END-