# loop for value and counts in raster attribute

Question asked by sean.woznicki on Jul 18, 2014
Latest reply on Jul 25, 2014 by sean.woznicki

Hi Guys,

I'm a beginner to python scripting in GIS. I ‘m trying to make a loop for field “value” and “count” of a raster data.  The logic behind the code is:

-1st calculate the total number of counts in the original raster

- use the " 1st value" from value field of the raster attribute of original raster to extract a new raster using raster calculator

-calculate the total number of counts in the new raster

-If the total counts of new raster < 0.25*(total counts of original raster)

-Then got the threshold value(which was used to calculate new raster from the original raster)

-Else, use the next value from the old raster attribute to calculate new raster and the total “count” of new raster until the total counts of new raster is <  0.25* (total counts of original raster).

I am providing the code below.However, I am getting "invalid syntax error" at the end of the for loop in the code below where it says "next row". I would appreciate any comment/suggestion.

Thanks!

Code:

import arcpy

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"

# set local variable

inraster ="01367620-r-r"

# read attribute table of raster

rows = arcpy.SearchCursor(inraster,"","","Count","")

# sum of counts in raster

s1=[]

for row in rows:

s1.append(row.getValue("Count"))

A= sum(s1)

#checkout ArcGIS spatial analyst extension license

arcpy.CheckOutExtension("Spatial")

# read attribute table of raster

rows = arcpy.SearchCursor(inraster,"","","value","")

for row in rows:

inSQLClause = "VALUE = row[i]"

attExtract = ExtractByAttributes(inraster, inSQLClause)

attExtract.save("C:/Subhasis/Test/Neshanic_Python/STI-TH")

inraster="STI-TH"

rows = arcpy.SearchCursor(inraster,"","","Count","")

s2=[]

for row in rows:

s2.append(row.getValue("Count"))

B=sum(s2)

if B<=.25*A:

print 'got the Threshold point for STI row[i]'

else:

print'need to use different STI'

next row

..........................................................................................................................