Last modified June 3, 2010
Print all topics in : "Batch processing with geoprocessing scripts"
This topic was updated for 9.3.1.
A common geoprocessing task is to merge many datasets into a new or existing dataset to create a single dataset covering a larger area or a table containing a greater number of records. Often, the attributes, or fields, are the same for all the inputs that are used in a merge or append operation; sometimes, however, they do not match, and the relationships between fields of different names and types have to be mapped. The Merge tool in the Data Management toolbox facilitates this mapping of relationships so data is placed in the desired output fields with the correct values.
View Field Mappings object properties
The FieldMap object provides a field definition and a list of input fields from a set of tables or feature classes that provide its values. The FieldMappings object is a collection of FieldMap objects, and it is used as the parameter value for tools that perform field mapping, such as Merge. The easiest way to work with these objects is to first create a FieldMappings object, then initialize its FieldMap objects by adding the input feature classes or tables that are to be combined. Once all inputs are provided, the FieldMappings object will contain one FieldMap object, or output field, for each unique field name from all the inputs. This list can be modified by adding new fields, altering the properties and/or contents of an output field, or removing any unwanted output fields.
The properties of the FieldMap object include the start and end position of an input text value, so a new output value can be created using a slice of an input value. If a FieldMap object contains multiple input fields from the same table or feature class, each record's values are merged using the MergeRule property. This is a convenient way to join values such as a street name that is held in one field and a street type that is held in another—for example, Eureka and Street. The Delimiter property of a FieldMap is used if the MergeRule value of Join is specified. Any set of characters, such as a space, can be used as a delimiter. In the above example, this would create a value of Eureka Street.
View Field Map object properties
In the following example, a number of feature classes containing U.S. census data will be merged to form a new feature class. One of the input attributes found in all the inputs is a numeric field, STFID. This 15-digit value is a unique identifier for all census blocks for the United States. The value can be broken into four components. The first two digits provide the state code, the next three indicate the county, the following six identify the census tract, and the last four identify the census block. The value 360899912001006 represents the census block (1006) containing the State University of New York at Potsdam in upstate New York (36), within census tract 991200 of the county of St. Lawrence (089). The script sample will merge these feature classes together and also create two new fields, TRACTID and BLOCKID, because the input data only has the STFID attribute. To do this, the FieldMappings object is initialized using the AddTable method to enter each input. Then, the default FieldMappings object is modified by creating two new FieldMap objects, populating their properties, and adding them to the FieldMappings object.
# Import native arcgisscripting module # import arcgisscripting import sys # Create the geoprocessor object # gp = arcgisscripting.create(9.3) # Each of the input Feature classes has an STFID, which is the # combination of the Tract ID and Block ID for each block. # Separate these values out from this field into two new # fields, TRACTID and BLOCKID. # gp.Workspace = "C:/Data/CityBlocks.mdb" outfc = "C:/Data/CityBlocks.mdb/AllBlocks" # Create fieldmappings and two new fieldmaps. # fieldmappings = gp.CreateObject("FieldMappings") fldmap_TRACTID = gp.CreateObject("FieldMap") fldmap_BLOCKID = gp.CreateObject("FieldMap") # List all the feature classes in the workspace that start with # 'block' in their name and are of polygon feature type. # fcs = gp.ListFeatureClasses("block*", "Polygon") # Create a value table that will hold the input feature classes to Merge # vt = gp.CreateObject("ValueTable") for fc in fcs: # Adding a table is the fast way to load all the fields from the # input into fieldmaps held by the fieldmappings object. # fieldmappings.AddTable(fc) # In this example also create two fieldmaps by 'chopping up' # an input field. Feed the chopped field into the new fieldmaps. # fldmap_TRACTID.AddInputField(fc, "STFID") fldmap_BLOCKID.AddInputField(fc, "STFID") # Populate the input value table with feature classes # vt.AddRow(fc) # Set the starting and ending position of the fields going into the # TractID fieldmap. This is the location in the STFID field where the # TractID falls. # for x in range(0, fldmap_TRACTID.InputFieldCount): fldmap_TRACTID.SetStartTextPosition(x, 5) fldmap_TRACTID.SetEndTextPosition(x, 10) # Set the Name of the Field output from this field map. # fld_TRACTID = fldmap_TRACTID.OutputField fld_TRACTID.Name = "TRACTID" fldmap_TRACTID.OutputField = fld_TRACTID # Set the starting and ending position of the fields going into the # BlockID fieldmap. This is the location in the STFID field where the # blockID falls. # for x in range(0, fldmap_BLOCKID.InputFieldCount): fldmap_BLOCKID.SetStartTextPosition(x, 11) fldmap_BLOCKID.SetEndTextPosition(x, 16) # Set the Name of the Field output from this field map. # fld_BLOCKID = fldmap_BLOCKID.OutputField fld_BLOCKID.Name = "BLOCKID" fldmap_BLOCKID.OutputField = fld_BLOCKID # Add the custom fieldmaps into the fieldmappings object. # fieldmappings.AddFieldMap(fldmap_TRACTID) fieldmappings.AddFieldMap(fldmap_BLOCKID) # Run the Merge tool. # gp.Merge(vt, outfc, fieldmappings)
# Import native arcgisscripting module # import arcgisscripting import sys # Create the geoprocessor object # gp = arcgisscripting.create(9.3) outfc = "C:/data/CityData.mdb/AllBlocks" # Want to merge these two feature classes together. Have a field # that has the same content but the names are slightly different: # Blocks1 has TRACT2000 and Blocks2 TRACTCODE. Name the output # the same as Blocks1. # fc1 = "C:/data/CityData.mdb/Blocks1" fc2 = "C:/data/CityData.mdb/Blocks2" # Create a new fieldmappings and add the two input feature classes. # fieldmappings = gp.CreateObject("FieldMappings") fieldmappings.AddTable(fc1) fieldmappings.AddTable(fc2) # First get the TRACT2000 fieldmap. Then add the TRACTCODE field # from Blocks2 as an input field. Then replace the fieldmap within # the fieldmappings object. # fieldmap = fieldmappings.GetFieldMap(fieldmappings.FindFieldMapIndex("TRACT2000")) fieldmap.AddInputField(fc2, "TRACTCODE") fieldmappings.ReplaceFieldMap(fieldmappings.FindFieldMapIndex("TRACT2000"), fieldmap) # Remove the TRACTCODE fieldmap. # fieldmappings.RemoveFieldMap(fieldmappings.FindFieldMapIndex("TRACTCODE")) # Create a value table that will hold the inputs for Merge. # vt = gp.CreateObject("ValueTable") vt.AddRow(fc1) vt.AddRow(fc2) # Run the Merge tool. # gp.Merge(vt, outfc, fieldmappings)