ModelBuilder – Truncating Field Names to 10 Characters in ArcGIS Desktop

arcgis-desktoparcmaplimitationsmodelbuildershapefile

I have a number of different animal GPS location points, and a bunch of different habitat characteristics. I have built a simple model that uses the Near tool to calculate the distance from each point, create a new field using Add Field, naming it after the habitat characteristic shapefile name (using Parse Path and inline variable substitution), and then populates that added field with the NEAR_DIST values. It then continues on for all of my habitat characteristics.

I built the model a while ago and have since changed the names of the habitat characteristics (and there are a bunch of them) and unfortunately in the renaming them they are all now above the 10 character limit for a field name. What I was wondering is if there was any way to truncate the habitat characteristic shapefile title to 10 characters so that the add field tool doesn't error out.

Examples of habitat names are – 2001 Upland, 2001 Harvest, 2001 Deciduous

Essentially I need to remove the year and the space and they will all be under 10 characters.

The tool I created runs through but doesn't successfully "Add Field" because it is trying to name it after the "Near Feature" (the habitats whose names are too long) and so it just skips it.

enter image description here

Best Answer

You can do this using Calculate Value and a bit of Python:

  • Expression:

    make_fieldname(%Points%)
    

    (where Points is the value you actually want to use, I just guessed)

  • Code Block:

    def make_fieldname(input):
        return '_'.join(input.split(' ')[1:])[:10]
    

This will take your input value, split it at the spaces, strip off the first element (the year in your examples), join the rest with underscores, and finally truncate it at 10 characters to be safe.

Test results:

>>> make_fieldname("2001 Upland")
'Upland'
>>> make_fieldname("2001 Harvest")
'Harvest'
>>> make_fieldname("2001 Deciduous")
'Deciduous'
>>> make_fieldname("1999 Party Time")
'Party_Time'
>>> make_fieldname("2013 A Really Long Year")
'A_Really_L'