I would like to create an expression using Arcade for symbology purposes.
I have one layer with six fields, each listing a distance that the feature is from that type of infrastructure. For example, one polygon's six fields tell me it's 100 feet to the nearest railroad, 150 feet to the nearest primary road, 175 feet to the nearest secondary road, 200 feet to the nearest local road, 225 feet to the nearest other type of road, and 250 feet to the nearest trail. I would like to identify the closest infrastructure type to a feature.
I have no problem getting the nearest value; I just use the Min() function. However, I can't get the field name that the value comes from. I'd like to be able to symbolize the features with 6 different colors depending on what infrastructure type is the nearest.
Any assistance is appreciated.
Solved! Go to Solution.
Here's one way to do it. The variable fieldList (and Expects) contains the names of the fields you are comparing .
Expects($feature, 'railroad', 'primary_road', 'secondary_road', 'local_road', 'other_road', 'trail') //field names
var fieldList = ['railroad', 'primary_road', 'secondary_road', 'local_road', 'other_road', 'trail']
var minimum = 99999999;
var minField;
for (var i in fieldList) {
if ($feature[fieldList[i]] < minimum) {
minimum = $feature[fieldList[i]]
minField = fieldList[i]
}
}
return minField
But the Min function will return a 0 if one of the features has a null value. You'd have to do this to ignore either null or 0 values.
**Updated to put the IsEmpty and zero checks at the front of the if statement
Expects($feature, 'railroad', 'primary_road', 'secondary_road', 'local_road', 'other_road', 'trail') //field names
var fieldList = ['railroad', 'primary_road', 'secondary_road', 'local_road', 'other_road', 'trail']
var minimum = 99999999;
var minField;
for (var i in fieldList) {
if (!IsEmpty($feature[fieldList[i]]) && $feature[fieldList[i]] != 0 && $feature[fieldList[i]] < minimum) {
minimum = $feature[fieldList[i]]
minField = fieldList[i]
}
}
return minField
Here's one way to do it. The variable fieldList (and Expects) contains the names of the fields you are comparing .
Expects($feature, 'railroad', 'primary_road', 'secondary_road', 'local_road', 'other_road', 'trail') //field names
var fieldList = ['railroad', 'primary_road', 'secondary_road', 'local_road', 'other_road', 'trail']
var minimum = 99999999;
var minField;
for (var i in fieldList) {
if ($feature[fieldList[i]] < minimum) {
minimum = $feature[fieldList[i]]
minField = fieldList[i]
}
}
return minField
Thanks for the help @KenBuja ! I thought I had it figured out after I modified it a bit as there are situations where the field value is null/0, so I just changed the variable minimum from 99999999 to use the Min() function to equal that variable.
Expects($feature, 'DistRail', 'DistPrim', 'DistSec', 'DistLoc', 'DistOth', 'DistBikeWalk') //field names
var fieldList = ['DistRail', 'DistPrim', 'DistSec', 'DistLoc', 'DistOth', 'DistBikeWalk']
var minimum = Min($feature.DistRail, $feature.DistPrim, $feature.DistSec, $feature.DistLoc, $feature.DistOth, $feature.DistBikeWalk);
var minField;
for (var i in fieldList) {
if ($feature[fieldList[i]] == minimum) {
minimum = $feature[fieldList[i]]
minField = fieldList[i]
}
}
return minField
Except that there are a lot of features that get put in the "Other" column that say they have no value. Any idea on that one?
But the Min function will return a 0 if one of the features has a null value. You'd have to do this to ignore either null or 0 values.
**Updated to put the IsEmpty and zero checks at the front of the if statement
Expects($feature, 'railroad', 'primary_road', 'secondary_road', 'local_road', 'other_road', 'trail') //field names
var fieldList = ['railroad', 'primary_road', 'secondary_road', 'local_road', 'other_road', 'trail']
var minimum = 99999999;
var minField;
for (var i in fieldList) {
if (!IsEmpty($feature[fieldList[i]]) && $feature[fieldList[i]] != 0 && $feature[fieldList[i]] < minimum) {
minimum = $feature[fieldList[i]]
minField = fieldList[i]
}
}
return minField
Thanks for the explanation! That worked!