Managed expression. Arcade help for beginner.

863
9
Jump to solution
01-13-2022 11:21 AM
Marokapara
Occasional Contributor

Hi there,

I've made two expressions EF and Ha using Arcade that I'd like to use in a Map Viewer pop-up. I've created them in the Managed Expressions area.

If I want to use both of them to create an additional expression, how do I do this?

I'm still new to Arcade and don't have a background as a coder.

Hopefully this is an easy question for someone!

Marokapara_0-1642100688195.png

Marokapara_1-1642100728690.png

I tried $feature.EF/$feature.Ha. That doesn't work though because $feature are fields written into the table and Arcade is front end only?

EF's expression looks like this.

When($feature.ParameterTotal=='No vegetation Utility areas (tracks, buildings, yards)', 0,
$feature.ParameterTotal=='No vegetation Ponds/river/stream', 1,
$feature.ParameterTotal=='Non-woody vegetation Wetland/swamp/bog Grazed by animals Cultivated Fertilised', 2,

'No value');

Note: I will also need to swap out $feature.ParameterTotal to Parameter Total (Web arcade version). Currently I have it hardcoded while I try to figure out the correct expression syntax.

Ha's expression looks like this.

Round($feature.Shape__Area/10000,2)

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

You'd need to set your default output to another number instead of 'no value'. When your script goes to divide the text "no value" by Ha, it gives a NaN, since that doesn't make sense.

You might consider an If statement to check for that value and return something else.

var Total = Concatenate([$feature.Parameter1,$feature.P2,$feature.P3, $feature.P4],'  ')
var TotalTrim = Trim(Total)

var EF = When(
    TotalTrim=='No vegetation Utility areas (tracks, buildings, yards)', 0,
    TotalTrim=='No vegetation Ponds/river/stream', 1,
    TotalTrim=='Non-woody vegetation Wetland/swamp/bog Grazed by animals Cultivated Fertilised', -2,
    'No value'
);

var Ha = Round($feature.Shape__Area/10000,2);

If ( EF == 'No value' ){
    return 'No value'
} else {
    return EF / Ha
}

The rest of the expression looks good, though.

- Josh Carlson
Kendall County GIS

View solution in original post

9 Replies
jcarlson
MVP Esteemed Contributor

This is easy enough if you use variables in your expression. Just use the syntax "var <name> = <expression>", and you can re-reference that variable later. Combining your expressions would look like this:

var EF = When(
    $feature.ParameterTotal=='No vegetation Utility areas (tracks, buildings, yards)', 0,
    $feature.ParameterTotal=='No vegetation Ponds/river/stream', 1,
    $feature.ParameterTotal=='Non-woody vegetation Wetland/swamp/bog Grazed by animals Cultivated Fertilised', 2,
    'No value'
);

var Ha = Round($feature.Shape__Area/10000,2);

return EF / Ha

 

- Josh Carlson
Kendall County GIS
Marokapara
Occasional Contributor

Hi @jcarlson , thanks for this.

What about if I have a negative value in the When() statement? I'm getting NaN sometimes as output.

Also, I want to switch out $feature.ParameterTotal to a variable (TotalTrim) before the start of the entire expression.

Would it look like this?

var Total = Concatenate([$feature.Parameter1,$feature.P2,$feature.P3, $feature.P4],'  ')
var TotalTrim = Trim(Total)

var EF = When(
    TotalTrim=='No vegetation Utility areas (tracks, buildings, yards)', 0,
    TotalTrim=='No vegetation Ponds/river/stream', 1,
    TotalTrim=='Non-woody vegetation Wetland/swamp/bog Grazed by animals Cultivated Fertilised', -2,
    'No value'
);

var Ha = Round($feature.Shape__Area/10000,2);

return EF / Ha

 

 

 

0 Kudos
jcarlson
MVP Esteemed Contributor

You'd need to set your default output to another number instead of 'no value'. When your script goes to divide the text "no value" by Ha, it gives a NaN, since that doesn't make sense.

You might consider an If statement to check for that value and return something else.

var Total = Concatenate([$feature.Parameter1,$feature.P2,$feature.P3, $feature.P4],'  ')
var TotalTrim = Trim(Total)

var EF = When(
    TotalTrim=='No vegetation Utility areas (tracks, buildings, yards)', 0,
    TotalTrim=='No vegetation Ponds/river/stream', 1,
    TotalTrim=='Non-woody vegetation Wetland/swamp/bog Grazed by animals Cultivated Fertilised', -2,
    'No value'
);

var Ha = Round($feature.Shape__Area/10000,2);

If ( EF == 'No value' ){
    return 'No value'
} else {
    return EF / Ha
}

The rest of the expression looks good, though.

- Josh Carlson
Kendall County GIS
Marokapara
Occasional Contributor

Thanks for all your help @jcarlson ! Learned a lot today. I've realised some of the casing I had in the drop downs wasn't matching up with what I'd put in the expression, so I need to go back through the full thing. Things should work then.

0 Kudos
Marokapara
Occasional Contributor

@jcarlson I've gotten the expression to work... for the first two EF options. There are still 125 to go. I'm not sure what I've done wrong. The syntax looks the same '', spacing wise, letter casing and spelling. 

Here is a snip of 5 of them. I can get these first two to output correctly, but the following three come up as no value. (Thanks for that no value explanation higher up in the post.)

