Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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
share|improve this answer
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? –  kolor Nov 18 '11 at 22:17
    
Is that the only IMG, or did others work? –  nmpeterson Nov 18 '11 at 22:55
1  
Actually, nevermind. That file is indeed corrupted! –  kolor Nov 18 '11 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. –  kolor Nov 19 '11 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 '11 at 17:05
add comment

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
>>> 
share|improve this answer
3  
Go with nmpeterson's solution, he is spot on. –  Chad Cooper Nov 18 '11 at 22:14
    
Thanks for your help! –  kolor Nov 18 '11 at 22:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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