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?
-
Hi I am trying to replicate the same model at my end. However, not able to do so. Is it possible to share the toolbox? I am just wondering how that Output Path has come at the end which is linked to Composite Bands– NehaCommented Dec 18, 2018 at 9:08
-
HI @Neha... sorry for the late response. The Output Path is called %value% inside the Output Raster. I have some other toolbox to shared if needed. Let me know. Not sure if we can mention email contact by here.– crianopaCommented Jan 30, 2019 at 13:31
3 Answers
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"
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.
-
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– crianopaCommented Jul 16, 2014 at 17:24
-
To loop through subfolders, activate the recursive option in the iterator tool– NxaunxauCommented Jul 16, 2014 at 21:31
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 :)