ArcObjects Library Reference  

FieldMapping

About the Geoprocessing field mapping Sample

[C#]

FieldMapping.cs

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

namespace GPFieldMapping
{
    class FieldMapping
    {
        [STAThread]
        static void Main(string[] args)
        {

            if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine))
            {
                if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop))
                {
                    System.Windows.Forms.MessageBox.Show("This application could not load the correct version of ArcGIS.");
                    return;
                }
            }

            LicenseInitializer aoLicenseInitializer = new LicenseInitializer();
            if (!aoLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngine, esriLicenseProductCode.esriLicenseProductCodeBasic, esriLicenseProductCode.esriLicenseProductCodeStandard, esriLicenseProductCode.esriLicenseProductCodeAdvanced },
            new esriLicenseExtensionCode[] { esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork }))
            {
                System.Windows.Forms.MessageBox.Show("This application could not initialize with the correct ArcGIS license and will shutdown. LicenseMessage: " + aoLicenseInitializer.LicenseMessage());
                aoLicenseInitializer.ShutdownApplication();
                return;
            }

            // Run the geoprocessing code
            RunGPFieldMapping();

            aoLicenseInitializer.ShutdownApplication();
        }

        private static void RunGPFieldMapping()
        {
            // Initialize the Geoprocessor
            ESRI.ArcGIS.Geoprocessor.Geoprocessor GP = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
            GP.OverwriteOutput = true;

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

            // Create a DETable data element object 
            IDETable inputTableA = (IDETable)gputilities.MakeDataElement(@"C:\data\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:\data\citiblocks.gdb";
            tblTotbl.out_name = "census_out";
            tblTotbl.field_mapping = fieldmapping;

            object sev = null;
            try
            {
                GP.Execute(tblTotbl, null);
                System.Windows.Forms.MessageBox.Show(GP.GetMessages(ref sev));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                System.Windows.Forms.MessageBox.Show(GP.GetMessages(ref sev));
            }
        }
    }
}

[Visual Basic .NET]

FieldMapping.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
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

        <STAThread()> _
        Shared Sub Main(ByVal args As String())

            If (Not ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine)) Then
                If (Not ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop)) Then
                    System.Windows.Forms.MessageBox.Show("This application could not load the correct version of ArcGIS.")
                End If
            End If

            Dim aoLicenseInitializer As LicenseInitializer
            aoLicenseInitializer = New LicenseInitializer

            'ESRI License Initializer generated code.
            If (Not aoLicenseInitializer.InitializeApplication(New esriLicenseProductCode() {esriLicenseProductCode.esriLicenseProductCodeEngine, esriLicenseProductCode.esriLicenseProductCodeBasic, esriLicenseProductCode.esriLicenseProductCodeStandard, esriLicenseProductCode.esriLicenseProductCodeAdvanced}, _
            New esriLicenseExtensionCode() {esriLicenseExtensionCode.esriLicenseExtensionCodeNetwork})) Then
                System.Windows.Forms.MessageBox.Show("This application could not initialize with the correct ArcGIS license and will shutdown. LicenseMessage: " + aoLicenseInitializer.LicenseMessage())
                aoLicenseInitializer.ShutdownApplication()
                Return
            End If

            ' Run geoprocessing code
            RunGPFieldMappinig()

            ' Shutdown application
            aoLicenseInitializer.ShutdownApplication()

        End Sub



        Private Shared Sub RunGPFieldMappinig()
            ' Initialize the Geoprocessor
            Dim GP As ESRI.ArcGIS.Geoprocessor.Geoprocessor = New ESRI.ArcGIS.Geoprocessor.Geoprocessor()
            GP.OverwriteOutput = True

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

            ' Create a DETable data element object 
            Dim inputTableA As IDETable = CType(gputilities.MakeDataElement("C:\data\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:\data\citiblocks.gdb"
            tblTotbl.out_name = "census_out"
            tblTotbl.field_mapping = fieldmapping

            Dim sev As Object = Nothing
            Try
                GP.Execute(tblTotbl, Nothing)
                System.Windows.Forms.MessageBox.Show(GP.GetMessages(sev))
            Catch ex As Exception
                System.Windows.Forms.MessageBox.Show(GP.GetMessages(sev))
            End Try
        End Sub

    End Class

End Namespace