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

6041
11
Jump to solution
04-14-2015 02:42 PM
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 but I am having difficulty bringing them into the tool. The attached script has the Composite tool commented out but the result is a printed list of each geotiff. Any thoughts on how to move from here into an automated processing method to output new false color composite geotiffs?

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


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


outws=(r"C:\Users\cc\Desktop\tcamPractice\false")
for root, dirs, files in os.walk(r"C:\Users\cc\Desktop\tcamPractice\point1_tif"):
    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(raster, outraster)



0 Kudos
11 Replies
CaseyCalamaio
New Contributor

Jake, thanks for your help. Below is the script that iterates through all of the folders and rasters in a main directory, defines each single band TIF as the respective Nir, Red, and Green band for the CompositeBand tool, and finally outputs a false color composite for each scene, i.e. flight number. Some slight modifications to your suggestion but it works great! I have a few more organizational things to do, namely to save the composites from each flight path to a unique folder but I think I can handle that. Some of the techniques you've shown me will help in the future as I continue to learn Python georpocessing. I'm going to add to this script today a second tool that compiles an NDVI from the same datasets. Out of curiosity, is it generally more efficient to simply use raster objects and Python math operators as opposed to using the RasterCalculator tool from arcpy? Thanks again for you help!

import arcpy  
from arcpy import env  
env.overwriteOutput = 1  
  
workspace = r"C:\Users\\Desktop\tcamPractice"  
  
#create list  
list = []  
  
#populate list with flight, i.e. TTC2500  
walk = arcpy.da.Walk(workspace, datatype="RasterDataset", type="TIF")  
for dirpath, dirnames, filenames in walk:  
    for raster in filenames:  
        list.append(raster.split("_")[0])  
  
#remove duplicates from list  
list = dict.fromkeys(list)  
list = list.keys()  
  
for n in list:  
    walk = arcpy.da.Walk(workspace, datatype="RasterDataset", type="TIF")  
    for dirpath, dirnames, filenames in walk:  
        env.workspace = dirpath
        outws=r"C:\Users\\Desktop\falseColors"
        for raster in filenames:  
            if n + "_Green" in raster:  
                green = env.workspace + "\\" + raster  
            elif n + "_Red" in raster:  
                red = env.workspace + "\\" + raster  
            elif n + "_Nir" in raster:  
                nir = env.workspace + "\\" + raster  
    if green != '' and red != '' and nir != '':  
        compBands = '"' + nir + ';' + red + ';' + green + ';"'  
        compRasterName = "comp_" + str(n[3:]) + ".tif"  
        outputRaster = outws + "\\" + compRasterName  
        arcpy.CompositeBands_management(compBands, outputRaster) 
0 Kudos
JakeSkinner
Esri Esteemed Contributor

I won't be able to comment about the raster objects vs raster calculator as I don't have much experience in this area.  You may want to start a new thread about that discussion.

0 Kudos