Select to view content in your preferred language

Label help with condition formatting

265
2
03-11-2024 11:24 AM
JummaKhan
New Contributor III

Hi,

I am unable to figure out how I can format labels using ArcCade or Python. 

I have 2 fields (row, col). Row can be empty at some location. I want to show col only. How to setup a condition.

It displays "-col1" when row is empty. My syntax is 

$feature.ROW + '-' + $feature.COL

please help!

0 Kudos
2 Replies
Robert_LeClair
Esri Notable Contributor

I think this Arcade label expresion will work if the ROW field has 0 (zero) values in it.

if ($feature.ROW == 0){
return $feature.COL}
else
if ($feature.ROW > 0){
return $feature.NAME + "-" + $feature.ACRES}

MErikReedAugusta
Occasional Contributor III

Just to make sure I understand your question correctly, I'm going to answer referencing the sample data in the table below.  If the expected values are wrong, it might be best for you to share your old label script, so we can match the expected behavior.

TestFeatureRowColDesired Result
A575-7
B<Null>77
C5<Null>5
D<Null><Null>ERROR: NO VALUES

 

Arcade:

 

var row = $feature.ROW
var col = $feature.COL

// First, see if you have BOTH values
if (!IsEmpty(row) && !IsEmpty(col))
{
  return row + '-' + col
}
// If you don't have both, but you DO have a column, return it alone.
else if (!IsEmpty(col))
{
  return col
}
// This assumes TestFeature C is valid.  If you don't have both, but you DO have a row, return it alone.
else if (!IsEmpty(row))
{
  return row
}
// This assumes it's possible to have a case like TestFeature D where both values are missing
else
{
  return 'ERROR: NO VALUES'
}

 

Some notes:

  1. The above assumes that your EMPTY state is an actual <Null> value.  If it's a 0, then replace !IsEmpty(row) with row > 0, and do the same for col.
  2. Written the above way, if you get a single value, you won't be able to tell whether it's a row or a column.  This might not be a problem if an EMPTY column isn't possible.  If it's possible for either one to be empty, then I'd alter your label to indicate which number is which, for example:
    1. 'R.' + row + '-' + 'C.' + col in line 7 produces "R.5-C.7" for Test Feature A above
    2. Or you could just add the R. and C. to lines 12 & 17, for the single values.

 

Python 3:

 

def FindLabel ([ROW], [COL]):
  row = [ROW]
  col = [COL]

  # First, see if you have both values (Test Feature A)
  if row is not None and col is not None:
    return f'{row}-{col}'
  # If you don't have both, see if you have just a column (Test Feature B)
  elif col is not None:
    return f'{col}'
  # If you don't have both, see if you have just a row, assuming that's a valid option.  (Test Feature C)
  elif row is not None:
    return f'{row}'
  # If you've reached this point, you don't have either value. (Test Feature D)
  else:
    return 'ERROR: NO VALUES'

 

Some notes:

  1. As before, I'm assuming your EMPTY state is a proper <Null>.  If it's actually a 0, replace is not None with > 0.
  2. As before, it might be a good idea to add a prefix specifying row and column, if it's possible for either to be <Null>.
    1. Line 7 becomes return f'R.{row}-C.{col}'
    2. Line 10 becomes return f'R.{row}'
    3. Line 13 becomes return f'C.{col}'