【ArcGIS自定义脚本工具】绘图:栅格统计结果变化图

一、功能介绍
1-1
根据像元统计类型的不同,绘制下面四类图像中的一种。
1-2

二、脚本代码
代码的整体流程是利用ArcGIS内置的"波段集统计"工具(位置为Spatial Analyst->多元分析->波段集统计)生成栅格的统计结果txt文件至所在目录,文件内容样式如下:2-1
之后利用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()

三、工具参数
3-1
3-2

四、工具界面
4-1
脚本运行时的界面如下图4-2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Salierib

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值