#coding:cp932
#マルチバンド → シングルバンド
import arcpy,os
inras = arcpy.GetParameterAsText(0) #入力ラスタ データセット
outWS = arcpy.GetParameterAsText(1) #出力ラスタ データセット
desc = arcpy.Describe(inras)
inpath = desc.catalogPath
BandCount = desc.bandCount
Bands = desc.children
desc2 = arcpy.Describe(outWS)
outWS_Type = desc2.workspaceType
arcpy.AddMessage("\n" + inras + u" は、" + str(BandCount) + u" バンドラスタです。\n")
if not arcpy.Exists(inpath) or desc.format == "AFR":
if outWS_Type == u"FileSystem":
tmpWS = arcpy.CreateFileGDB_management(outWS,"tmp.gdb")
tmpRas = arcpy.CopyRaster_management(inras,tmpWS.getOutput(0) + os.sep + "tmp")
elif outWS_Type == u"LocalDatabase":
tmpWS = outWS
tmpRas = arcpy.CopyRaster_management(inras,tmpWS + os.sep + "tmp")
tmp_desc = arcpy.Describe(tmpRas)
tmp_Bands = tmp_desc.children
if "." in str(arcpy.GetParameter(0)):
rasName = str(arcpy.GetParameter(0)).split(".")[0]
else:
rasName = str(arcpy.GetParameter(0))
i = 0
for tmp_Band in tmp_Bands:
if outWS_Type == u"FileSystem":
inras2 = tmpRas.getOutput(0) + os.sep + tmp_Band.name
outras = outWS + os.sep + rasName + "_" + Bands[i].name + ".tif"
elif outWS_Type == u"LocalDatabase":
inras2 = tmpWS + os.sep + "tmp" + os.sep + tmp_Band.name
outras = outWS + os.sep + rasName + "_" + Bands[i].name
arcpy.CopyRaster_management(inras2,outras)
arcpy.AddMessage("\n" + outras + u" の出力が完了しました。" + "\n")
i += 1
if outWS_Type == u"FileSystem":
if arcpy.Exists(tmpWS.getOutput(0)):
arcpy.Delete_management(tmpWS.getOutput(0))
elif outWS_Type == u"LocalDatabase":
if arcpy.Exists(tmpWS + os.sep + "tmp"):
arcpy.Delete_management(tmpWS + os.sep + "tmp")
elif arcpy.Exists(inpath):
for Band in Bands:
inras2 = inpath + os.sep + Band.name
if outWS_Type == u"FileSystem":
outras = outWS + os.sep + os.path.basename(inpath).split(".")[0] + "_" + Band.name + "." + os.path.basename(inpath).split(".")[1]
elif outWS_Type == u"LocalDatabase":
outras = outWS + os.sep + os.path.basename(inpath).split(".")[0] + "_" + Band.name
arcpy.CopyRaster_management(inras2,outras)
arcpy.AddMessage("\n" + outras + u" の出力が完了しました。" + "\n")
del outWS,desc2,desc
記事
