0

How do I get a list of band names using ArcPy in ArcGIS Desktop 10.x?

In ArcGIS Pro, I can use Raster('myRaster').bandNames, but this is not an option in ArcGIS 10.x and I cannot seem to figure out how to access band names.

1

If you want the band names you can set arcpy.env.workspace to the raster, then use arcpy.ListRasters() to list the bands, here's an example wrapped up into a function:

import arcpy

def get_bands(path_to_raster):
    """ Get a list of band names from a multiband raster """

    # Save previous workspace
    oldws = arcpy.env.workspace 

    #Get raster objects from band names
    arcpy.env.workspace = path_to_raster
    bands = arcpy.ListRasters()

    #Restore previous workspace
    arcpy.env.workspace = oldws

    return bands

Or if you want the full path:

import os
import arcpy

def get_bands(path_to_raster):
    """ Get a list of band names from a multiband raster """

    # Save previous workspace
    oldws = arcpy.env.workspace 

    #Get raster objects from band names
    arcpy.env.workspace = path_to_raster
    bands = [os.path.join(path_to_raster, b) for b in arcpy.ListRasters()]

    #Restore previous workspace
    arcpy.env.workspace = oldws

    return bands

If you don't actually care what the band names are and want to access a raster band by index (band number), use arcpy.MakeRasterLayer_management

arcpy.MakeRasterLayer_management(input_raster, "lyr_band3", band_index="3")

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.