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

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

1 Answer 1

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)

answered Aug 13, 2015 at 16:56
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.