ArcGIS Desktop – Loop in ModelBuilder for Compositing Bands of Multiple Images

arcgis-desktopcompositeiterationloopmodelbuilder

I'm attempting to automate the "Composite Bands" tool somehow in ModelBuilder, but haven't been able to figure out an effective method.
I have landsat 7 data that is separated into folders and each holds 7 bands (tif). Basically I want to generate a model or script that composites all bands in the raster output for each location, and then saves the result in a output folder.
I've prepared a ModelBuilder that take each band and save it in its correspondent folder. However, the input raster just read 1 band, not all 6 bands. Any Solutions, maybe Python?
Model Builder

Composite Bands which just i raster uploaded :(

Best Answer

You mentioned that you are open to a Python solution, which is good because automating this process with Python is much easier and more flexible than a ModelBuilder approach.

First, import the necessary modules

import arcpy, os

Define the workspace that contains all of the folders with the Landsat imagery

arcpy.env.workspace = r'C:\imagery'

Specify where you would like the output to go

outws = r'C:\temp'

List all of the workspaces in the previously defined workspace

folders = arcpy.ListWorkspaces()

Iterate through this list of workspaces and create a new list within each iteration of unstacked raster bands e.g. ['LC80260272014159LGN00_B1', 'LC80260272014159LGN00_B2',...]

for folder in folders:
    arcpy.env.workspace = folder
    rasters = arcpy.ListRasters("*.tif")
    name = os.path.join(outws, rasters[1].split("_")[0] + ".tif")
    arcpy.CompositeBands_management(rasters, name)

Landsat files follows this form: LC80260272014159LGN00_B1.tif, so we need to strip off anything after the "_" and use the first basename as the output name. You can do this with various slicing methods and string manipulation in Python.

name = os.path.join(outws, rasters[1].split("_")[0] + ".tif")

The complete script:

import arcpy, os

arcpy.env.workspace = r'C:\imagery'
outws = r'C:\temp'

# list all folders in a directory

folders = arcpy.ListWorkspaces()

for folder in folders:
    arcpy.env.workspace = folder
    rasters = arcpy.ListRasters("*.tif")
    name = os.path.join(outws, rasters[1].split("_")[0] + ".tif")
    arcpy.CompositeBands_management(rasters, name)

print "Processing complete"
Related Question