Batch composite bands using arcpy

3627
11
Jump to solution
12-21-2017 10:28 PM
BrunoDeus1
New Contributor III

Branched from: Help: Batch - composite bands Arc-python 

Hi,

I would like use this script, but it fails. Some hint?

import arcpy, os

arcpy.env.workspace = r'C:\Teste_Auto_CLEIA\Ima_compactadas'

# list all folders in a directory
folders = arcpy.ListWorkspaces("L8*", "Folder") # Inside main directory, already have other folders, but the bands are always inside folders that begin with "L8", e.g. "L8227069".

#The output files place
outws = r'C:\Teste_Auto_CLEIA\Imagem' # Line 9

for folder in folders:
    arcpy.env.workspace = folder
    rasters = arcpy.ListRasters("*.tif") # I already erased the bands which I won't use
    chk4 = ['band{0}.tif'.format(i) for i in range(0, 7)] # I changed the range
    rasters = [a for a in rasters if a[-9:] in chk4]
    name = os.path.join(outws, rasters[1].split("_")[0] + ".tif") # I wrote "outws"
    arcpy.CompositeBands_management(rasters, name) # I put it
    print(rasters)

I'm using ArcMap 10.1 and the bands names aren't the same, for instance, "LO82270682017183CUB00_B1", ... , "LO82270682017183CUB00_B7".

Thanks!

0 Kudos
11 Replies
XanderBakker
Esri Esteemed Contributor

I'm glad it works!

0 Kudos
NihalAlbuquerque
New Contributor

This is the code I used for my Landsat work, put all the Landsat folders into a working folder. You can alter the code to suit your purposes. 

 

import arcpy
import glob
import os

#working drirectory with landsat data in individual folder
os.chdir('E:\GIS\landsat2019')
#list folders
folders = glob.glob('*/')
print(folders) 

#below code, makes a composite file for landsat bands in each folder    
for folder in folders:
    #list file name for each band
    filelist = glob.glob(folder +'/L*_B*.tif')
    print(filelist)
    #makes the folder the working directory
    arcpy.env.workspace = folder
    #creates file name based on folder name
    name = str(folder)
    names = str(name[0:-1]+'.tif')
    print (names)
    ##Compose multi types of single band raster datasets to a TIFF format raster and dataset
    arcpy.CompositeBands_management(filelist,names)
    print 'complete'
    #puts the file with the folder name, in each landsat folder

 

 

0 Kudos