I am trying to stack bands 3 and 4 together for numerous landsat scenes, and I am trying to do it using a loop and arcpy.CompositeBands_management
. The code I am using is:
import arcpy
#set environment
arcpy.env.workspace=r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_masked\NDVI'
#path to band 3
band3 = arcpy.ListRasters("*band3*")
#path to band 4
band4 = arcpy.ListRasters("*band4*")
#loop that matches raster names and only executes process for those that match
for raster in band3:
rasterName = raster
stack = "" #initialize your variable to be populated in the inner loop
for band in band4:
if band.startswith(rasterName.split("_")[0]):
stack = band
break #break out of the inner loop - we have a match
if stack != "": #just to make sure we have something
#generic naming
outraster = rasterName.replace(".tif", "_stacked.tif")
arcpy.CompositeBands_management('rasterName;stack',outraster)
print('Done Processing')
but this returns the error:
ExecuteError: ERROR 000271: Cannot open the input datasets
Failed to execute (CompositeBands).
I am thinking it has something to do with this line:
arcpy.CompositeBands_management('rasterName;stack',outraster)
If i take the '
out of this line it still won't work though
Edit:
If it helps at all, when I print out the variables rasterName
, stack
and outraster
they are populated as I would expect
1 Answer 1
The first argument to arcpy.CompositeBands_management
should be a list of rasters. You can do this as a Python list (below) or as a string, with the rasters separated by ';', which is how the examples show it.
As your code is now, it's looking for two rasters in the current workspace named rasterName and stack, which is obviously not what you really want.
Instead of using the string format, just pass your rasters as a list:
arcpy.CompositeBands_management([rasterName,stack],outraster)
stack
. I use this code for other processes as well and change the variable names