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?
Solved! Go to Solution.
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
)
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
)
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
)
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!
I wasn't running into any errors on the tests I ran. What did you code look like?
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
)
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')];
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!
Your way would have worked, but you were missing the quotes around 'Feat_Code'