Python地理数据处理 十八:arcpy批量处理数据之栅格图像的统计分析(Calculate Statistics)

本文介绍如何使用Python脚本结合ArcGIS工具批量处理地理空间数据,包括统计多个文件夹中.tif图像的基本属性(如平均值、标准差),并对这些图像进行批量裁剪。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 初步运行代码

在这里插入图片描述

目的:获取文件夹中所有tif格式的图像的统计数据,包括平均值、最大值、最小值、标准差等。
如果想在arcgis中获取,会非常的繁琐且耗时:

ArcGIS中calculate statistics工具的路径:

在这里插入图片描述

在这里插入图片描述
废话不多说直接上代码

# -*- coding: cp936 -*-
import arcpy
import os
import glob
import arcpy
from arcpy.sa import *

arcpy.CheckOutExtension("ImageAnalyst")
arcpy.CheckOutExtension("spatial")

input =  r"D:/arcgisDatasetFrost/"
Output = open('D:/arcgisDatasetFrost/data.csv', 'w')

rasters = glob.glob(os.path.join(input, "*.tif"))
whereClause = "VALUE = -32556"

for ras in rasters:
    outSetNull = SetNull(ras, ras, whereClause)
    meanValueInfo = arcpy.GetRasterProperties_management(outSetNull, 'MEAN')
    stdValueInfo = arcpy.GetRasterProperties_management(outSetNull, 'STD')
    meanValue = meanValueInfo.getOutput(0)
    stdValue = stdValueInfo.getOutput(0)
    print os.path.basename(ras).split('_')[0] + ',' + str(meanValue) + '\n'
    Output.write(os.path.basename(ras).split('_')[0] + ',' + str(meanValue) + '\n')

for ras in rasters:
    outSetNull = SetNull(ras, ras, whereClause)
    stdValueInfo = arcpy.GetRasterProperties_management(outSetNull, 'STD')
    stdValue = stdValueInfo.getOutput(0)
    print os.path.basename(ras).split('_')[0] + ',' + str(stdValue) + '\n'
    Output.write(os.path.basename(ras).split('_')[0] + ',' + str(stdValue) + '\n')

Output.close()
print("All project is OK!")

批量裁剪

# -*- coding: cp936 -*-
import arcpy
arcpy.CheckOutExtension("ImageAnalyst")
arcpy.CheckOutExtension("spatial")

arcpy.env.workspace = "D:\\arcgisDatasetFrost\\clip"#栅格文件路径
rasters = arcpy.ListRasters("*","tif")#也可以是其他的文件格式,如grd
mask = "D:\\arcgisDatasetFrost\\a.shp"# 填写你的shp文件位置
for raster in rasters:
    print str(raster)
    out = "D:\\arcgisDatasetFrost\\center\\" + raster
    arcpy.gp.ExtractByMask_sa(raster, mask, out)
    print("ma_" + raster + " has done!")
print("All project is OK!!!")

2. 2023/3/16代码改进

2.1 一次性对多个文件夹中的图层进行统计,并分别生成对应的.csv文件
# -*- coding: cp936 -*-
import arcpy
import os
import glob
import arcpy
import time
from arcpy.sa import *

start = time.clock()
arcpy.CheckOutExtension("ImageAnalyst")
arcpy.CheckOutExtension("spatial")

# 指定文件夹路径
folder_path = r"D:\data" # 填写你的文件路径
# 获取文件夹下所有文件名
file_names = os.listdir(folder_path)

# 将文件名存入向量
for file_name in file_names:
    inws =  r"D:\data\\" + file_name # 输入文件夹路径,与folder_path一致
    OutputFile = open(os.path.join(folder_path, file_name + '.csv'), 'w')#修改文件名后缀为.csv
    #OutputFile = open(r'D:\data\1.csv', 'w')

    rasters = glob.glob(os.path.join(inws, "*.tif"))
    whereClause = "VALUE = -32556"
    for ras in rasters:
        outSetNull = SetNull(ras, ras, whereClause)
        meanValueInfo = arcpy.GetRasterProperties_management(outSetNull, 'MEAN')
        #stdValueInfo = arcpy.GetRasterProperties_management(outSetNull, 'STD')
        meanValue = meanValueInfo.getOutput(0)
        #stdValue = stdValueInfo.getOutput(0)
        print os.path.basename(ras).split('_')[0] + ',' + str(meanValue) + '\n'
        OutputFile.write(os.path.basename(ras).split('_')[0] + ',' + str(meanValue) + '\n')

	for ras in rasters:
    	outSetNull = SetNull(ras, ras, whereClause)
    	stdValueInfo = arcpy.GetRasterProperties_management(outSetNull, 'STD')
    	stdValue = stdValueInfo.getOutput(0)
    	print os.path.basename(ras).split('_')[0] + ',' + str(stdValue) + '\n'
    	OutputFile.write(os.path.basename(ras).split('_')[0] + ',' + str(stdValue) + '\n')
    
    OutputFile.close()
    print("Subprojects is OK! ")
    print("--------------------------------------------------------------------------------")
    print("--------------------------------------------------------------------------------")

end = time.clock()  
print("All project is OK!!!")
print (str(end-start))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Jackson的生态模型

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

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

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

打赏作者

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

抵扣说明:

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

余额充值