Python batch processing MODIS raster image data - find the mean, sum and maximum and minimum values

1. Batch processing - multiple raster images - average value of spatial overlay

Task description: There are 12 months of remote sensing images in the folder to calculate the annual average, maximum value, cumulative value, etc. (the result is a raster image)
Data preparation: The folder (workspace) contains tif images that need to be processed in batches
Tool preparation: Python arcpy environment
operation: Just replace the path in the code

# coding=utf-8
import arcpy
from arcpy import env
from arcpy.sa import *

# Set environment settings
# 输入工作空间文件夹(即存放需批处理tif影像的文件夹)
env.workspace = "D:/DATA/2000" # 注意此处‘/’的方向 
# Set local variables
# 遍历工作空间中的tif格式数据
rasters = arcpy.ListRasters("*", "tif") 

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# MEAN均值;SUM总和;STD标准差;MINIMUM最小值;MAXIMUM最大值;
outCellStatistics = CellStatistics(rasters, "MEAN", "DATA")
# 输出结果影像的路径和名称
outCellStatistics.save("D:/Year_MEAN/2000_mean.tif")
print("All project is OK!")

2. Batch processing - the average value of each single raster image

Task description: There are images of vegetation coverage and surface temperature in the research area for 12 months in the folder, and find the average, maximum and minimum values ​​of vegetation coverage (surface temperature) in the area for 12 months (one image Generate a value)
Data preparation: The folder (workspace) contains tif images that need to be processed in batches
Tool preparation: Python arcpy Environment
operation: Just replace the path in the code

# -*- coding: UTF-8 -*-
import os
import arcpy
from arcpy.sa import *

arcpy.CheckOutExtension("ImageAnalyst")  # 检查许可
arcpy.CheckOutExtension("spatial")

inws = r"D:\DATA\04_T"

# 更改要存储结果的路径,自动生成表格
OutputFile = open('D:/08_Mean_Value/Mean.txt'.decode('utf-8'), 'w')

arcpy.env.workspace = inws
rasters = arcpy.ListRasters("*", "tif")
print(rasters)

'''
# 利用glob包,将inws下的所有tif文件读存放到rasters中
rasters = glob.glob(os.path.join(inws, '*.tif'))
path = os.path.join(inws, '*.tif')
print(path)
'''

whereClause = "VALUE = 0"  # 去除异常值。如果无异常值可删去.

# 循环rasters中的所有影像,进行“求平均值”操作
for ras in rasters:
    outSetNull = SetNull(ras, ras, whereClause)  # 去除异常值;如果无异常值可删去

    meanValueInfo = arcpy.GetRasterProperties_management(outSetNull, 'MEAN')
    # MINIMUM —输入栅格中所有像元的最小值。
    # MAXIMUM —输入栅格中所有像元的最大值。
    # MEAN —输入栅格中所有像元的平均值。
    # STD —输入栅格中所有像元的标准差。
    # TOP —范围的顶部值或Y最大值(YMax)。
    # LEFT —范围的左侧值或X最小值(XMin)。
    # RIGHT —范围的右侧值或X最大值(XMax)。
    meanValue = meanValueInfo.getOutput(0)
    print os.path.basename(ras).split('_')[0] + ',' + str(meanValue) + '\n'
    OutputFile.write(os.path.basename(ras).split('_')[0] + ',' + str(meanValue) + '\n')

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

Guess you like

Origin blog.csdn.net/qq_43874102/article/details/124256877