2

I am trying to extract bands within rasters:

import arcpy
arcpy.env.workspace = 'd:/Rstack'
raster_list = arcpy.ListRasters("*RADIOMETRY.tif", "TIF")
list1 = []
for i in raster_list:
    arcpy.MakeRasterLayer_management(i, "lyr", "1")
    list1.append("lyr")

list1 returns a list of strings which the loop does not seem to go through make raster layer function. Any other way to batch extract bands? Extract sub dataset tool does not support tif files.

2

Okay, I see two things here. The Make Raster Layer tool makes a temporary layer, not a tangible file, so if you're looking for something that will persist after your ArcMap session is over, you need to take the layer you made and export it out to a raster file with something like the Copy Raster tool. Also, you're iterating through a list, but each time you're creating a layer named "lyr" so they're probably going to overwrite each other. And make sure that you're accounting for all the variables that the Make Raster Layer tool is expecting. (You can leave blank quotes if you don't have a value for the where query or the envelope.) Consider something like this:

import arcpy, os
arcpy.env.workspace = r'd:/Rstack'
raster_list = arcpy.ListRasters("*RADIOMETRY.tif", "TIF")
newDir = r'd:/Rstack/NewRasters' #An existing folder to stash your new rasters
for i in raster_list:
    print(i)
    lyr = arcpy.MakeRasterLayer_management(i, "tempLyr", "", "", "1")
    newFC = arcpy.CopyRaster_management(lyr,os.path.join(newDir,i))
    list1.append(newFC)

This will produce a folder of single band rasters, and also a list of the output files, if you need that.

If you don't want to write the rasters out the hard drive, this code should just make the layer files for you.

import arcpy
arcpy.env.workspace = r'd:/Rstack'
raster_list = arcpy.ListRasters("*RADIOMETRY.tif", "TIF")
list1 = []
for i in raster_list:
    lyrName = str(i) + "_temp"
    list1.append(arcpy.MakeRasterLayer_management(i, lyrName, "", "", "1"))
  • Thank you very much! This is a great solution! The reason why I am creating a list is that I want to stack the rasters of band 1 into that list. I have enabled overwriting to write the layer output to avoid too many file output. So there's no way to avoid outputing many layer files and re-read them into arcpy? – Yuyun He May 21 '16 at 1:17
  • You don't have to write the files out to the hard drive like I did with the copyraster tool, but if you want a list of layers, I think they're going to have to have unique layer names. – geobug May 21 '16 at 3:50
  • I tried the flowing: for i in rasters2: s += 1 arcpy.MakeRasterLayer_management(i, "d:/Rstack/test"+str(s),"2") a = "d:/Rstack/test" + str(s) list1.append(a) but it still returns a list of string and does not iterate through the Make Raster Layer function... – Yuyun He May 22 '16 at 3:21
  • 1
    Okay, if you just want layers, then you need a unique name, but not a path, because it really only lives in memory. Also, the Make Raster Layer function expects anything in the third variable slot to be a where expression, so put some comma-separated empty quotes in there to get your band selection into the fifth slot. I added a code alternative to my original answer above to demostrate. – geobug May 22 '16 at 23:58

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.