ArcGIS Maplex Python Labeling: If a value in a date field is null, how do I still show other values?

1266
20
03-09-2017 07:38 AM
MichaelHutchison
New Contributor

I'm trying to label point features with ArcGIS Maplex Engine, using [field1], [field2], [field3], [datefield].  My code is set up to label [field1] + '\n' + [datefield][-2:].  If [field1] is null, it'll label [field2] + '\n' + [field3] + '\n' + [datefield][-2:].  For whatever reason, my code doesn't work if a feature has a null value in [datefield].  How do I tell my code if the [datefield] is null, label [field1] AND if [datefield] and [field1] is null, label [field2] + '\n' + [field3]?  This is my code so far.

def FindLabel ( [field1], [field2], [field3], [datefield] 😞
  if str([field1]) == "None" :
    return [field2] + '\n' + [field3] + '\n' + [datefield][-2:]
  elif str([field1]) != "None" :
    return [field1] + '\n' + [datefield][-2:]

I tried adding this section but the engine doesn't seem to read it.

  elif [datefield] == "None" :

    return [field1]

  elif [datefield] == "None" and str([field1]) == "None" :

    return [field2] + '\n' + [field3]

My entire code is this.

def FindLabel ( [field1], [field2], [field3], [datefield] 😞
  if str([field1]) == "None" :
    return [field2] + '\n' + [field3] + '\n' + [datefield][-2:]
  elif str([field1]) != "None" :
    return [field1] + '\n' + [datefield][-2:]

  elif [datefield] == "None" :

    return [field1]

  elif [datefield] == "None" and str([field1]) == "None" :

    return [field2] + '\n' + [field3]

Thanks for the help.

0 Kudos
20 Replies
IanMurray
Frequent Contributor

Hi Michael.

I think your issue is that you are having it check again the string value "None" instead of a None object.  Try checking it against the key word None instead of the string.

http://stackoverflow.com/questions/3289601/null-object-in-python

I suggest using the if ... is None format in the link for your syntax.

Regards,

Ian

MichaelHutchison
New Contributor

I tried simplifying it for testing purposes and it did not work for me.

def FindLabel ( [FIELD1], [DATEFIELD] 😞

  if [DATEFIELD] is None :

    return [FIELD1]

Am I missing something?

0 Kudos
IanMurray
Frequent Contributor

Actually it doing exactly what you are telling it to do.  You said if it is a NULL label it a NULL which is nothing.

To test if its working I tried having it return something if it was Null:

def FindLabel ( [gasonsite] 😞
  if [gasonsite] is None:
    return "Value is Null"

I had dataset with some NULL values in a field and this expression successfully returned my string for the features that had NULL in that field.  When I used the expression you used, it was blank like your probably was.

IanMurray
Frequent Contributor

I thought about a good way to check and not add Null values to a label expression and here is a sample I used in ArcMap.

def FindLabel ( [gasonsite], [featuredlabel], [poweronsite]   😞
  fields =  ([gasonsite] , [featuredlabel] , [poweronsite])
  label = ""
  for field in fields:
    if field is None:
      continue
    else:
      label = label + field + "\n"
  return label

If the value is Null, it add nothing to the label, it is has a value, add it to the label to be returned and add a carriage return.

Please note all my fields I used were strings and if you are using other types you will need to use the str() to convert them prior to concatenating.

TedKowal
Regular Contributor II

I ran a quick test with two date fields Birthdate and Expiredate from one of my datasets .....

def FindLabel ( [BirthDate], [ExpireDate]  ):
  FindLabel = ""
  if [BirthDate] is None:
    FindLabel = "Null" + '\n'
  else:
    FindLabel = [BirthDate] + '\n'
  if [ExpireDate] is None:
    FindLabel = FindLabel + "Null2" + '\n'
  else:
    FindLabel = FindLabel + [ExpireDate] + '\n'
  return FindLabel + "Finished Dates"

0 Kudos
RandyBurton
MVP Regular Contributor

Perhaps a variation of this:

field1 = None # "Field1"
field2 = "Field2"
field3 = None # "Field3"
datefield = None # "2014-12-13"

if ( (field1 != None) and (datefield != None) ):
    print field1 + '\n' + datefield[-2:] 
elif (datefield != None):
    print '\n'.join(filter(None,(field2, field3, datefield[-2:])))
else:
    print '\n'.join(filter(None,(field2, field3)))
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

point raised before, but it is going to be more important in 3.x for future-proofing, because semantically there is only one None, so it is or isn't, hence nothing else can, or cannot be equal to it

RandyBurton
MVP Regular Contributor

What about using:

if field1:
   do something
DanPatterson_Retired
MVP Esteemed Contributor

True, but I don't like to equate None with 'empty'... again preference and semantics and how booleans are created.  I sometimes prefer to check for an object type (ie the NoneType) versus and object's state (ie an empty list)a

field1 = None
if field1:
    print("is not None")
else:
    print("is not necessarily None")
   
is not necessarily None

field1 = []
if field1:
    print("is not None")
else:
    print("is not necessarily None")
   
is not necessarily None‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

As in this case...

field1 = None
if isinstance(field1, (type(None), list, tuple)):
    print("could be None")
else:
    print("is not necessarily None")
   
could be None