Select to view content in your preferred language

Arcade Label Expression Not Evaluating Properly

434
3
Jump to solution
03-15-2024 09:23 AM
MFazio
by
Regular Contributor

I have an expression to convert decimal degrees to display a cardinal direction:

// Convert degrees to cardinal direction

var wind = $feature.Wind_Dir

When(
  wind >= 337.5 && wind < 22.5, 'N',
  wind >= 22.5  && wind < 67.5, 'NE',
  wind >= 67.5  && wind < 112.5, 'E',
  wind >= 112.5 && wind < 157.5, 'SE',
  wind >= 157.5 && wind < 202.5, 'S',
  wind >= 202.5 && wind < 247.5, 'SW',
  wind >= 247.5 && wind < 292.5, 'W',
  wind >= 292.5 && wind < 337.5, 'NW',
  "N/A"
  )

 

When the expression is used in the popup, everything evaluates correctly EXCEPT when the value falls in the North category... I'm not sure what I'm overlooking here. Thanks!

1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Your problem in the first line of the When function is a wind can't be both (&&) greater than 337.5 and 22.5. You want to check if it's greater OR (||) less than those values. An easier way to write that expression is

var wind = $feature.Wind_Dir;

When(
  wind < 22.5, 'N',
  wind < 67.5, 'NE',
  wind < 112.5, 'E',
  wind < 157.5, 'SE',
  wind < 202.5, 'S',
  wind < 247.5, 'SW',
  wind < 292.5, 'W',
  wind < 337.5, 'NW',
  wind < 360, 'N',
  'NA'
);

If the function doesn't evaluate the first line as true, then it moves to the second line. For example, if the wind is 30, it fails the first check and moves on to the second. You don't need to check if is greater than 22.4 and less than 67.5 since it already verified that it's greater than 22.5.

View solution in original post

0 Kudos
3 Replies
CodyPatterson
Frequent Contributor

Hey @MFazio

It may be better to try and check 337.5 to 360 and 0 to 22.5 rather than what you have now, I can see that being an issue since it will not satisfy that initial condition with any number between as it is checking for a number greater than 337.5 and also being less than 22.5, here is this:

// Convert degrees to cardinal direction

var wind = $feature.Wind_Dir;

When(
  (wind >= 337.5 && wind < 360) || (wind >= 0 && wind < 22.5), 'N',
  wind >= 22.5 && wind < 67.5, 'NE',
  wind >= 67.5 && wind < 112.5, 'E',
  wind >= 112.5 && wind < 157.5, 'SE',
  wind >= 157.5 && wind < 202.5, 'S',
  wind >= 202.5 && wind < 247.5, 'SW',
  wind >= 247.5 && wind < 292.5, 'W',
  wind >= 292.5 && wind < 337.5, 'NW',
  'N/A'
)
KenBuja
MVP Esteemed Contributor

Your problem in the first line of the When function is a wind can't be both (&&) greater than 337.5 and 22.5. You want to check if it's greater OR (||) less than those values. An easier way to write that expression is

var wind = $feature.Wind_Dir;

When(
  wind < 22.5, 'N',
  wind < 67.5, 'NE',
  wind < 112.5, 'E',
  wind < 157.5, 'SE',
  wind < 202.5, 'S',
  wind < 247.5, 'SW',
  wind < 292.5, 'W',
  wind < 337.5, 'NW',
  wind < 360, 'N',
  'NA'
);

If the function doesn't evaluate the first line as true, then it moves to the second line. For example, if the wind is 30, it fails the first check and moves on to the second. You don't need to check if is greater than 22.4 and less than 67.5 since it already verified that it's greater than 22.5.

0 Kudos
MFazio
by
Regular Contributor

I've accepted this as the solution because it is the most simplistic expression to reach the solution I was aiming for. Not sure how I missed the fact that the number cant be both larger than 337.5 & lower than 22.5, but alas here we are. Thank you!

0 Kudos