Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 :(

share|improve this question
add comment

3 Answers 3

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.

share|improve this answer
    
Thanks @Nxau, I worked in the sub model and I can do the Composite Bands for one folder, however It cannot loop inside each folder. I realized I could also use "Iterate Folder" to repeat the SubModel on each folder, It does the first one but I got an error for next ones. Check images of Model and the SubModel –  crianopa Jul 16 at 17:24
    
To loop through subfolders, activate the recursive option in the iterator tool –  Nxau Jul 16 at 21:31
add comment

Ok, The model is ready: It takes every Band from the main folder and sub folders to do a "Composite Bands". The result is saved in an "Ouput Path". A "Calculate Value" was used to just rename the result with the desirable part of the original name, avoiding, for instance, the double ".tif.tif". The SubModel didn't change :)

FinalModel SubModel

share|improve this answer
add comment

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"
share|improve this answer
    
Thanks @Aaron it works Perfect!!! –  crianopa yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.