需求:
用户有上千幅多波段栅格数据(.tif),由于操作失误,导致每个栅格数据在不同波段中出现nodata值的情况,用户的需求是想将栅格数据中的nodata值改成255。
思路:
(1)创建输出文件夹。创建文件夹用于存储处理后的栅格数据。
(2)遍历原始数据文件夹。
(3)对每一个遍历的栅格数据执行计算统计值、然后对每一个多波段数据的各个波段(band1、band2、band3)执行con条件函数运算、最后对每个多波段的单波段(band1、band2、band3)进行波段合成运算。
具体实现:
本次测试是通过ArcGIS10.6.1的IDLE(Python GUI)成功实现。
import arcpy
from arcpy import env
from arcpy.sa import *
import os
def mkdir(path):
# 去除首位空格
path=path.strip()
# 去除尾部 \ 符号
path=path.rstrip("\\")
# 判断路径是否存在
# 存在 True
# 不存在 False
isExists=os.path.exists(path)
# 判断结果
if not isExists:
# 如果不存在则创建目录
# 创建目录操作函数
os.makedirs(path)
print path+' 创建成功'
return True
else:
# 如果目录存在则不创建,并提示目录已存在
print path+' 目录已存在'
return False
env.workspace=r"E:\temp\output"
# 定义要创建的目录
mkpath="E:\\temp\\output\\t1\\"
mkpath1="E:\\temp\\output\\t2\\"
# 调用函数
mkdir(mkpath)
mkdir(mkpath1)
#遍历文件夹中的栅格数据
tifList=arcpy.ListRasters()
for tiftest in tifList:
print "打印栅格数据名称......"
print (tiftest)
#计算统计值
arcpy.CalculateStatistics_management(
tiftest, "1", "1", "",
"SKIP_EXISTING")
#将nodata改成255
d = arcpy.Describe(tiftest)
r=1
for rb in d.children:
print rb.name
bandname=tiftest+"/"+rb.Name
outCon = Con(IsNull(bandname),255, bandname)
outCon.save(mkpath+tiftest[:-4]+"b"+str(r)+ ".tif")
r=r+1
b1=mkpath+tiftest[:-4]+"b1.tif"
b2=mkpath+tiftest[:-4]+"b2.tif"
b3=mkpath+tiftest[:-4]+"b3.tif"
compbands=mkpath1+"compbands"+tiftest
print compbands+"..........."
#波段合成
arcpy.CompositeBands_management("%s;%s;%s"%(b1,b2,b3),"%s"%(compbands))
print "ok!!!"
操作过程:
链接:https://pan.baidu.com/s/1Ema-wfowgIQ0IGrD1xm1Xg
提取码:ltkv
参考资料:
https://www.cnblogs.com/hyl2018/p/8970769.html
https://www.cnblogs.com/xunbu7/p/8074417.html
https://www.cnblogs.com/landhu/p/6497975.html python空值为None