Arcpy ArcGIS – Why Make Raster Layer Function Does Not Produce Right Output in ArcGIS Desktop

arcgis-10.2arcgis-desktoparcmaparcpy

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.

Best Answer

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"))
Related Question