How to label "_, Mount" as "Mount _" ?

1528
11
Jump to solution
05-08-2018 01:46 PM
CoryBoschman
New Contributor II

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?

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

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'

View solution in original post

11 Replies
ChrisDonohue__GISP
MVP Alum

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

CoryBoschman
New Contributor II

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

0 Kudos
DanPatterson_Retired
MVP Emeritus

Show your field calculator in a screen grab

  • add a text field to hold the calculations
  • set the field calculator to Python
  • put this in the expression   replacing 'fld' with your fieldname that contains the original data
  • ie  [[i, " ".join(i.split(", ")[::-1])][", " in i] for i in !TheOriginalFieldThatIsMessedUp! ]
  • note field names in python are enclosed in double exclamation marks ! .... !
CoryBoschman
New Contributor II

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

0 Kudos
DanPatterson_Retired
MVP Emeritus

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]

CoryBoschman
New Contributor II

Hi Dan,

That got it running, but it just returned the first letter of the string from the original field.

0 Kudos
DanPatterson_Retired
MVP Emeritus

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

0 Kudos
CoryBoschman
New Contributor II

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

0 Kudos
DanPatterson_Retired
MVP Emeritus

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'