make raster layer using each band separately (ArcGIS 10.0)

2182
2
12-05-2012 07:52 PM
KatSuda
New Contributor
Hello,

I am trying to take several classifications of the same image and automate the symbology so that I can visually inspect before going any further.  This might not even be the best way to do it, since I won't be able to apply the symbology from a layer as I can't make a single band raster (after the slice operation on the layers).

So far I have tried to make the bands of the class probability raster into raster layers.  I tried setting up a list of the bands by class name and then using the band index number to name the layer according to the band list.  I'm not sure what I've done wrong, but I get the error message posted below.

Also, this is not going to be made into a tool right away, so if anyone has any ideas of how to apply symbology to a single band raster output from a slice, I would really love to hear from you.  I will be using the natural breaks to divide the data into 2 values.  I don't see that option with the other reclass tools.

Thanks for any help.

krs

#make list of classified rasters 
        classif = arcpy.ListDatasets("cp*", "Raster")
        
        #make list of bands
        bands=["blank", "cld_w_1", "cld_g_2", "ipv_dk_3", "ipv_md_4", "ipv_lt_5", \
               "ipv_brt_6", "conc_7", "soil_8", "decid_9", "drGrs_10", "evgrn_11", \
               "grGrs_12", "bShd_i_13", "tShd_i_14", "bShd_v_15", "tShd_v_16"]
        
        #Loop through list and make raster layers by band
        for tiles in classif:
            for band in bands[1:]:
                arcpy.MakeRasterLayer(tiles, band + ".lyr", "", bands())
                arcpy.SaveToLayerFile_management(band, band + ".lyr", "RELATIVE")

        #list the new layer files
        layers = arcpy.ListDatasets("*.lyr", "All")
        
        #slice the rasters
        for layer in layers:
            outslice = Slice(layer, 2, "NATURAL_BREAKS", 1)
            print(outslice)  # + outslice)


The "blank" at index (0) is there so I can start with index (1) to match up the names.

ERROR messages:
ARCPY ERRORS:


PYTHON ERRORS:
Traceback Info:
  File "C:/scripts/test_make_layers.py", line 44, in main
    arcpy.MakeRasterLayer(tiles, band + ".lyr", "", bands())

Error Info:
     <type 'exceptions.AttributeError'>: 'module' object has no attribute 'MakeRasterLayer'
Tags (2)
0 Kudos
2 Replies
KatSuda
New Contributor
I have shortened the code to omit the slicing part, and I actually get layer files in my folder.  BUT they are composites - all 17 bands.  I had understood that by including a band index in the MakeRasterLayer operation, I would get only that band.

Have I misunderstood or just done something wrong?

Thanks for any insight.

krs

 #import system modules
import arcpy, os, sys, traceback
from arcpy import env

#set workspace
env.workspace="L:\FINAL\\output"
#print ("workspace defined")

#import mapping module and define the map
import arcpy.mapping
mxd=arcpy.mapping.MapDocument("L:\FINAL\\projectMap.mxd")
#print ("map defined")

#define data frame
df=arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
#print("data frame found")

#import spatial analyst
from arcpy.sa import *
print ("SA imported")

def main():

    try:
        if arcpy.CheckExtension("spatial") == "Available":
            #checkout extension
            arcpy.CheckOutExtension("spatial")
            print("SA available and checked out")
            
        #overwrite files with the same name
        env.overwriteOutput = True

        #make list of rasters
        tiles=arcpy.ListDatasets("CP*", "Raster")
        print("classes listed")  

        #make list of bands in each raster
        bands=["BLANK","cld_w_1","cld_dk2", "evgn_3", \
               "gr_grs_4", "decid_5", "drm_grs_6", "soil_7", \
               "ipv_dk_8", "ipv_md_9", "ipv_gn_10", "ipv_lt_11",\
               "conc_12", "ipv_br_13", "shv_ip_14", \
               "shb_ip_15", "shv_vg_16", "shb_vg_17"]
                
        #Loop through folder making raster layer by band
        for tile in tiles:
            for band in bands[1:18]:
                arcpy.MakeRasterLayer_management (tile, band, "", "", "bands[]")
                arcpy.SaveToLayerFile_management(band, band + "lyr", "RELATIVE")

    except:
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n     " +        str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
        msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"

        arcpy.AddError(msgs)
        arcpy.AddError(pymsg)

        print msgs
        print pymsg

        arcpy.AddMessage(arcpy.GetMessages(1))
        print arcpy.GetMessages(1)

main()
                
 
0 Kudos
KatSuda
New Contributor
I think my mistake was in the index portion of the code.

I have changed that to:

   #Loop through folder making raster layer by band
        for tile in tiles:
            for band in bands:
                index = 1
                while index>0:
                    arcpy.MakeRasterLayer_management (tile, band, "", "", index)
                    arcpy.SaveToLayerFile_management(band, band + "lyr", "RELATIVE")
                    index += 1 


The result of this is 17 one-band layer files named properly for the 17 classes, BUT they are all the same.  They are all class 17.

Does anyone have any ideas about this?  (I don't suppose I need to tell you that I haven't done much programming...)

Thanks again.

krs
0 Kudos