Hello!
I am trying to replace the values for variables in an Arcade expression, however, it fails when I replace the numbers for variables.
Can you please, help me to fix the expression?
arcpy.CalculateField_management(Image_classified, "Dens",
"when($feature.gridcode == 1, 1.51, $feature.gridcode == 2,1.48, $feature.gridcode == 3, 5.03, 0)", "ARCADE")
I tried
a=1.51
b=1.48
c=5.03
arcpy.CalculateField_management(Image_classified, "Dens",
"when($feature.gridcode == 1, a, $feature.gridcode == 2,b, $feature.gridcode == 3, c, 0)", "ARCADE")
and gave me this error:
Traceback (most recent call last):
File "C:\Users\milop\Documents\ArcGIS\Projects\Image_processing_110621\model_summwhitin_230621.py", line 103, in <module>
"when($feature.gridcode == 1, a, $feature.gridcode == 2,b, $feature.gridcode == 3, c, 0)", "ARCADE")
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 5209, in CalculateField
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 5206, in CalculateField
retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block, field_type), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 511, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 002717: Invalid Arcade expression, Arcade error: Object not found a...
Solved! Go to Solution.
Putting code in a specially formatted box makes it much more readable. Expand the toolbar in the comment window, then click on "Inserd/Edit code sample":
Arcade has no way to know that you defined the variables in Python.
# If you want to define the variables in the Arcade code, do this:
expression = """
var a = 1.51
var b = 1.48
var c = 5.03
when($feature.gridcode == 1, a, $feature.gridcode == 2, b, $feature.gridcode == 3, c, 0)
"""
arcpy.CalculateField_management(Image_classified, "Dens", expression, "ARCADE")
# If you want to define the variable in Python, do this:
a = 1.51
b = 1.48
c = 5.03
expression = "when($feature.gridcode == 1, {}, $feature.gridcode == 2, {}, $feature.gridcode == 3, {}, 0)".format(a, b, c)
arcpy.CalculateField_management(Image_classified, "Dens", expression, "ARCADE")
Putting code in a specially formatted box makes it much more readable. Expand the toolbar in the comment window, then click on "Inserd/Edit code sample":
Arcade has no way to know that you defined the variables in Python.
# If you want to define the variables in the Arcade code, do this:
expression = """
var a = 1.51
var b = 1.48
var c = 5.03
when($feature.gridcode == 1, a, $feature.gridcode == 2, b, $feature.gridcode == 3, c, 0)
"""
arcpy.CalculateField_management(Image_classified, "Dens", expression, "ARCADE")
# If you want to define the variable in Python, do this:
a = 1.51
b = 1.48
c = 5.03
expression = "when($feature.gridcode == 1, {}, $feature.gridcode == 2, {}, $feature.gridcode == 3, {}, 0)".format(a, b, c)
arcpy.CalculateField_management(Image_classified, "Dens", expression, "ARCADE")
Thank you so much, Johannes, it worked!