I'm trying to write an expression to create custom symbology in a feature service. Currently I've made it this far:
var ftype = $feature.Feature_Type
var label = $feature.Label
var symbol = Replace(Replace(Replace(Replace(iif(ftype == "Hazard",iif(label == "Asbestos","Asbestos Hazard","Hazard"),ftype), ' ' , '' ), '(' , '' ), ')' , '' ), '-' , '' )
var d = Dictionary('Bore', 1, 'ExternalGate', 2, 'ExternalGateLocked', 3, 'GravelPit', 4, 'Hazard', 5, 'AsbestosHazard', 6, 'Helipad', 7, 'House', 8, 'InternalGate', 9, 'NoAccess', 10, 'OtherInfrastructure', 11, 'PhoneReception', 12, 'Shed', 13, 'WaterPoint', 14, 'WaterPointTank', 15)
var category = d.symbol
return category
The Replace functions are removing any spaces, brackets and dashes from the value returned from the $feature.Feature_Type value and work fine. The issue I'm having is I can't get the number value out of the dictionary using a variable as an input. I know this must be possible (or it would be pointless having dictionaries) but I can't figure out how to get the d.symbol (line 7) to use the value returned at line 4.
If I change line 7 to say:
var category = d.Bore
...then it all works. As soon as I change d.Bore back to d.symbol then it fails (even though the variable symbol is returning "Bore").
I also tried formatting d.symbol through text concatenation, but that didn't work either.
Solved! Go to Solution.
So there are two ways to retrieve (and set) values from a dictionary:
var d = {"A": 1}
// Dot notation
Console(d.A) // 1
d.B = 2
Console(d.B) // 2
// Bracket notation
Console(d["A"]) // 1
d["C"] = 3
Console(d["C"]) // 3
The dot notation is quicker to write and (imo) easier to read, but it does not resolve variables. In your case, it looks for the key "symbol", doesn't find it and raises an error.
To make this work, you need the bracket notation:
var category = d[symbol]
So there are two ways to retrieve (and set) values from a dictionary:
var d = {"A": 1}
// Dot notation
Console(d.A) // 1
d.B = 2
Console(d.B) // 2
// Bracket notation
Console(d["A"]) // 1
d["C"] = 3
Console(d["C"]) // 3
The dot notation is quicker to write and (imo) easier to read, but it does not resolve variables. In your case, it looks for the key "symbol", doesn't find it and raises an error.
To make this work, you need the bracket notation:
var category = d[symbol]
Awesome. Thank you. I'll give that a try tomorrow.
This worked a treat. Thanks. Final code below (changed the way the iif statements worked as well as they weren't returning the expected results - now uses a function to compare labels meaning it can find any Asbestos reference and not just in "Hazard" points).
var ftype = $feature.Feature_Type
var label = $feature.Label
function HazCat(txt,type) {
if (Find("Asbestos", txt, 0)>-1) {
return "AsbestosHazard";
} else {
return type;
}
}
var symbol = Replace(Replace(Replace(Replace(HazCat(label,ftype),' ' , '' ), '(' , '' ), ')' , '' ), '-' , '' );
var d = Dictionary('Bore', 1, 'ExternalGate', 2, 'ExternalGateLocked', 3, 'GravelPit', 4, 'Hazard', 5, 'AsbestosHazard', 6, 'Helipad', 7, 'House', 8, 'InternalGate', 9, 'NoAccess', 10, 'OtherInfrastructure', 11, 'PhoneReception', 12, 'Shed', 13, 'WaterPoint', 14, 'WaterPointTank', 15)
var category = d[symbol]
return category