[GIS] ArcPy merge datasets with slightly different fields

arcpyfield-mappingmerge

i have many (>15) shapefiles which have slightly differing fields.

s1: (Name, age, layer_1, ...)
s2: (Name, age, layer_1, layer_2, ...)
...
s_n: (Name, age, layer_1, layer_1_extra, ...)

I want to combine s1,s2 up to s_n into a new feature class/shapefile with attributes

s_merge: (Name, age, layer_1, layer_1_extra, layer_2, ...)

Field order is irrelevant. layer_1_extra should be set to ' ' or 0 depending on type, where features come from e.g. s1 ()which does not have layer_1_extra)).

Do I have to program this myself using search cursors (to get the field names), add field tool and insert cursors, or is there a way to coerce e.g. the merge tool to do what I want?

So far i tried to use the merge tool, but this fails with

arcgisscripting.ExecuteError: ERROR 001156: Failed on input OID 0, could not write value ' ' to output field SCHICHT_2

Field SCHICHT_2 is a number field, so of course arcgis can't write ' ' to it.

Code for setting the fieldMap is

for shapepath in ...:
  land_shape_paths.append(shapepath)
  fieldMappings.addTable(shapepath)
arcpy.Merge_management(land_shape_paths, land+'_full.shp', fieldMappings)

I had guessed ArcGIS was aware of field types and tried to write sensible "empty" values depending on field type. Apparently not, or my data is more botched than I think.

Any help (especially on a tool more suited to my needs than merge, or on the needed fieldMap magic) is appreciated!

Best Answer

I've tried to do this with code and had a similar situation as yours. Arc kept trying to merge a text field with a integer field because the fields were named the same, even though the data type was different. Here is an example:

Data Type Conflict

I had to bring all the shape files I wanted to merge into the merge tool and go through the field mappings to make sure that the fields were going into a proper place. I'm not sure how familiar you are with the Merge tool, but if you right click a field name in the field map box, you have some options to sort things out.

If you right click an input field, you can delete it from and output field. If you right click the whitespace to the right, you can add an output field (if the default fields aren't enough). If you right click the output field you can add an input field, or many input fields. Once you have it built, run the tool, then copy the result as a Python Snippet and you can insert it into a script, editing as necessary.

Related Question