Select to view content in your preferred language

arcpy FieldMappings input table

390
1
07-22-2023 06:49 AM
TylerT
by
Frequent Contributor

Arcpy FieldMappings documentation includes the path to the input table which can be seen in the following string representation as c:\\data\\fgdb.gdb\\south_america:

 

fieldmappings_string = 'REGION "Region" true true false 21 Text -1 -1,First,#,c:\\data\\fgdb.gdb\\north_america,REGION,-1,-1,c:\\data\\fgdb.gdb\\south_america,REGION2,-1,-1'

 

In the case of arcpy ExportFeatures both in_features and field_mappings will include path to input table.

arcpy.conversion.ExportFeatures(in_features, out_features, 
{where_clause}, {use_field_alias_as_name}, {field_mapping}, {sort_field})

 This makes me wonder a few things.  What if the input tables between 'in_features' and 'field_mapping' did not match?  If they must match, then why doesn't fieldmappings inherit the input path from in_features?  

Thank you,

Tyler

0 Kudos
1 Reply
by Anonymous User
Not applicable

The input needs to match or you will get an error that says it cant find the field in the featureclass specified. Think about it though, if you try to use a field from another featureclass, how would it know which value goes to what feature? What if there are more or less values from that other featureclass?

It does inherit the path if you do not set the optional field mapping parameter. If you want to drop/rename fields, you'll need to use the field_mapping parameter.

----------

Right below that example, which is for a merge (so there are two inputs), there is a key that could shed some light into this. Lets split this string up to match the help.

The first nine values in the string define an output field and are space delimited.

'REGION "Region" true true false 21 Text -1 -1,
  • The name of the output field. -> REGION
  • The alias of the output field. -> Region
  • Whether the output field is editable (true or false). -> true
  • Whether the output field supports nulls (true or false). -> true
  • Whether the output field is required (true or false). -> false
  • The length of the output field (text fields only). -> 21
  • The field type of the output field. -> Text
  • The precision of the output field (float and double fields only). -> -1
  • The scale of the output field (float and double fields only). -> -1

The remaining values define the field map characteristics and are comma delimited.

First,#,c:\\data\\fgdb.gdb\\north_america,REGION,-1,-1,c:\\data\\fgdb.gdb\\south_america,REGION2,-1,-1'

 

  • The field map merge rule. -> First
  • The concatenator to join values. -> Skipped
  • The path to the input table(s). -> c:\\data\\fgdb.gdb\\north_america, c:\\data\\fgdb.gdb\\south_america, ...
  • The field name from the input table(s).-> REGION, REGION2, ...
  • The start position of an input text value(s). -> -1, -1, ...
  • The end position of an input text value(s). -> -1, -1, ...

Any number of input fields can be mapped to the output field, not only two, as implied in the example. Include the merge rule and concatenator once, and include the dataset path, field name, and start position and end position for each input field.

You can see how confusing the string representation can get and I have yet to come into a need to code it that way. I added the emphasis because its a bit hidden in the docs but is key here. Hope this helps.

0 Kudos