Hi Nathan Bush ,
Sure, I can provide an example of how you can do this. However, it will be a long script in case you have to evaluate 100+ fields.
Have a look at the expression below.
- The function "CorrectVal" is to correct the high values 99 and 100 to 0 to force them to the end of the list
- The function "compareVal" is a helper function to sort the values
- The function "TranslateVal" will translate the values 0 a 5 to the corresponding percentages
- Lines 33 to 40 is where the values of the attributes are read and written to an array containing objects with properties "FieldName" and "FieldVal". See note on line 1 on reading the attribute value. For testing I am using fixed values, and they should be corrected using the function "CorrectVal".
- The code will take the array of field names and field values and sort it in descending order using the value. Note that you will have to correct the values and replace texts by a value (for instance values 99 and 100 should be '1 to force them to the end of the list)
- The "maxrank" variable will define how many species should be returned in the ranking
Function CorrectVal(val) {
if (val > 5) {
return 0;
} else {
return val;
}
}
Function compareVal(a,b){
if (a["FieldVal"]<b["FieldVal"])
return 1;
if (a["FieldVal"]<b["FieldVal"])
return -1;
return 0;
}
Function TranslateVal(val) {
var dct = {"1": "<1%", "2": "1-10%", "3": "10-25%",
"4": "25-60%", "5": ">60%"};
val = Text(val);
if (HasKey(dct, val)) {
return dct[val];
} else {
return "Zero/NOT TARGETED/UNKNOWN";
}
}
var fldsArray = [{"FieldName":"Alligator_Weed", "FieldVal": CorrectVal(1)},
{"FieldName":"American_Lotis", "FieldVal": CorrectVal(5)},
{"FieldName":"Amur_Honeysuc", "FieldVal": CorrectVal(4)},
{"FieldName":"Asiatic_Tearthumb", "FieldVal": CorrectVal(2)},
{"FieldName":"Autumn_Olive", "FieldVal": CorrectVal(4)},
{"FieldName":"Barberry", "FieldVal": CorrectVal(3)},
{"FieldName":"Another_specie1", "FieldVal": CorrectVal(100)},
{"FieldName":"Another_specie2", "FieldVal": CorrectVal(99)}];
var test = Sort(fldsArray, compareVal);
var maxrank = 5;
var rank = 0;
var cnt = 0;
var prevval = Null;
var result = "Ranking:";
for (var i in test) {
cnt += 1;
var specie = Replace(test[i]["FieldName"], "_", " ");
var val = test[i]["FieldVal"];
if (val != prevval) {
rank = cnt;
}
if (rank <= maxrank) {
result += TextFormatting.NewLine + " - " + rank + ": " + specie + " (" + TranslateVal(val) + ")";
} else {
return result;
}
prevval = val;
}
return result;
The result of this test expression is:
Ranking:
- 1: American Lotis (>60%)
- 2: Amur Honeysuc (25-60%)
- 2: Autumn Olive (25-60%)
- 4: Barberry (10-25%)
- 5: Asiatic Tearthumb (1-10%)
I hope this helps and if you need a hand you could share the data in a group and invite me ("xbakker.spx") to it to help create the expression.