python实现栅格数据最大值合成法

今天需要用python实现栅格数据的最大值合成法,辗转颇久,偶然搜到了CellStatistics。哈哈,先用arcpy实现吧,有余力了再自己写(估计猴年马月了)。
根据多个栅格数据计算每个像元的统计数据。可用的统计数据有:众数、最大值、均值、中位数、最小值、少数、范围、标准差、总和及变异度。
在本示例中,将针对多个输入 Grid 栅格中的每个像元计算标准差,并将结果输出为 Grid 栅格。
CellStatistics (独立脚本)

# Name: CellStatistics_Ex_02.py
# Description: Calculates a per-cell statistic from multiple rasters
# Requirements: Spatial Analyst Extension

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

# Set environment settings
env.workspace = "C:/sapyexamples/data"

# Set local variables
inRaster01 = "degs"
inRaster02 = "negs"
inRaster03 = "cost"

# Execute CellStatistics
outCellStatistics = CellStatistics([inRaster01, inRaster02, inRaster03], "RANGE", "NODATA")

# Save the output 
outCellStatistics.save("C:/sapyexamples/output/cellstats")

演变:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Import system modules
import os
import arcpy
from arcpy import env
from arcpy.sa import *
import string

cpy.CheckOutExtension("spatial")  # 检查模块许可
arcpy.gp.overwriteOutput = 1
inputFilePath= "D:\\aaa"
npp_m= "D:\\aaa\\npp_m.tif"
threefile = [ ]
key = "2017"
for file in os.listdir(inputFilePath):
    if os.path.splitext(file)[1] == '.tif':  # 查找.tif文件
        if string.find(file, key) != -1:  # 满足条件往下进行
            sourcefile = os.path.join(inputFilePath, file)  # 拼路径
            threefile.append(sourcefile)

outCellStatistics = CellStatistics(threefile, "MAXIMUM", "DATA")

# Save the output
outCellStatistics.save(npp_m)

发布了6 篇原创文章 · 获赞 1 · 访问量 5463

猜你喜欢

转载自blog.csdn.net/aGang_Gg/article/details/86660437