1

I have a collection of hundreds of shape files and a python dictionary mapping the input field to the output field. I'm building my fieldmappings dynamically using this dictionary since it already exists and is in a configuration file, editable by the user if fields change.

How should I handle the SHAPE field? I want the geometry copied over during any merge operations. The mapping in my config file, "SHAPE@": "SHAPE@" (input field : output field) throws an error for the shape field but all other mappings work.

Update 3: Code included...

field_names = GetFieldNamesInFeatureClass(file)

fieldmappings = arcpy.FieldMappings()

for input_name in field_names:

    output_name = fmFromCfg[input_name]

    fieldmap = arcpy.FieldMap()
    fieldmap.addInputField(file, input_name)

    fld = fieldmap.outputField
    fld.type = "DOUBLE"
    fld.name = output_name
    fld.aliasname = "Output Field Alias"
    fld.isNullable = True

    fieldmap.outputField = fld
    fieldmappings.addFieldMap(fieldmap)

The configuration file is JSON. It is read into fmFromCfg. Here is a simplified version with relevant section:

{
  "FieldMapping": {
    "FID": "OWNER",
    "SHAPE": "SHAPE"
  }
}

Update 2: Changed the field mapping to "SHAPE": "SHAPE" but I'm still getting an error, RuntimeError: FieldMap: Error in adding input field to field map.

Update 1: When I change the mapping to "SHAPE": "SHAPE@", a match is made but I get RuntimeError: FieldMap: Error in adding input field to field map because the @ at the end of the output field.

  • 1
    Field name probably isn't named SHAPE@. SHAPE@ has special meeting in several arcpy.da functions, but doesn't apply elsewhere. – DWynne Jun 5 '15 at 16:58
  • My first comment is applicable to both input and output fields. – DWynne Jun 5 '15 at 17:04
  • Changing the mapping to "SHAPE": "SHAPE" still results in an error, RuntimeError: FieldMap: Error in adding input field to field map – DenaliHardtail Jun 5 '15 at 17:04
  • You might need to show your code. Not clear how you're using your config file or setting up the fieldmappings. – DWynne Jun 5 '15 at 17:05
  • In the arcpy.Describe section, there is a shapeFieldName property – user681 Jun 7 '15 at 1:38
2

You don't actually need to express the geometry field ('SHAPE') in the fieldmappings. It will come along for the ride. (If you try to do something equivalent using a tool dialog that uses a fieldmappings, you'll notice the geometry field isn't added to the control.)

So, given that this field is in your config file, just add an if to exclude it.

for input_name in field_names:

    output_name = fmFromCfg[input_name]

    if output_name != 'SHAPE':
        fieldmap = arcpy.FieldMap()
        fieldmap.addInputField(file, input_name)

        fld = fieldmap.outputField
        fld.type = "DOUBLE"
        fld.name = output_name
        fld.aliasname = "Output Field Alias"
        fld.isNullable = True

        fieldmap.outputField = fld
        fieldmappings.addFieldMap(fieldmap)

Update:

Or better yet, filter out the geometry field when you create your list of field names. I can't see into your GetFieldNamesInFeatureClass function, but something equivalent to:

field_names = [f.name for f in arcpy.ListFields(file) if f.type != 'Geometry']
  • It would be more generically applicable to data sources where the geometry column is not always "SHAPE" if you get the type of the field and test on geometry type. – Vince Jun 5 '15 at 17:26
  • Agreed. Best spot to do that might be in @DenaliHardtail's GetFieldNamesInFeatureClass function. – DWynne Jun 5 '15 at 17:28
  • Just as a catch all, it may be best to filter all non-required fields: fields = [f.name for f in arcpy.ListFields(fc) if not f.required]. This will also exclude fields like OBJECTID. – crmackey Jun 5 '15 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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