How to use Field Mappings in Geoprocessing with C# and Geoprocessing.MakeValueArray

553
6
08-03-2021 06:33 AM
ole1986
New Contributor III

A general question is how to define the Parameters when using FieldMapping arguments in Geoprocessing tools

For example the "FeatureClassToFeatureClass" allows (according to Feature Class To Feature Class (Conversion)—ArcGIS Pro | Documentation) the fields to be renamed, deleted, etc...

But there is no example avaialble for C#

 

0 Kudos
6 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

This sample code worked for me:

protected override async void OnClick()
	{
	  try
	  {
        var environment = Geoprocessing.MakeEnvironmentArray(overwriteoutput: true);

        var defaultGDB = Project.Current.DefaultGeodatabasePath;
        
        var srcLayer = "TestPoints";
        var srcGdb = @"C:\Data\FeatureTest\FeatureTest.gdb";

        var outputFeatureClass = @"TestTestPoints";

        var fields = new string[] { 
          @"TheDate ""TheDate"" true true false 8 Date 0 0,First,#,TestPoints,TheDate,-1,-1",
          @"CodePoint ""CodePoint"" true true false 4 Long 0 0,First,#,TestPoints,CodePoint,-1,-1",
          @"TheString ""TheString"" true true false 255 Text 0 0,First,#,TestPoints,TheString,0,255",
          @"TheInteger ""TheInteger"" true true false 4 Long 0 0,First,#,TestPoints,TheInteger,-1,-1",
          @"TheDouble ""TheDouble"" true true false 8 Double 0 0,First,#,TestPoints,TheDouble,-1,-1",
          @"NewString ""NewString"" true true false 255 Text 0 0,First,#,TestPoints,TheString,0,255'" };
        var fieldMap = string.Join (";", fields);

        var toolParameters = Geoprocessing.MakeValueArray(srcLayer, srcGdb, outputFeatureClass, null, fieldMap, null);

        GPExecuteToolFlags executeFlags = GPExecuteToolFlags.AddOutputsToMap | GPExecuteToolFlags.GPThread | GPExecuteToolFlags.AddToHistory | GPExecuteToolFlags.RefreshProjectItems;

        IGPResult gpResult = await Geoprocessing.ExecuteToolAsync("FeatureClassToFeatureClass", toolParameters, environment, null, null, executeFlags);

        Geoprocessing.ShowMessageBox(gpResult.Messages, "GP Messages", gpResult.IsFailed ? GPMessageBoxStyle.Error : GPMessageBoxStyle.Default);
      }
	  catch (Exception ex)
	  {
        MessageBox.Show($@"Error: {ex}");
	  }
	}
0 Kudos
ole1986
New Contributor III

This example works for me too (so far).

So i am still in the expression that the Tool "Append" seem to have a bug using field mappings.
See my bug report Bug in Geoprocessing.ExecuteToolAsync("Append") wh... - Esri Community

0 Kudos
mcamp1
by
New Contributor II

Where is the sample code from? Thanks!

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I made up the sample that you see above to show that one can add different types of columns to a file geodatabase.   If I remember correctly the bug had to do with adding columns to a shapefile.

0 Kudos
mcamp1
by
New Contributor II

Hi Wolf,

Thank you for the quick response. I am trying to create a field mapping to pass as a parameter to the ‘Table to Table’ geoprocessing tool, and your example code is the best resource I have found for defining the Field Mappings so far. I have a couple more questions if don’t mind.

Does the c# SDK have a field mapping object like python? If so, can you point me towards any documentation or code examples?

If not, is there any documentation for the string field mapping? This link was the closest I could find.

Thanks!

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Unfortunately, the SDK does not have field mapping objects like python.  This and better Pro SDK for .Net suited documentation and sample usage code has been (and still is) on the Pro SDK team's wish list for a while.  If I remember correctly the example above (references in the 'bug' report) worked fine for geodatabases (file, etc.) but not for shapefiles.   Are you trying read/write to a shapefile?   I noticed that the 'bug' thread you mentioned above was never solved, I will reach out to Nobbir and find out where we left off.   However, if you are just trying to copy / append data to a geodatabase the sample snippet should work.   We also now have DDL support in the API: ProConcepts DDL · Esri/arcgis-pro-sdk Wiki (github.com) which will allow you to inspect the schema of existing tables/feature classes.  However, DDL support is also limited to Geodatabases only.   

Let me know what you try to do.  You can also reach out to me at: wkaiser@esri.com if you want to keep samples/data confidential.

0 Kudos