Python Script to iterate numerous, single-band rasters for arcpy.CompositeBands

2410
2
04-14-2015 09:54 AM
CaseyCalamaio
New Contributor

I have a large collection of single-band geotiffs (NIR, Green, and Red) that I would like to composite using a Python script. The goal is to iterate through multiple folders in a main directory, identify each tiff as band1, band2, band3 to input into CompositeBands_management, and do this for every raster scene. To clarify, each image number, i.e. scene, has three single-band tiffs associated. For example, the naming convention is: TTC2500_Green_Channel_8.TIF where TTC2500 is the image number.

So far, my script can go through each folder and identify the specific bands based on the filename b...

import arcpy
import os
from arcpy.sa import *
from arcpy import env


env.workspace=(r"C:\Users\cc\Desktop\tcamPractice")
env.overwriteOutput = True


outws=(r"C:\Users\cc\Desktop\tcamPractice\false")
folders=arcpy.ListWorkspaces()


for root, dirs, files in os.walk(r"C:\Users\cc\Desktop\tcamPractice"):


    for raster in files:
        rasterlist=arcpy.ListRasters("*", "TIF")
        outraster=outws+"comp.tif"
        if raster.find("Green")>0:
            band1=raster
            print band1
        elif raster.find("Red")>0:
            band2=raster
            print band2
        elif raster.find("Nir")>0:
            band3=raster
            print band3
        #arcpy.CompositeBands_management([band3,band2,band1], outraster









Thanks Mr. Patterson.

0 Kudos
2 Replies
DanPatterson_Retired
MVP Esteemed Contributor

could you edit your post and use the Use Advanced Editor option and format your code using Python syntax.  Use Advanced Editor will be available when you access this thread directly and not through the mail notification

RebeccaStrauch__GISP
MVP Esteemed Contributor

The reason Dan is requesting that is spacing is very important in Python.  A cut-n-paste to GeoNet, without it the spacing correct makes it hard for others to debug since the first response will be your indentation being off. This link might help you modify you post .

Posting Code blocks in the new GeoNet

Also, adding the tag "python" will also get more geonet users to see your posting.

...edit....oops, you beat me to it.  Yea!