YouTube | Facebook | X(Twitter) | RSS

マルチバンド→シングルバンド

2016/9/1 (木)

#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

関連記事

  • この記事を書いた人

羽田 康祐

伊達と酔狂のGISエンジニア。GIS上級技術者、Esri認定インストラクター、CompTIA CTT+ Classroom Trainer、潜水士、PADIダイブマスター、四アマ。WordPress は 2.1 からのユーザーで歴だけは長い。 代表著書『地図リテラシー入門―地図の正しい読み方・描き方がわかる』 GIS を使った自己紹介はこちら。ESRIジャパン(株)所属、元青山学院大学非常勤講師を兼務。日本地図学会第31期常任委員。発言は個人の見解です。

-プログラミング, ArcGIS
-

WINGFIELD since1981をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む