4

I'm having troubles finding the band count for my raster files using ArcPy.

I need to first list all the raster files in a directory, and then display the band count for each file. I am able to list the files, but when it gets to the band count, it gives me an error stating that Method Band Count does not exist.

The following is what I have for my code so far:

import os
import arcpy

arcpy.env.workspace = "C:\\Rasters" # Contains TIFF, IMG, GRID formats
filePath = arcpy.env.workspace

rasterList = arcpy.ListRasters("*", "ALL")
desc = arcpy.Describe(filePath)

for names in rasterList:
    print names
    print desc.bandCount

Any ideas where I'm going wrong? I was using the Geoprocessor Programming Model as a reference.

2 Answers 2

5

Your Describe() is referencing the workspace (a folder) rather than the individual images contained within it. Try this:

import os 
import arcpy 

arcpy.env.workspace = "C:\\Rasters" # Contains TIFF, IMG, GRID formats 
filePath = arcpy.env.workspace 

rasterList = arcpy.ListRasters("*", "ALL") 

for name in rasterList:
    desc = arcpy.Describe(filePath + "\\" + name)
    print name
    print desc.bandCount
5
  • 1
    This works! Sweet. But for my last file (which is an IMG), it gives the same error as described in my initial post. Could this be because it has no band or is maybe corrupted?
    – kaoscify
    Nov 18, 2011 at 22:17
  • Is that the only IMG, or did others work?
    – nmpeterson
    Nov 18, 2011 at 22:55
  • 1
    Actually, nevermind. That file is indeed corrupted!
    – kaoscify
    Nov 18, 2011 at 23:20
  • 1
    nmpeterson - Any idea what I would have to do to insert a file validation system? For example, if someone scans a directory with shapefiles instead of raster data, how can I show a message stating that the directory does not contain the appropriate data.
    – kaoscify
    Nov 19, 2011 at 7:28
  • 1
    I could probably come up with something after thinking about it for a while, but I would suggest posting that as another question: get it more visibility and a better chance of receiving some great insights :)
    – nmpeterson
    Nov 19, 2011 at 17:05
4

You need to cast them to Raster objects first:

>>> import arcpy
>>> arcpy.Raster('NcLidarClipUTMmeters')
D:\Projects\VIESORE\GIS_Data\Testing.gdb\NcLidarClipUTMmeters
>>> r = arcpy.Raster('NcLidarClipUTMmeters')
>>> r.bandCount
1L
>>> t = arcpy.Raster('m1.img')
>>> t.bandCount
3L
>>> 
1
  • 3
    Go with nmpeterson's solution, he is spot on. Nov 18, 2011 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.