0

I need to merge several shapefiles. All of them has the same attribute/field structure, but the width is not the same for the attributes with the same name. I tried to use Fieldmappings to set a wide enough destination field. But is not working. It doesn't create a long (50 characters) enough, so the error persistes. Error: Error 001156: Failed on input 26788, could not write value 'looong value here'. I know that the long strings are the problem. I've erased that polygon ID, and the error is gone. However, I need to have this process automated, solving it manually is not the solution. What is wrong with my code/Fieldmappings?

folder_combined_rasters        = r"C:\Projects\Soils\FECv3Out.gdb"
MapSeries   = []

# loading all feature classes into shapefiles memory list
arcpy.env.workspace = folder_combined_rasters
shapefiles = arcpy.ListFeatureClasses()
shapefiles.sort()

# Adding only the shapefiles that ends in 'Series' into MapSeries
for j,county in enumerate(shapefiles):
    if county[3:] == 'Series':
        MapSeries.append(county)

# defining fieldmaps
fldmps      = arcpy.FieldMappings()
fm_series   = arcpy.FieldMap()

# Adding only the needed fields to the fieldmap output
for field in arcpy.ListFields("SA_Series"):
    field.lenght = 60
    fm_series.addInputField(fm_series, field.name)

fldmps.addFieldMap(fm_series)

seriename = "Mine_Series"

arcpy.Merge_management(MapSeries,   seriename , fldmps)
  • 1
    Could it be the typo "field.lenght = 60"? – Ben S Nadler Aug 10 '16 at 3:18
  • Hi @BenSNadler, thanks for your comment. I don't see the typo. Besides, the the python interpreter would cacht it and complain. No syntax errors were reported from this code. – Delonix R. Aug 10 '16 at 11:57
  • The typo is right there in the code field.lenght = 60 This will not throw an error, just add a lenght property to the field object. Fix this and try again. Additionally, the input for the fm_series.addInputField is (TABLE,fieldname). you are passing in fm_series. check the example at the bottom of the help page pro.arcgis.com/en/pro-app/arcpy/classes/fieldmappings.htm – Ben S Nadler Aug 10 '16 at 17:19
1
#Create FieldMapping
fldmps= arcpy.FieldMappings()
#Load output fields from table, creating ouput schema
fldmps.addTable("SA_Series")
#Iterate over each of the fields in the fieldmappings generate from the table
for index,fldmap in enumerate(fldmps.fieldMappings, start=0):
    # Loop through rasters to add input fields (can omit the "SA_Series" since we already used it)
    for table in MapSeries:
        #find each field in the rasters with the same name and add to mapping 
        fldmap.addInputField(table, fldmap.outputField.name)
    #change output length to 60
    fld = fldmap.outputField
    fld.length = 60
    #commit changes into field mapping
    fldmap.outputField = fld
    #commit changes into fieldMappings
    fldmps.replaceFieldMap(index,fldmap)
#Now use field mappings in merge
  • Thanks for your help @BenSNadler, however I found a way to turnaround this: a) populate a memory list with all the shapefiles to be merged. b) create another mem list with the field.length values for each shpfile to be merged c) create a 3rd mem list just like the one created in a) (but this will work along the list created in b) d) Buble sort the b) list and as well the c) list e) replace values in a) list with the ones in c) list – Delonix R. Aug 10 '16 at 19:14
  • So now the mem list has on first position the longest lenght field, and the Merge tool will use that one to creat the atribute structure. Problem solved. However, your code give me a good lesson on Fieldmapping. Thanks! – Delonix R. Aug 10 '16 at 19:21

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.