5

I need to edit an existing python script to add an additional field to a field mapping function. This field mapping function does NOT use the arcpy field mapping object--it instead uses a structured string such as:

PRINMERCD \"Principal Meridian Code\" true true false 2 Text 0 0 , First,#,{0},PLSSID_1,2,3;
        PRINMER \"Principal Meridian Text\" true true false 40 Text 0 0 , First,#,{0},PrincipalMeridian_1,-1,-1;
        TWNSHPNO \"Township Number\" true true false 3 Text 0 0 , First,#,{0},TownshipNumber_1,-1,-1;
        TWNSHPFRAC \"Township Fraction\" true true false 1 Text 0 0 , First,#,{0},TownshipFraction_1,-1,-1;
        TWNSHPDIR \"Township Direction\" true true false 1 Text 0 0 , First,#,{0},TownshipDirection_1,-1,-1;

I'm able to deduce much of what I see in here, but I really need documentation. So far, I haven't been able to locate any. ESRI keeps pointing to the field mappings object, which isn't much help since it isn't feasible for me to refactor tons of code.

This article hints at my problem but then glazes over the string bit.

In summation, all I need is one document/resource explaining the syntax of this string so that I can manipulate it to suit my needs.

1
  • Much like the article says I would consider using FieldMappings object. The code would be more verbose but would be easier to understand.
    – Hornbydd
    Commented Aug 8, 2017 at 18:31

1 Answer 1

7

The string you are looking at is the string representation of the field mapping object which is used to map fields when exporting feature classes or when running operations that will combine attributes from multiple feature classes, for instance when running the Spatial Join geoprocessing tool when you may need to control the fields that will be taken into the output as well as their properties.

I am pretty sure you will not find any detailed docs on what each of strings part mean. I strongly suggest refactoring the Python code you have inherited though. Making changes using large strings will be a very painful process and you will benefit by introducing use of field maps and field mappings objects.

Anyways, here is the description (I have refactored a lot of legacy code myself, so I have done this deductive work a couple of years ago).

Here I load existing feature class fields into a new FieldMappings object and export it to the string. It's a great helper for debugging when you are not sure whether you have got your field mapping right.

>>> fms = arcpy.FieldMappings()
>>> fms.addTable(r'Database Connections\[email protected]\esrigdb.DBO.cities')
>>> fms.exportToString()

Each field is separated by the semicolon (;).

Let's take field POP1990 to look at:

;POP1990 "POP1990" true true false 4 Long 0 10 ,First,#,Database Connections\\[email protected]\\esrigdb.DBO.cities,POP1990,-1,-1;

Name = POP1990 
Alias = "POP1990" 
Editable = true 
Is Nullable = true 
Required = false 
Length = 4 
Type = Long 
scale = 0
precision = 10
Merge rule = First
joinDelimiter = # (meaning '', null string)
Data source = Database Connections\\[email protected]\\esrigdb.DBO.cities
Output field name = POP1990
-1
-1

I don't really know what -1 and -1 in the end stand for, but you happen to have 2,3 for the first field. If anyone knows what's that - please add a comment!

Hope this information will help you to refactor the code and use the field mappings object!

3
  • I was afraid of this answer, but nevertheless, thank you, Alex! I might be able to make use of the exportToString() method you've illustrated here for a quick/temporary fix. I too am baffled by the two numbers on the end. I'll repost here if I ever figure it out!
    – gwhitaker
    Commented Aug 8, 2017 at 19:10
  • No problem! Working with legacy code can be a curse, good luck! :0 Commented Aug 8, 2017 at 19:39
  • 5
    I've discovered since the original post that the two trailing numbers represent beginning/end characters of field value. Why -1, -1 is used to retrieve the full value, I don't know. However, if you insert other numbers, it works like an index slice. Just FYI in case anyone else has a similar question. Cheers!
    – gwhitaker
    Commented Dec 14, 2017 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.