【ArcGIS自定义脚本工具】绘图:栅格统计结果变化图
一、功能介绍根据像元统计类型的不同,绘制下面四类图像中的一种。二、脚本代码代码的整体流程是利用ArcGIS内置的"波段集统计"工具(位置为Spatial Analyst->多元分析->波段集统计)生成栅格的统计结果txt文件至所在目录,文件内容样式如下:之后利用python读取这个文件,获取对应的统计结果,再进行绘图。#!/usr/bin/python# -*- ###...
·
一、功能介绍
根据像元统计类型的不同,绘制下面四类图像中的一种。
二、脚本代码
代码的整体流程是利用ArcGIS内置的"波段集统计"工具(位置为Spatial Analyst->多元分析->波段集统计)生成栅格的统计结果txt文件至所在目录,文件内容样式如下:
之后利用python读取这个文件,获取对应的统计结果,再进行绘图。
#!/usr/bin/python
# -*- #################
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import arcpy
from arcpy import env
import os
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
# 设置标签为负号可显示,坐标轴可显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
def stat_r(filename):
with open(filename) as obj:
lines = obj.readlines()
# create list named "stats" to save result,the format likes this:
# [layer, min , max, mean, std]
end = lines.index("# ===============================================================\n")
stats = []
for i in range(6, end):
stats.append(lines[i].split())
return stats
def column_r(list_d, n):
column_list = []
for i in list_d:
column_list.append(i[n])
return column_list
# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")
raster_layer = arcpy.GetParameterAsText(0)
statistics_type = arcpy.GetParameterAsText(1)
x_axis_name = arcpy.GetParameterAsText(2)
list_1 = [raster_layer.split(';')[0]]
txt_path_a = os.path.split(list_1[0])[0]
env.workspace = txt_path_a
txt_path_b = "\\" + "statistic_result.txt"
# use raster_name to save x_label
raster_name = ['#']
if x_axis_name == '':
for i in raster_layer.split(';'):
raster_name.append(os.path.split(i)[1][:-4])
else:
raster_name.extend(x_axis_name.split(';'))
try:
os.remove(txt_path_b)
except WindowsError:
pass
arcpy.AddMessage(txt_path_a + txt_path_b)
arcpy.gp.BandCollectionStats_sa(raster_layer, txt_path_b, "BRIEF")
dict_sta = {}
list_sta = status_r(txt_path_a + txt_path_b)
if statistics_type == 'Min':
dict_sta["min"] = column_r(list_sta, 1)
x_min = map(float, list(range(1, len(dict_sta["min"])+1)))
y_min = map(float, dict_sta["min"])
plt.plot(x_min, y_min, 'gv-', linewidth=2, label='Min')
for x, y in zip(x_min, y_min):
plt.text(x+0.1, y, '%.2f' % y, ha='center', va='center', fontsize=10)
plt.xticks(x_min, raster_name, rotation=0)
elif statistics_type == 'Max':
dict_sta["max"] = column_r(list_sta, 2)
x_max = map(float, list(range(1, len(dict_sta["max"])+1)))
y_max = map(float, dict_sta["max"])
plt.plot(x_max, y_max, 'r^-', linewidth=2, label='Max')
for x, y in zip(x_max, y_max):
plt.text(x+0.1, y, '%.2f' % y, ha='center', va='center', fontsize=10)
plt.xticks(x_max, raster_name, rotation=0)
elif statistics_type == 'Mean':
dict_sta["mean"] = column_r(list_sta, 3)
x_mean = map(float, list(range(1, len(dict_sta["mean"])+1)))
y_mean = map(float, dict_sta["mean"])
plt.plot(x_mean, y_mean, 'bo-', linewidth=2, label='Mean')
for x, y in zip(x_mean, y_mean):
plt.text(x+0.05, y, '%.2f' % y, ha='left', va='center', fontsize=10)
plt.xticks(x_mean, raster_name, rotation=0)
elif statistics_type == 'STD':
dict_sta["std"] = column_r(list_sta, 4)
x_std = map(float, list(range(1, len(dict_sta["std"])+1)))
y_std = map(float, dict_sta["std"])
plt.plot(x_std, y_std, 'c*-', linewidth=2, label='STD')
for x, y in zip(x_std, y_std):
plt.text(x+0.1, y, '%.2f' % y, ha='center', va='center', fontsize=10)
plt.xticks(x_std, raster_name, rotation=0)
ax = plt.gca()
x_major_locator = MultipleLocator(1)
ax.xaxis.set_major_locator(x_major_locator)
plt.legend(loc=0)
plt.show()
三、工具参数
四、工具界面
脚本运行时的界面如下图
更多推荐
已为社区贡献15条内容
所有评论(0)