Hi,
Before I dive into more detail. I have an issue with a layer that somebody wrote this expression into this labeling expression. I have the Drawing Alert that tells me . I am trying to understand this and I am not sure how to fix it.
The layer I had was from an original layer came from the upper management and they did this for the whole country and I had to use it to clipped it to smaller area for our project. So when you clipped the layer , I believe some of the expression did not work in the label expression.
So here is the Expression from the Labeling section is what I have here from an original layer:
def FindLabel ( [TRAIL_NO], [ADMIN_ORG], [MANAGING_ORG], [TRAIL_NAME], [NATIONAL_TRAIL_DESIGNATION] ):
if [ADMIN_ORG].startswith('02'):
s='.'
if [ADMIN_ORG].startswith('0201'):
return [TRAIL_NO] [2:]
else:
return [TRAIL_NO].split(s,1)[0]
elif [ADMIN_ORG].startswith('0919') or [ADMIN_ORG] .startswith('0922') or [ADMIN_ORG] .startswith('0903')or [ADMIN_ORG] .startswith('0913'):
return [TRAIL_NO] [2:]
elif [ADMIN_ORG] .startswith('0909') or [ADMIN_ORG] .startswith('0907'):
return [TRAIL_NO] [2:].replace('-','')
elif [ADMIN_ORG] .startswith('0904'):
return [TRAIL_NO] [1:]
elif [ADMIN_ORG].startswith('0915'):
return [TRAIL_NO][4:]
elif [ADMIN_ORG].startswith('08'):
if [ADMIN_ORG].startswith('0805') or [ADMIN_ORG].startswith('0810'):
return [TRAIL_NO].replace('TRBS','').replace('TR','')[1:]
else:
return [TRAIL_NO].replace('TRBS','').replace('TR','')
elif [ADMIN_ORG].startswith('04'):
if [ADMIN_ORG].startswith('0419'):
return [TRAIL_NO] [1:]
elif [ADMIN_ORG].startswith('0402'):
return [TRAIL_NO][:3]
elif [MANAGING_ORG].startswith('0402'):
return [TRAIL_NO][:3]
elif [ADMIN_ORG].startswith('040702') and [TRAIL_NO].startswith('32000.'):
return [TRAIL_NO].replace('32000','').replace('.','')
elif [ADMIN_ORG].startswith('040702'):
return [TRAIL_NO][2:]
else:
return [TRAIL_NO][1:]
elif [ADMIN_ORG].startswith('0303'):
return [TRAIL_NO] [2:]
elif [MANAGING_ORG].startswith('031204'):
return [TRAIL_NO].replace('MT-','')
else:
return [TRAIL_NO].lstrip("0").replace('.', '-').replace('TER-','').replace('TRD','')
I have the clippped layer here and I have looked into the attributes and I can see some of the split it mention in the expression and yes I can see some of the numbers like this 157.2 or "Name of trail".456
The Problem is the error that tells me
Traceback (most recent call last):
File "<expression>", line 1, in <module>
File "<string>", line 7, in FindLabel
AttributeError: 'NoneType' object has no attribute 'split'
I am clueless here and I am not sure what the line 7 is and I am not sure where to find it.
Please excuse my grammar here. I am deaf and I try my best to understand how it works and it will help me to understand better down in the road should I came up the same thing in the future...
Thanks GIS friends !
I am in the VDI, Citrix, and ArcGIS Pro 3.3.4.
It's difficult to follow the logic of this code. When posting code (especially Python code, where indenting is important), please use the "Insert/Edit code sample" button.
Please fix the indenting
If a record contain a null value in [TRAIL_NO], then you'll get the split error. You have to check whether that attribute is not null before attempting to split it (line 7).
def FindLabel ( [TRAIL_NO], [ADMIN_ORG], [MANAGING_ORG], [TRAIL_NAME], [NATIONAL_TRAIL_DESIGNATION] ):
if [ADMIN_ORG].startswith('02'):
s='.'
if [ADMIN_ORG].startswith('0201'):
return [TRAIL_NO] [2:]
else:
if [TRAIL_NO]:
return [TRAIL_NO].split(s,1)[0]
else:
return "TRAIL_NO is null"
I fixed it and it worked; however on line 2 which I am not sure about but I can see the Admin_Org has null and I don't know how to include it in the code ?
def FindLabel ( [TRAIL_NO], [ADMIN_ORG], [MANAGING_ORG], [TRAIL_NAME],[NATIONAL_TRAIL_DESIGNATION]):
if [ADMIN_ORG].startswith('02'):
s='.'
if [ADMIN_ORG].startswith('0201'):
return [TRAIL_NO][2:]
else:
if [TRAIL_NO]:
return [TRAIL_NO]
else:
return "TRAIL_NO is null"
Traceback (most recent call last):
File "<expression>", line 1, in <module>
File "<string>", line 2, in FindLabel
AttributeError: 'NoneType' object has no attribute 'startswith'
And here is the screenshot for the field name here is
You'll have to do the same thing with any field that a text function (split, startswith, replace, etc). Since most of the conditional lines are testing [ADMIN_ORG], you should put that at the beginning, like this:
def FindLabel ( [TRAIL_NO], [ADMIN_ORG], [MANAGING_ORG], [TRAIL_NAME],[NATIONAL_TRAIL_DESIGNATION]):
if [ADMIN_ORG]:
if [ADMIN_ORG].startswith('02'):
//
You can put that on one line, like this:
elif [MANAGING_ORG] and [MANAGING_ORG].startswith('031204'):