ArcObjects Library Reference  

FieldMapping

About the Geoprocessing field mapping Sample

[C#]

FieldMapping.cs

using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.ConversionTools;

namespace GPFieldMapping
{
    class FieldMapping
    {
        static void Main(string[] args)
        {
            // Initialize license
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
            IAoInitialize aoi = new AoInitializeClass();
            aoi.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);

            // Initialize the Geoprocessor
            Geoprocessor GP = new Geoprocessor();
            
            // Create the GPUtilites object
            IGPUtilities gputilities = new GPUtilitiesClass();

            // Create a DETable data element object 
            IDETable inputTableA = (IDETable)gputilities.MakeDataElement(@"C:\gp\citiblocks.gdb\census", null, null); 

            // Create an array of input tables
            IArray inputtables = new ArrayClass();
            inputtables.Add(inputTableA);

            // Initialize the GPFieldMapping
            IGPFieldMapping fieldmapping = new GPFieldMappingClass();
            fieldmapping.Initialize(inputtables, null);
            
            // Create a new output field
            IFieldEdit trackidfield = new FieldClass();
            trackidfield.Name_2 = "TRACTID";
            trackidfield.Type_2 = esriFieldType.esriFieldTypeString;
            trackidfield.Length_2 = 50;

            // Create a new FieldMap
            IGPFieldMap trackid = new GPFieldMapClass();
            trackid.OutputField = trackidfield;

            // Find field map "STFID" containing the input field "STFID". Add input field to the new field map.
            int fieldmap_index = fieldmapping.FindFieldMap("STFID");
            IGPFieldMap stfid_fieldmap = fieldmapping.GetFieldMap(fieldmap_index);
            int field_index = stfid_fieldmap.FindInputField(inputTableA, "STFID");
            IField inputField = stfid_fieldmap.GetField(field_index);
            trackid.AddInputField(inputTableA, inputField, 5, 10);

            // Add the new field map to the field mapping
            fieldmapping.AddFieldMap(trackid);

            // Execute Table to Table tool using the FieldMapping
            TableToTable tblTotbl = new TableToTable();
            tblTotbl.in_rows = inputTableA;
            tblTotbl.out_path = @"C:\gp\citiblocks.gdb"; 
            tblTotbl.out_name = "CensusVista";
            tblTotbl.field_mapping = fieldmapping;

            GP.Execute(tblTotbl, null);
        }
    }
}

[Visual Basic .NET]

FieldMapping.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Geoprocessing
Imports ESRI.ArcGIS.Geoprocessor
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.ConversionTools

Namespace GPFieldMapping
	Friend Class FieldMapping
		Shared Sub Main(ByVal args As String())
            ' Initialize license
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)

            Dim aoi As IAoInitialize = New AoInitializeClass()
            aoi.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo)

            ' Initialize the Geoprocessor
            Dim GP As ESRI.ArcGIS.Geoprocessor.Geoprocessor = New ESRI.ArcGIS.Geoprocessor.Geoprocessor()

			' Create the GPUtilites object
			Dim gputilities As IGPUtilities = New GPUtilitiesClass()

			' Create a DETable data element object 
            Dim inputTableA As IDETable = CType(gputilities.MakeDataElement("C:\gp\citiblocks.gdb\census", Nothing, Nothing), IDETable) 

			' Create an array of input tables
			Dim inputtables As IArray = New ArrayClass()
			inputtables.Add(inputTableA)

            ' Initialize the GPFieldMapping
			Dim fieldmapping As IGPFieldMapping = New GPFieldMappingClass()
			fieldmapping.Initialize(inputtables, Nothing)

			' Create a new output field
			Dim trackidfield As IFieldEdit = New FieldClass()
			trackidfield.Name_2 = "TRACTID"
			trackidfield.Type_2 = esriFieldType.esriFieldTypeString
			trackidfield.Length_2 = 50

			' Create a new FieldMap
			Dim trackid As IGPFieldMap = New GPFieldMapClass()
			trackid.OutputField = trackidfield

			' Find field map "STFID" containing the input field "STFID". Add input field to the new field map.
			Dim fieldmap_index As Integer = fieldmapping.FindFieldMap("STFID")
			Dim stfid_fieldmap As IGPFieldMap = fieldmapping.GetFieldMap(fieldmap_index)
			Dim field_index As Integer = stfid_fieldmap.FindInputField(inputTableA, "STFID")
			Dim inputField As IField = stfid_fieldmap.GetField(field_index)
			trackid.AddInputField(inputTableA, inputField, 5, 10)

			' Add the new field map to the field mapping
			fieldmapping.AddFieldMap(trackid)

			' Execute Table to Table tool using the FieldMapping
			Dim tblTotbl As TableToTable = New TableToTable()
			tblTotbl.in_rows = inputTableA
            tblTotbl.out_path = "C:\gp\citiblocks.gdb" 
            tblTotbl.out_name = "CensusVistaVB"
			tblTotbl.field_mapping = fieldmapping

			GP.Execute(tblTotbl, Nothing)
		End Sub
	End Class
End Namespace