I am trying to make labels for all the named peaks across British Columbia. The name string in the attribute table has many entries like "Rad, Mount" or "Rad Peak, The", and I'd like to display them on the map as "Mount Rad" or "The Rad Peak". There are about 13,000 features in the table with only maybe a quarter that need to be changed.
Any suggestions on the best way to accomplish this?
Solved! Go to Solution.
So much for making things simpler, but since you seem to be adept with 'defs'
#---- This is your python code block for the field calculator
def fix(fld):
if ", " in fld:
val = " ".join(fld.split(", ")[::-1])
else:
val = fld
return val
fld = "Try, Final"
fix(fld) #---- This would be your expression
'Final Try'
One way to do this is with a script. If a consistent symbol or pattern can be identified for cases where a change needs to be made, then that would be employed in the script to detect what records need to be modified and then do the modification. For example, if all the records that need to be modified are situations where the information after a comma needs to instead be placed in front of a comma.
As to specific code, that will depend on the script language of choice. Are you familair with Python or another script language used in GIS?
As one means to do this, check out the label expression examples in this link. They are not exactly what you need, but will give a feel for how to employ the script in ArcGIS when labelling.
Building label expressions—Help | ArcGIS for Desktop
Chris Donohue, GISP
Hi Chris,
I've looked through the names and it appears that everything that is after a comma should be moved to the front of the string. I have pretty limited experience with python so writing an expression that will accomplish this is beyond me. I've been playing around with what Dan pasted below, but can't seem to get it to work.
Thanks,
Cory
Show your field calculator in a screen grab
Hi Dan,
I'm getting an error that says the value type is incompatible with the field type (see screen shot). The field I'm calculating (Label) is a text field with the default length of 50 characters.
Any tips?
Thanks,
Cory
In the middle of the dialog... see the Type.. you have it set to Number change it to String (aka Text)
And throw a [0] at the end of the line since you need the first and it isn't reading the full column as in my example
[[i, " ".join(i.split(", ")[::-1])][", " in i] for i in ['hello.'] ][0]
Hi Dan,
That got it running, but it just returned the first letter of the string from the original field.
OK... dump the ... [0] ...I thought it might be returning a list... but if it isn't or try ... [:] ... instead
I really should use the field calculator more
Returns the same error as before with or with out the [:]. If not a field calculation would inserting it in to a label expression be possible? see screen shot
So much for making things simpler, but since you seem to be adept with 'defs'
#---- This is your python code block for the field calculator
def fix(fld):
if ", " in fld:
val = " ".join(fld.split(", ")[::-1])
else:
val = fld
return val
fld = "Try, Final"
fix(fld) #---- This would be your expression
'Final Try'