for fc in listFCs: fcFieldList = arcpy.ListFields(fc) if (valueField in fcFieldList) & (classField in fcFieldList): rows = arcpy.UpdateCursor(fc) for row in rows: row.setValue(classField, calc(row.getValue(valueField))) # looks complex, but is just getting the value from valueField, passing it to CALC then writing the output to the classField rows.updateRow(row) try: del row except NameError: pass del rows
if len(classBreaks)- 1 != len(classValues): arcpy.AddError('Class Values and Class Breaks must be the same length.')
import arcpy def calc(x): # function that does the calculation '''calc(x) iterates through the classBreaks until it finds the bounding values of the input number, then returns the corresponding value from classVals ''' for i in xrange(len(classBreaks)-1): if classBreaks <= x < classBreaks[i+1]: return classValues elif x == classBreaks[-1]: # x equals the last value - wouldn't work above, as it is less than... return classValues[-1] return None # return something if the value isn't within the range, can be whatever you want - None will be converted to Arc NULL, but you could have 'No Class' or something like that, if you wanted... Geodatabase = arcpy.GetParameterAsText(0) # Location of Geodatabase classBreaks_str = arcpy.GetParameterAsText(1) # Break values (comma separated values) classValues_str = arcpy.GetParameterAsText(2) # Classes (comma separated values) to be returned, must correspond with the Break values valueField = arcpy.GetParameterAsText(3) # Field to be calculated on classField = arcpy.GetParameterAsText(4) # Field for results to go in classBreaks_str = '[' + classBreaks_str + ']' # add square brackets, so they look like lists classValues_str = '[' + classValues_str + ']' try: classBreaks = eval(classBreaks_str) except SyntaxError: arcpy.AddError('Class Breaks was entered incorrectly; values must each seperated by a comma.') except NameError: arcpy.AddError('Class Breaks was entered incorrectly; values must either be numbers or, if strings, within quotes.') try: classValues = eval(classValues_str) except SyntaxError: arcpy.AddError('Class Values was entered incorrectly; values must each seperated by a comma.') except NameError: arcpy.AddError('Class Values was entered incorrectly; values must either be numbers or, if strings, within quotes.') if len(classBreaks) != len(classValues): arcpy.AddError('Class Values and Class Breaks must be the same length.') arcpy.env.workspace = Geodatabase # set the workspace listFCs = arcpy.ListFeatureClasses('*') for fc in listFCs: rows = arcpy.UpdateCursor(fc) for row in rows: row.setValue(classField, calc(row.getValue(valueField))) # looks complex, but is just getting the value from valueField, passing it to CALC then writing the output to the classField rows.updateRow(row) try: del row except NameError: pass del rows
import arcpy import arcgisscripting gp = arcgisscripting.create() Feature_Class = gp.GetParameterAsText(0) # Location of Geodatabase classBreaks_str = gp.GetParameterAsText(1) # Break values (comma separated values) classValues_str = gp.GetParameterAsText(2) # Classes (comma separated values) to be returned, must correspond with the Break values valueField = gp.GetParameterAsText(3) # Field to be calculated on classField = gp.GetParameterAsText(4) # Field for results to go in classBreaks_str = '[' + classBreaks_str + ']' # add square brackets, so they look like lists classValues_str = '[' + classValues_str + ']' try: classBreaks = eval(classBreaks_str) except SyntaxError: arcpy.AddError('Class Breaks was entered incorrectly; values must each seperated by a comma.') except NameError: arcpy.AddError('Class Breaks was entered incorrectly; values must either be numbers or, if strings, within quotes.') try: classValues = eval(classValues_str) # I called this classVals in the previous post except SyntaxError: arcpy.AddError('Class Values was entered incorrectly; values must each seperated by a comma.') except NameError: arcpy.AddError('Class Values was entered incorrectly; values must either be numbers or, if strings, within quotes.') if len(classBreaks) != len(classValues): arcpy.AddError('Class Values and Class Breaks must be the same length.') def calc(x): # function that does the calculation '''calc(x) iterates through the classBreaks until it finds the bounding values of the input number, then returns the corresponding value from classVals ''' for i in xrange(len(classBreaks)-1): if classBreaks <= x < classBreaks[i+1]: return classValuess elif x == classBreaks[-1]: # x equals the last value - wouldn't work above, as it is less than... return classValues[-1] return None # return something if the value isn't within the range, can be whatever you want - None will be converted to Arc NULL, but you could have 'No Class' or something like that, if you wanted... listFCs = arcpy.ListFeatureClasses("*") for fc in listFCs: rows = arcpy.UpdateCursor(fc) for row in rows: row.classField = calc(row.valueField) rows.updateRow(row) try: del row except NameError: pass del rows
<type 'exceptions.RuntimeError'>: Row: Field valueField does not exist Failed to execute (Classify)
# presumably you are bringing in other variables, you only mentioned 0 then jumped to 3 - the parameter numbers must go up in order... classBreaks_str = gp.GetParameterAsText(3) # Break values (comma separated values) classValues_str = gp.GetParameterAsText(4) # Classes (comma separated values) to be returned, must correspond with the Break values valueField = gp.GetParameterAsText(5) # Field to be calculated on classField = gp.GetParameterAsText(6) # Field for results to go in
classBreaks_str = '[' + classBreaks_str + ']' # add square brackets, so they look like lists classValues_str = '[' + classValues_str + ']' try: classBreaks = eval(classBreaks_str) except SyntaxError: arcpy.AddError('Class Breaks was entered incorrectly; values must each seperated by a comma.') except NameError: arcpy.AddError('Class Breaks was entered incorrectly; values must either be numbers or, if strings, within quotes.') try: classValues = eval(classValues_str) # I called this classVals in the previous post except SyntaxError: arcpy.AddError('Class Values was entered incorrectly; values must each seperated by a comma.') except NameError: arcpy.AddError('Class Values was entered incorrectly; values must either be numbers or, if strings, within quotes.') if len(classBreaks) != len(classValues): arcpy.AddError('Class Values and Class Breaks must be the same length.') # rest of processing (previous posts) goes here...
del row, rows
try: del row except NameError: pass del rows
Pre-Logic Script Code: classBreaks = [0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 2] # defines where the values for the breaks are classVals = [1, 2, 3, 4, 5, 4, 3, 2, 1] # what should be returned - corresponds to the position of the value in classBreaks def calc(x): # function that does the calculation '''calc(x) iterates through the classBreaks until it finds the bounding values of the input number, then returns the corresponding value from classVals ''' for i in xrange(len(classBreaks)-1): if classBreaks <= x < classBreaks[i+1]: return classVals elif x == classBreaks[-1]: # x equals the last value - wouldn't work above, as it is less than... return classVals[-1] return None # return something if the value isn't within the range, can be whatever you want - None will be converted to Arc NULL, but you could have 'No Class' or something like that, if you wanted... ClassField= calc(!Value!)
import arcpy classBreaks = [0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 2] # defines where the values for the breaks are classVals = [1, 2, 3, 4, 5, 4, 3, 2, 1] # what should be returned - corresponds to the position of the value in classBreaks def calc(x): # function that does the calculation '''calc(x) iterates through the classBreaks until it finds the bounding values of the input number, then returns the corresponding value from classVals ''' for i in xrange(len(classBreaks)-1): if classBreaks <= x < classBreaks[i+1]: return classVals elif x == classBreaks[-1]: # x equals the last value - wouldn't work above, as it is less than... return classVals[-1] return None # return something if the value isn't within the range, can be whatever you want - None will be converted to Arc NULL, but you could have 'No Class' or something like that, if you wanted... arcpy.env.workspace = r"C:\temp\python\test.gdb" listFCs = arcpy.ListFeatureClasses("*") for fc in listFCs: rows = arcpy.UpdateCursor(fc) for row in rows: row.ClassField = calc(row.Value) rows.updateRow(row) del row, rows
import arcpy from arcpy import env env.workspace = r"C:\temp\python\test.gdb" lstFCs = arcpy.ListFeatureClasses("*") for fc in lstFCs: rows = arcpy.UpdateCursor(fc) try: for row in rows: if row.Value >= 0.0 and row.Value <= 0.2: row.Class = 1 if row.Value > 0.2 and row.Value <= 0.4: row.Class = 2 if row.Value > 0.4 and row.Value <= 0.6: row.Class = 3 if row.Value > 0.6 and row.Value <= 0.8: row.Class = 4 if row.Value > 0.8 and row.Value <= 1.0: row.Class = 5 if row.Value > 1.0 and row.Value <= 1.2: row.Class = 4 if row.Value > 1.2 and row.Value <= 1.4: row.Class = 3 if row.Value > 1.4 and row.Value <= 1.6: row.Class = 2 if row.Value > 1.6 and row.Value <= 2.0: row.Class = 1 rows.updateRow(row) except RuntimeError: pass del row, rows
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.