Select to view content in your preferred language

Raster' object is not iterable

2910
2
Jump to solution
07-23-2014 08:34 AM
InceptionWoznicki
Deactivated User

Hi Guys,

 

I 'm trying to calculate the percentage of cell counts in new raster and the new raster is created from an old raster based on the Value > 14. In my code, 1st for loop is for original raster and keeping the value and count fields in a dictionary. Then, creating new raster using Value > 14. The 2nd for loop is for the new raster and keeping its value and counts of in a new dictionary and finally calculating the percentage.

 

However, I am getting an error on the 2nd for loop. The error says " 'Raster' object is not iterable". Anybody has any suggestion. Thank you for your help!

 

Code:

 

import arcpy, os

from arcpy import env

from arcpy.sa import *

 

#To overwrite output

arcpy.env.overwriteOutput = True

 

#Set environment settings

env.workspace = "C:/Subhasis/Test/Neshanic_Python"

 

outws="C:/Subhasis/Test/Neshanic_Python/extract"

 

#checkout ArcGIS spatial analyst extension license

arcpy.CheckOutExtension("Spatial")

 

# set local variable

inraster = ["01367620-r-r"]

for i in inraster:

    flds = ("VALUE", "COUNT") 

    dct = {row[0]:row[1] for row in arcpy.da.SearchCursor(i, flds)} 

    sumcnt = sum(dct.values())

    newraster = ExtractByAttributes(str(i), "VALUE>=14")

    for j in newraster:

        flds1 = ("VALUE", "COUNT")

        dct1 = {row[0]:row[1] for row in arcpy.da.SearchCursor(j, flds)}

        sumcnt1 = sum(dct1.values())

        percentage=(sumcnt/sumcnt1)

        print percentage

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi Subhasis,

I notice you have 5 separate threads on more or less the same topic:

Inception Woznicki‌ (just look at the content)

I have a few recommendations:

  • Concentrate on this topic: loop for value and counts in raster attribute is has the most answers.
  • If there are helpful comments mark them as helpful, it will help other user to find useful content faster
  • If a question was answered mark the thread as answered at the right comment, this also helps the community to find useful content, and avoids people to invest their time in your question, if your question was already solved.

Kind regards, Xander

View solution in original post

2 Replies
XanderBakker
Esri Esteemed Contributor

Hi Subhasis,

I notice you have 5 separate threads on more or less the same topic:

Inception Woznicki‌ (just look at the content)

I have a few recommendations:

  • Concentrate on this topic: loop for value and counts in raster attribute is has the most answers.
  • If there are helpful comments mark them as helpful, it will help other user to find useful content faster
  • If a question was answered mark the thread as answered at the right comment, this also helps the community to find useful content, and avoids people to invest their time in your question, if your question was already solved.

Kind regards, Xander

InceptionWoznicki
Deactivated User

Hi Xander,

This is a pretty good suggestion. I marked it as answer.

Regards,

Subhasis

0 Kudos