-1

I am now trying to display the information I got using Describe. For some reason, I keep getting error message saying Key error: 'pixelType.'. I have no idea why this gives me the error because this is exactly how it is worded in ArcGIS help website. Could someone please tell me what is wrong?

def data_report(workspace):

elements = defaultdict(list)

walk = arcpy.da.Walk(workspace, topdown=True)

for dirpath, dirnames, filenames in walk:
    for dirname in dirnames:  # 'FeatureDataset'
        desc = arcpy.da.Describe(os.path.join(dirpath, dirname))
        elements[desc['dataType']].append(desc)

    for filename in filenames:
        desc = arcpy.da.Describe(os.path.join(dirpath, filename))
        elements[desc['dataType']].append(desc)  

report = []

for element_type, element_list in elements.items():
    report += [f"{element_type}:"]
    for element_description in element_list:



if element_type == "RasterDataset":    
    report +=  [f"\t\t{element_description['name']}"]
    report +=  [f"\t\t\t\tThe number of band: {element_description['bandCount']}"]
    report +=  [f"\t\t\t\tThe format: {element_description['format']}"]
    report +=  [f"\t\t\t\tPixel type: {element_description['pixelType']}"]
  • I got the whole code, but I was told once not to post my entire code. But if it is necessary, I can post it. – Yoshi Feb 20 at 1:26
  • Ok, I added more code. Thank you. – Yoshi Feb 20 at 1:28
  • But then I do not know why everything else is displayed. The name, bandCount, and other properties are displayed on the screen when I run this code, but only pixel type gives me the error. – Yoshi Feb 20 at 1:31
  • 1
    Your indentation is off. – Aaron Feb 20 at 4:24
4

Here's the documentation for the Describe Raster Dataset.

There is no pixelType property there. However, as you can see at the top of the documentation, it can also include the properties for Raster Band ONLY IF the raster is a single-band raster.

Here's the documentation for the Describe Raster Band. As you can see, a Raster Band, and hence a single-band raster dataset would have a pixelType property.

So you must test if it is a single band raster before requesting this property. OR, test to see if the property exists before attempting to read it.

So, you could change the last part of your code to:

if element_type == "RasterDataset":    
    report +=  [f"\t\t{element_description['name']}"]
    report +=  [f"\t\t\t\tThe number of band: {element_description['bandCount']}"]
    report +=  [f"\t\t\t\tThe format: {element_description['format']}"]
    if 'pixelType' in element_description:
        report +=  [f"\t\t\t\tPixel type: {element_description['pixelType']}"]
|improve this answer|||||
2

In this case I've found it simpler and safer to use the children property of the describe object:

if element_type == "RasterDataset":
    InRas = arcpy.Raster(element_description['catalogPath']) # make a raster object
    Desc  = arcpy.Describe(InRas)
    PxType= Desc.children[0].pixelType

InRas=arcpy.Raster(r'C:\PATH\TO\RASTER\Raster3band.png')
Desc = arcpy.Describe(InRas)
print('Raster has {} bands with a pixel type of {}'.format(Desc.bandCount,Desc.children[0].pixelType))

returns Raster has 3 bands with a pixel type of U8

InRas=arcpy.Raster(r'C:\PATH\TO\RASTER\RasterSingleBand.img')
Desc = arcpy.Describe(InRas)
print('Raster has {} bands with a pixel type of {}'.format(Desc.bandCount,Desc.children[0].pixelType))
InRas = None # free the python object to free up resources not the dataset

returns Raster has 1 bands with a pixel type of F32

But first cast your path string to a Raster object before describing, I have found that some raster formats have issues being described directly, the Raster object causes less issues when described; commonly the bands can be acessed by element_description['catalogPath'] + r'\band_1' but in some instances band_1 is named Red, just to make life difficult, grabbing the first child is safer to get the properties of the first (and second, and third etc) band irrespective of the band naming.

|improve this answer|||||
  • Thank you, but how can I make that "print" part into "report +=" ? I need to add the result to the report – Yoshi Feb 20 at 3:42
  • I use string.format to pack up multiple variables. I find it easier to re-read later as the format string contains the layout with {} placemarkers = a shorter format string, then have a variable for each of your properties, then pack them all up to a list. What's up with all the tabs? What do you want your output to look like? – Michael Stimson Feb 20 at 5:17
  • In my initial post, I have a report = [], and once I extract all these information, I am writing them onto the report using report += . I think your code simply just prints the results, but I need them to be written on the report first, and then I am printing the report. – Yoshi Feb 20 at 19:16
  • The question is about pixelType as a property of a raster, thus the answer is about getting the pixelType from a raster; I cannot understand what you're trying to do with your objects when you have them therefore I can't help integrate the value of the pixelType into your code - a question about that sort of thing needs to be posted on Stack Overflow as there would no longer be a GIS component. – Michael Stimson Feb 21 at 2:14

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.