2

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

4
  • What value are you expecting rasterMask to hold? It never gets set to anything other than an empty string.
    – papadoo
    Aug 13 '15 at 16:14
  • sorry, typo it should have said stack. I use this code for other processes as well and change the variable names
    – spotter
    Aug 13 '15 at 16:17
  • make sure to change to arcpy.CompositeBands_management('rasterName;stack',outraster)
    – papadoo
    Aug 13 '15 at 16:24
  • I did, the code I am using had the fixes I just didn't have them here. Its same error.
    – spotter
    Aug 13 '15 at 16:28
2

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)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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