Shows a messag box with Geoprocessing messages as formatted for the message box.
FieldMappings
var environment = Geoprocessing.MakeEnvironmentArray(overwriteoutput: true);
var prj = Project.Current;
var map = MapView.Active;
var defaultGDB = Project.Current.DefaultGeodatabasePath;
var featLayers = map.Map.Layers.OfType<FeatureLayer>();
var targetLayer = featLayers.ElementAt(0); // First layer in TOC
var joinLayer = featLayers.ElementAt(1); // Second layer in TOC
var outputFeatureClass = @"C:/temp/outputFC3.shp";
// Specify the field map in Spatial Join with target and join feature class/layers in the App
// Run Spatial Join manually - then Copy the fieldmap string from the result in Geoprocessing history and paste it for the fieldmap parameter.
// in this example of fieldmap, FireStations is the name of join layer
// FireStations layer has two numeric fileds (used in Fieldmap): TYPE and NUMBER - these two fields are used in the FiedlMap
//
var joinLayerName = joinLayer.Name;
var fieldMap = "TYPE 'TYPE' true true false 4 Long 0 0,Count,#,{joinLayerName},TYPE,-1,-1;NUMBER 'NUMBER' true true false 4 Long 0 0,Max,#,{joinLayerName},NUMBER,-1,-1";
var toolParameters = Geoprocessing.MakeValueArray(targetLayer, joinLayer, outputFeatureClass, "JOIN_ONE_TO_ONE", "KEEP_COMMON", fieldMap, "INTERSECT");
GPExecuteToolFlags executeFlags = GPExecuteToolFlags.AddOutputsToMap | GPExecuteToolFlags.GPThread | GPExecuteToolFlags.AddToHistory | GPExecuteToolFlags.RefreshProjectItems;
IGPResult gpResult = await Geoprocessing.ExecuteToolAsync("analysis.SpatialJoin", toolParameters, environment, null, null, executeFlags);
Geoprocessing.ShowMessageBox(gpResult.Messages, "GP Messages", gpResult.IsFailed ? GPMessageBoxStyle.Error : GPMessageBoxStyle.Default);
Setting environments, MakeEnvironmentArray
// get the syntax of the tool from Python window or from tool help page
string in_features = @"C:\data\data.gdb\HighwaysWeb84";
string out_features = @"C:\data\data.gdb\HighwaysUTM";
var param_values = Geoprocessing.MakeValueArray(in_features, out_features);
// crate the spatial reference object to pass as an argument to management.CopyFeatures tool
var sp_ref = await QueuedTask.Run(() =>
{
return SpatialReferenceBuilder.CreateSpatialReference(26911); // UTM 83 11N: 26911
});
// set output coordinate system environment
var environments = Geoprocessing.MakeEnvironmentArray(outputCoordinateSystem: sp_ref);
// set environments in the 3rd parameter
var gp_result = await Geoprocessing.ExecuteToolAsync("management.CopyFeatures", param_values, environments, null, null, GPExecuteToolFlags.AddOutputsToMap);
Geoprocessing.ShowMessageBox(gp_result.Messages, "Contents", GPMessageBoxStyle.Default, "Window Title");
//return gp_result;
Geoprocessing specialized MessageBox
var gp_result = await Geoprocessing.ExecuteToolAsync("management.GetCount", Geoprocessing.MakeValueArray(@"C:\data\f.gdb\hello"));
// this icon shows up left of content_header
string icon_src = @"C:\data\Icons\ModifyLink32.png";
Geoprocessing.ShowMessageBox(gp_result.Messages, "Content Header", GPMessageBoxStyle.Error, "Window Title", icon_src);
How to pass parameter with multiple or complex input values
var environments = Geoprocessing.MakeEnvironmentArray(overwriteoutput: true);
string toolName = "Snap_edit"; // or use edit.Snap
// Snap tool takes multiple inputs each of which has
// Three (3) parts: a feature class or layer, a string value and a distance
// Each part is separated by a semicolon - you can get example of sytax from the tool documentation page
var snapEnv = @"'C:/SnapProject/fgdb.gdb/line_1' END '2 Meters';'C:/SnapProject/fgdb.gdb/points_1' VERTEX '1 Meters';'C:/SnapProject/fgdb.gdb/otherline_1' END '20 Meters'";
var infc = @"C:/SnapProject/fgdb.gdb/poly_1";
var snapParams = Geoprocessing.MakeValueArray(infc, snapEnv);
GPExecuteToolFlags tokens = GPExecuteToolFlags.RefreshProjectItems | GPExecuteToolFlags.GPThread | GPExecuteToolFlags.AddToHistory;
IGPResult snapResult = await Geoprocessing.ExecuteToolAsync(toolName, parameters, environments, null, null, tokens);
//return gpResult;
How to use Geoprocessing public event
ArcGIS.Desktop.Core.Events.GPExecuteToolEvent.Subscribe(e =>
{
string id = e.ID; // Same as history ID
if (e.IsStarting == false) // Execute completed
_ = e.GPResult.ReturnValue;
System.Windows.MessageBox.Show("event triggered.");
});
await Geoprocessing.ExecuteToolAsync("management.GetCount", Geoprocessing.MakeValueArray(@"c:\shape_file.shp"));
Target Platforms: Windows 11, Windows 10
ArcGIS Pro version: 3 or higher.