Arcade expression for formatting a number into Station+ (Plus) format in WebMap Popup
I am trying to the equivalent Arcade expression for the following VBScript:
Function FindLabel ( [Station] )
if (LEN(ROUND([Station],0))) = "7" then
FindLabel = LEFT(ROUND([Station],0),5) + "+" + RIGHT(ROUND([Station],0),2)
elseif (LEN(ROUND([Station],0))) = "6" then
FindLabel = LEFT(ROUND([Station],0),4) + "+" + RIGHT(ROUND([Station],0),2)
elseif (LEN(ROUND([Station],0))) = "5" then
FindLabel = LEFT(ROUND([Station],0),3) + "+" + RIGHT(ROUND([Station],0),2)
elseif (LEN(ROUND([Station],0))) = "4" then
FindLabel = LEFT(ROUND([Station],0),2) + "+" + RIGHT(ROUND([Station],0),2)
elseif (LEN(ROUND([Station],0))) = "3" then
FindLabel = LEFT(ROUND([Station],0),1) + "+" + RIGHT(ROUND([Station],0),2)
elseif (LEN(ROUND([Station],0))) = "2" then
FindLabel = LEFT(ROUND([Station],0),0) + "0+" + RIGHT(ROUND([Station],0),2)
elseif (LEN(ROUND([Station],0))) = "1" then
FindLabel = LEFT(ROUND([Station],0),0) + "0+0" + RIGHT(ROUND([Station],0),2)
else
FindLabel = ROUND([Station],0)
end if
End Function
It appears there is not an Arcade text/string LENGTH function to be able to return the number of characters. Is there another way to format a number with varying digits/characters to take the last 2 digits for the record and insert a "+" before those 2 digits.
Example
VALUE STATION
639249 6392+49
11715 117+15
234 2+34
The Count function returns how many characters are in a string.
Try this:
var station = Round($feature.Station, 0);
var len = Count(Text(station));
var start = "";
var middle = "";
var end = "";
if (len > 7) {
start = station;
middle = "";
end = "";
} else if (len > 1) {
start = Left(station, len-2);
middle = "+";
end = Right(station, 2);
} else if (len == 1) {
start = "";
middle = "0+";
end = Right(station, 1);
} else {
start = "";
middle = "0+0";
end = "";
}
var label = start + middle + end;
return label;
I used this one:
var station = $feature.BeginStation;
var len = Count(Text(station))
var end = Text(station, "00.00")
var labelStation =""
var leftVal = len - 5
var start = Left(station, leftVal)
if (start=="") {
start="0";
}
labelStation = start + '+' + end
return labelStation
Hi Steven Weaver ,
Are you sure your expression is correct? The OP provided some values and desired results and when I use your expression they don't match the desired result:
Value OP | Desired STATION OP | STATION with your expression |
---|---|---|
639249 | 6392+49 | 6+49.00 |
11715 | 117+15 | 0+15.00 |
234 | 2+34 | 0+34.00 |
The expression for the OP could probably be shortened to something like this:
var value = $feature.Station;
if (Count(Text(value))<3) {
return Left(Text(value, "000"), Count(Text(value, "000"))-2) + "+" + Right(Text(value, "000"), 2);
} else {
return Left(Text(value), Count(Text(value))-2) + "+" + Right(Text(value), 2);
}
Hi Xander,
I'm trying to add in an "else if" to your code here to return and empty value if the field is blank instead of "+". I added in:
} else if (value=='') {
return "";
When the field is empty, it is still coming back as "+" instead of "". I also tried value==' ' to see if there was a space being saved as a blank but it was still returning "+". Do you have an idea of how I could get this to work?
Thanks!
Tim
Hi twoodfield_DawoodGIS ,
You could probably do something like this:
var value = $feature.Station;
if (IsEmpty(value)) {
return "";
} else {
if (Count(Text(value))<3) {
return Left(Text(value, "000"), Count(Text(value, "000"))-2) + "+" + Right(Text(value, "000"), 2);
} else {
return Left(Text(value), Count(Text(value))-2) + "+" + Right(Text(value), 2);
}
}
Thanks Xander Bakker, This was helpful. I used this to pair with linear referencing to do some stationing in Pro.
https://pro.arcgis.com/en/pro-app/help/mapping/layer-properties/linear-referencing-hatch-symbols.htm
Hi jgrossman_VHB ,
Glad to hear that it was helpful!