Select to view content in your preferred language

Use Arcade to Calculate Field Based on Options from Another Field

1092
7
Jump to solution
09-01-2023 09:15 AM
Labels (3)
ZachBodenner
MVP Regular Contributor

Hello,

I've got an Arcade question. I have a feature set of points with an attribute "Type" that details the feature type. I have another attribute that is a numeric ID that corresponds to the text attribute Type. Example: Hydrant =1, Curb Stop = 2 etc. What's the best way to write an attribute rule in Arcade that will populate the numeric ID based on the value from the text type? My knee jerk is to do a super long If/Else exchange to the effect of:

var waterType = $feature.Type
if (waterType='Hydrant'){
return 1;
}
if(waterType='Curb Stop'){
return 2;
}
etc

However, there are like, 60 possible types which strikes me as a bit onerous to put into an if statement like that. Is there a better way?

0 Kudos
2 Solutions

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Here are two possible ways of doing it. It is a little onerous to build it for your 60 types either way, but a little easier than using if statements.

Using a Dictionary

var dict = {
  'Hydrant': 1,
  'Curb Stop': 2
};
var waterType = $feature.Type
return dict[waterType];

Using a When function

var waterType = $feature.Type;
when (waterType == 'Hydrant', 1,
      waterType == 'Curb Stop', 2,
      -9999 //default value for any other type
      )

View solution in original post

0 Kudos
ZachBodenner
MVP Regular Contributor

I did an abbreviated dictionary/where as a test:

Not working

var dict = {
  'Gate Valve': 87,
  'Curb Stop': 12
};
var waterType = $feature.Feat_Code
return dict[waterType];

 

Working:

var waterType = $feature.Feat_Code;
when (waterType == 'Gate Valve', 87,
      waterType == 'Curb Stop', 12,
      -9999 //default value for any other type
      )

 

View solution in original post

0 Kudos
7 Replies
KenBuja
MVP Esteemed Contributor

Here are two possible ways of doing it. It is a little onerous to build it for your 60 types either way, but a little easier than using if statements.

Using a Dictionary

var dict = {
  'Hydrant': 1,
  'Curb Stop': 2
};
var waterType = $feature.Type
return dict[waterType];

Using a When function

var waterType = $feature.Type;
when (waterType == 'Hydrant', 1,
      waterType == 'Curb Stop', 2,
      -9999 //default value for any other type
      )
0 Kudos
ZachBodenner
MVP Regular Contributor

Aha, yes without knowing it was thinking of a dictionary, which is a data type I'm pretty unfamiliar/uncomfortable with, so I want to try to use them more. However, I get an Arcade error that basically says the expression calculator couldn't find the attribute Type. Any thoughts on that?

Additionally, the second option, the When function, works perfectly!

0 Kudos
KenBuja
MVP Esteemed Contributor

I wasn't running into any errors on the tests I ran. What did you code look like?

0 Kudos
ZachBodenner
MVP Regular Contributor

I did an abbreviated dictionary/where as a test:

Not working

var dict = {
  'Gate Valve': 87,
  'Curb Stop': 12
};
var waterType = $feature.Feat_Code
return dict[waterType];

 

Working:

var waterType = $feature.Feat_Code;
when (waterType == 'Gate Valve', 87,
      waterType == 'Curb Stop', 12,
      -9999 //default value for any other type
      )

 

0 Kudos
KenBuja
MVP Esteemed Contributor

Does Feat_Code have a Domain? If so, you'd have to use this code

var dict = {
  'Gate Valve': 87,
  'Curb Stop': 12
};
return dict[DomainName($feature, 'Feat_Code')];

 

0 Kudos
ZachBodenner
MVP Regular Contributor

It does have a domain, and I had tried to use it, but I had entered it as part of the variable, like this:

var dict = {
  'Gate Valve': 87,
  'Curb Stop': 12
};
var waterType = DomainName($feature,Feat_Code)
return dict[waterType];

That's clarifying, thank you!

0 Kudos
KenBuja
MVP Esteemed Contributor

Your way would have worked, but you were missing the quotes around 'Feat_Code'

0 Kudos