Loop in ModelBuilder for compositing bands of multiple Images

Create two models. One main and one sub. In the sub model, iterate through a folder to pick up the individual bands. Add Collect Values at the end of the model. Refer to the tool documentation for usage info. In the main model, call the sub model and pass the output from collect values to the composite bands tool.


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"