Select to view content in your preferred language

How to insert a variable in an Arcade expression

1836
2
Jump to solution
07-01-2021 01:14 PM
mlopezr95984
Occasional Contributor

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...

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

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":

JohannesLindner_0-1625208536742.png

 

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")

 


Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

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":

JohannesLindner_0-1625208536742.png

 

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")

 


Have a great day!
Johannes
mlopezr95984
Occasional Contributor

Thank you so much, Johannes, it worked!

0 Kudos