2

I am working with rasters in arcpy and I am not able to access individual bands using the Raster class. The same code that worked about a year ago no longer works.

I should be able to access a single band of a raster like so:

Band3 = Raster(raster_filename + "/Band_3")

But the resulting Band3 variable has all 6 bands in the raster, not just band 3.

The "/Band_3" syntax appears to work fine in the Describe method in the first half of the following code but doesn't work in the Raster class later on:

    folder = r"C:\Users\elyons\Desktop\Spring_2018\Labs\New_LabData"

    import arcpy
    from arcpy.sa import *
    arcpy.CheckOutExtension("Spatial")

    arcpy.env.workspace = folder
    arcpy.env.overwriteOutput = True

    raster_filename = "Landsat.tif"

    #Create describe objects
    desc = arcpy.Describe(raster_filename)
    desc_band = arcpy.Describe(raster_filename + "/Band_3")

    #These are properties of the describe raster BAND object
    print "Height = " + str(desc_band.height)
    print "Width = " + str(desc_band.width)
    print "Resolution = " + str(desc_band.meanCellWidth)

    #This one is a property of the describe raster dataset object
    print "Number of Bands is = " + str(desc.bandCount)

    #Open bands as raster band objects
    Band3 = Raster(raster_filename + "/Band_3")

    #print number of bands
    print Band3.bandCount

    #Save the raster
    print "saving"
    Band3.save("savebandtest.tif")
    print "saved"


    arcpy.CheckOutExtension("Spatial")

The above code prints out the correct information for the describe objects and then prints 6 for Band3.bandCount, which is not right. It then saves a six band raster to the filename savebandtest.tif that is identical to Landsat.tif. I have tried different multiband rasters, different computers, getting the band name programatically, setting the working directory to the raster, forward slashes, backward slashes, everything I can think of.

Has the syntax changed for accessing single bands from multiband raster files? I can't find any mention of a change in the help.

2

It depends on whether your band names are actually "Band_1" to "Band_N" or something else like 'CoastalAerosol', 'Blue', 'Green', 'Red', etc...

If you want to access the bands by index, use the Make Raster Layer (Data Management) tool.

See also Access individual bands and use them in map algebra.

  • The band names in this case are the default "Band_1" etc. I used the method from the linked thread but am still having the same problem and it should be insensitive to the band names. – Evan Lyons May 29 '18 at 20:41

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.