Arcade Expression: show 2 variable ranges, hide 3rd

323
4
Jump to solution
10-30-2020 02:14 PM
JohnBranum
New Contributor III

Hello -

I can't see why this expression for a temperature layer won't work.  It's addressing a Field ("$feature.TmaxC") listing temperature attributes in 5-degree increments from -50 degrees to 30 degrees.

var Cold = ($feature.TmaxC < 10)

var Freeze = iif(Cold < 5, "Freezing", "Near Freezing")

return Freeze

My goal is to limit what's shown to freezing polygons surrounded by near-freezing polygons, and hide any temperatures greater than 10(or 9.9) degrees.  What I THINK it's saying is:

"Cold" is only everything in $feature.TmaxC that's below 10 degrees.

"Freeze" is everything in Cold that's less than 5 degrees (so, 0 on down to -50), and we'll call that "Freezing".

What's left (anywhere it's 5 degrees) is called "Near Freezing".

Now show me the two categories of Freeze.

The result in my map is that the entire planet is one category - freezing.

Thanks for any help!

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Regular Contributor

Proper noob on the Arcade language, 

but the When function might be appropriate:

Logical Functions | ArcGIS for Developers 

var temp = $feature.TmaxC
var ranking = When (temp < 5, “Freezing”,
temp >= 5 && temp < 10, “Near Freezing”,
“”)
return ranking
I can't test this myself at the moment, but it might give a good steer even if it needs finessing.

View solution in original post

4 Replies
DavidPike
MVP Regular Contributor

Proper noob on the Arcade language, 

but the When function might be appropriate:

Logical Functions | ArcGIS for Developers 

var temp = $feature.TmaxC
var ranking = When (temp < 5, “Freezing”,
temp >= 5 && temp < 10, “Near Freezing”,
“”)
return ranking
I can't test this myself at the moment, but it might give a good steer even if it needs finessing.

View solution in original post

JohnBranum
New Contributor III

Thank you, David!  New at this, so the simple idea of "when" never entered my mind.

Learning in baby steps.

KenBuja
MVP Honored Contributor

Your first line

var Cold = ($feature.TmaxC < 10)

returns a boolean value (true for all temperatures below 10). So when you test that in your second line

var Freeze = iif(Cold < 5, "Freezing", "Near Freezing")

it's actually checking whether true (which evaluates as 1) is less than 5, which is true.

This syntax will give you all your different categories:

return when($feature.TmaxC < 5, "Freezing",
$feature.TmaxC == 5, "Near Freezing",
$feature.TmaxC < 10, "Cold",
"Other"
);
JohnBranum
New Contributor III

Thanks, Ken.  At first I thought you were suggesting combining mine with yours, which seemed redundant, then I figured out your "when" block was all it really needed.  Since no "other" is required here (and I like having variables), I finally went with this:

var Temp = $feature.TmaxC
var Cold = when(Temp < 5, "Freezing", Temp < 10 , "Near Freezing","")
return Cold

0 Kudos