var Total = Concatenate([$feature.Parameter1, $feature.P2_NoVeg, $feature.P2_NonWoodVeg, $feature.P2_MixedWoodNonWood, $feature.P2_WoodVeg, $feature.P3_NonWoodVeg_Graze, $feature.P3_MixedWoodNonWood_Graze, $feature.P3_WoodVeg_Riparian, $feature.P4_NonWoodVeg_NotGrazed, $feature.P4_WoodVeg_Riparian_Native, $feature.P5_NonWoodVeg_Fert, $feature.P5_WoodVeg_NatGen, $feature.P6_NonWoodVeg_NotFert, $feature.P6_WoodVeg_NotIrrigated],'  ')
var TotalTrim = Trim(Total)

var EF = When(
    TotalTrim=='No vegetation  Utility areas (tracks, buildings, yards)', 0,
    TotalTrim=='No vegetation  Ponds/river/stream', 1,
    TotalTrim=='Non-woody vegetation  Wetland/swamp/bog  Grazed by animals  Cultivated  Fertilised', -2,
    TotalTrim=='Non-woody vegetation  Wetland/swamp/bog  Grazed by animals  Cultivated  Not fertilised', -3,
    'No value'
);

If ( EF == 'No value' ){
    return 'No value'
} else {
    return EF
}
0 Kudos
Marokapara
Occasional Contributor

@jcarlson I've gotten the expression to work... for the first two EF options. There are still 125 to go. I'm not sure what I've done wrong. The syntax looks the same '', spacing wise, letter casing and spelling. 

Here is a snip of 5 of them. I can get these first two to output correctly, but the following three come up as no value. (Thanks for that no value explanation higher up in the post.)

var Total = Concatenate([$feature.Parameter1, $feature.P2_NoVeg, $feature.P2_NonWoodVeg, $feature.P2_MixedWoodNonWood, $feature.P2_WoodVeg, $feature.P3_NonWoodVeg_Graze, $feature.P3_MixedWoodNonWood_Graze, $feature.P3_WoodVeg_Riparian, $feature.P4_NonWoodVeg_NotGrazed, $feature.P4_WoodVeg_Riparian_Native, $feature.P5_NonWoodVeg_Fert, $feature.P5_WoodVeg_NatGen, $feature.P6_NonWoodVeg_NotFert, $feature.P6_WoodVeg_NotIrrigated],'  ')
var TotalTrim = Trim(Total)

var EF = When(
    TotalTrim=='No vegetation  Utility areas (tracks, buildings, yards)', 0,
    TotalTrim=='No vegetation  Ponds/river/stream', 1,
    TotalTrim=='Non-woody vegetation  Wetland/swamp/bog  Grazed by animals  Cultivated  Fertilised', -2,
    TotalTrim=='Non-woody vegetation  Wetland/swamp/bog  Grazed by animals  Cultivated  Not fertilised', -3,
    'No value'
);

If ( EF == 'No value' ){
    return 'No value'
} else {
    return EF
}
0 Kudos
Marokapara
Occasional Contributor

Hi @jcarlson , I changed the casing and thought I'd be good to go. I'm able to get the first two (No vegetation types) to output correctly, but when I try any of the others, I get no value. They should be outputting the last number in each of the lines.

I'm not sure what I'm doing wrong. The '', casing, spacing and spelling all look correct.

There are over 100 possible outputs, but I can only get the first two to work. This is a snippet of five. Is there anything I've done wrong? Maybe syntax wise?

var Total = Concatenate([$feature.Parameter1, $feature.P2_NoVeg, $feature.P2_NonWoodVeg, $feature.P2_MixedWoodNonWood, $feature.P2_WoodVeg, $feature.P3_NonWoodVeg_Graze, $feature.P3_MixedWoodNonWood_Graze, $feature.P3_WoodVeg_Riparian, $feature.P4_NonWoodVeg_NotGrazed, $feature.P4_WoodVeg_Riparian_Native, $feature.P5_NonWoodVeg_Fert, $feature.P5_WoodVeg_NatGen, $feature.P6_NonWoodVeg_NotFert, $feature.P6_WoodVeg_NotIrrigated],' ')

var EF = When(
TotalTrim=='No vegetation Utility areas (tracks, buildings, yards)', 0,
TotalTrim=='No vegetation Ponds/river/stream', 1,
TotalTrim=='Non-woody vegetation Wetland/swamp/bog Grazed by animals Cultivated Fertilised', -2,
TotalTrim=='Non-woody vegetation Wetland/swamp/bog Grazed by animals Cultivated Not fertilised', -3,
'No value'
);

If ( EF == 'No value' ){
    return 'No value'
} else {
    return EF
}

 

0 Kudos
jcarlson
MVP Esteemed Contributor

It's hard to say what might be going wrong. Is it possible to try and tackle this further upstream? For instance, if you used coded domains on these fields, it would be possible to represent each combination as a series of single digits, rather than these increasingly long strings of text.

Also, it looks like there are values being sort of "doubled" in your attributes. For instance, you've got "non wood veg" and "wood veg", then other attributes like "non wood veg not grazed" and "wood veg not irrigated".

Would it not make more sense to simply have "wood veg", "grazed", and "irrigated" with yes/no values in them? I think you'll have an easier time working with your data that way.

- Josh Carlson
Kendall County GIS
0 Kudos
elpinguino
Occasional Contributor III

@jcarlson Thank you for your help so far. I'll try and put domain names, use the yes/no strategy or try and attack this from a different angle.

0 Kudos