Select to view content in your preferred language

Summarize Attributes using ExecuteToolAsync

1458
9
Jump to solution
07-19-2022 04:36 PM
PeteVitt
Occasional Contributor III

Hi  - I'm not having success running the Summarize Attributes tool in the GeoAnalytics Desktop Tools using ExecuteToolAsync in ArcPro sdk 3.0. Below is my code.  Am I approaching this the correct way? I'm not sure if the tool name is correct, and I don't think the parameters I'm passing are correct either 

//summarize landscape classifications
var progSumDlg = new ProgressDialog("Running Geoprocessing Tool", "Cancel", 100, true);
progSumDlg.Show();
var sum_tool_name = "geoanalytics.Summarize";
//layer to summarize
var summarizeLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().First(l => l.Name.Equals("test_clip"));
//field to classifiy and field to get stats for
Table table = summarizeLayer.GetTable();
TableDefinition tableDefinition = table.GetDefinition();
Field classField = tableDefinition.GetFields().FirstOrDefault(x => x.Name.Contains("Class_name"));
Field statField = tableDefinition.GetFields().FirstOrDefault(x => x.Name.Contains("Ft2"));
//output table
var out_table = System.IO.Path.Combine(gdb, "test_clip_summary");
var sum_params = await QueuedTask.Run(() =>
{
return Geoprocessing.MakeValueArray(new object[] { summarizeLayer, out_table, classField, statField});
});

await Geoprocessing.ExecuteToolAsync(sum_tool_name, sum_params,
null, new CancelableProgressorSource(progSumDlg).Progressor, GPExecuteToolFlags.Default);

 

thanks

 

Pete

 

0 Kudos
1 Solution

Accepted Solutions
CharlesMacleod
Esri Regular Contributor

First thing I notice is that the toolname is "SummarizeAttributes" rather than "Summarize" as u have it so that needs to be changed.

Second thing I noticed was that using the toolname "geoanalytics.SummarizeAttributes" I actually got a _server_ flavor of the tool and not the _desktop_ flavor (not that I knew there were two as I am not personally familiar with the Geoanalytics toolbox).

I wonder if you have a similar issue and are invoking the Geoanalytics server tool and not the desktop tool?

GP_tool3.png

Well, eventually, I ended up with a toolname of "gapro.SummarizeAttributes" which seemed to do the trick and invoke the desktop toolbox. You can try my code below. (I am using the "Interacting with Maps.gdb\Crimes" dataset from the pro community sample data fyi).

internal class GPTestButton : Button {

  protected async override void OnClick()
  {
     var sum_tool_name = "gapro.SummarizeAttributes";
     //var sum_tool_name = "geoanalytics.SummarizeAttributes";
     var extent = MapView.Active.Extent;
     var summarizeLayer = MapView.Active.Map.GetLayersAsFlattenedList()
            .OfType<FeatureLayer>().FirstOrDefault(l => l.Name == "Crimes");
     if (summarizeLayer == null) return;

     var gdb = Project.Current.DefaultGeodatabasePath;
     //output table
     var out_table = System.IO.Path.Combine(gdb, "crimes_summary");

     var fields = new List<string>() { "Major_Offense_Type", "Police_Precinct" };
     var summary_fields = new List<string>() { "Major_Offense_Type COUNT" };

     var sum_params = await QueuedTask.Run(() =>
     {
        return Geoprocessing.MakeValueArray(
           new object[] { summarizeLayer, out_table, fields, summary_fields });
     });

     await Geoprocessing.ExecuteToolAsync(sum_tool_name, sum_params,
               null, CancelableProgressor.None,
               GPExecuteToolFlags.InheritGPOptions);
     //Or open the dialog
     //Geoprocessing.OpenToolDialog(sum_tool_name, sum_params);
  }
}

 

Gives:

GP_tool2.png

This is the tool dialog invoked via Geoprocessing.OpenToolDialog

GP_tool1.png

 

Note: To resolve the toolname I had to look in the GP tool cache to see what it (GP) was looking for. You can find that here: C:\Users\your_username\AppData\Local\ESRI\Local Caches. Despite their rather intimidating appearance they are just text files. Just drag and drop one of them into notepad. I searched for "SummarizeAttributes" and came up with two hits: "geoanalytics.SummarizeAttributes" and "gapro.SummarizeAttributes". As I had already tried "geoanalytics.SummarizeAttributes", I went with the other one.

GP_tool4.png

 

GP_tool5.png

 

View solution in original post

9 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

I have wrote about geoprocessing sdk help for .Net developers few times but unsuccessful.

Geoprocessing tool name should be constructed like  "SummarizeAttributes_geoanalytics".

MakeValueArray method doesn't need MCT. It should look like this:

List<string> fields = new List<string() { "Class_name", "Ft2" };
var sum_params = Geoprocessing.MakeValueArray(summarizeLayer, out_table, $"{string.Join(",", fields)}");

 

0 Kudos
KenBuja
MVP Esteemed Contributor

You can use the syntax "geoanalytics.SummarizeAttributes" for tool names.

0 Kudos
KenBuja
MVP Esteemed Contributor

You will need to put a couple of lines inside of a QueuedTask.Run

Table table = summarizeLayer.GetTable();
TableDefinition tableDefinition = table.GetDefinition();
Field classField = tableDefinition.GetFields().FirstOrDefault(x => x.Name.Contains("Class_name"));
Field statField = tableDefinition.GetFields().FirstOrDefault(x => x.Name.Contains("Ft2"));

 

0 Kudos
PeteVitt
Occasional Contributor III

I changed the tool name to "geoanalytics.SummarizeAttributes"

I tried two ways to call the tool as suggested:

1) with field objects inside QueuedTask

var sum_params = await QueuedTask.Run(() =>
{
Table table = summarizeLayer.GetTable();
 TableDefinition tableDefinition = table.GetDefinition();
Field classField = tableDefinition.GetFields().FirstOrDefault(x => x.Name.Contains("Class_name"));
 Field statField = tableDefinition.GetFields().FirstOrDefault(x => x.Name.Contains("Ft2"));
 var out_table = System.IO.Path.Combine(gdb, "test_clip_summary");
 return Geoprocessing.MakeValueArray(new object[] { summarizeLayer, out_table, classField, statField });
});

await Geoprocessing.ExecuteToolAsync(sum_tool_name, sum_params,
null, new CancelableProgressorSource(progSumDlg).Progressor, GPExecuteToolFlags.Default);

2) with fields as strings:

List<string> fields = new List<string>() { "Class_name", "Ft2" };
var out_table = System.IO.Path.Combine(gdb, "test_clip_summary"); ;
var sum_params = Geoprocessing.MakeValueArray(summarizeLayer, out_table, $"{string.Join(",", fields)}");

await Geoprocessing.ExecuteToolAsync(sum_tool_name, sum_params,
null, new CancelableProgressorSource(progSumDlg).Progressor, GPExecuteToolFlags.Default);

in both cases the summary table is not being generated.

I've based my call to 'Summarize Attributes' on the Parameters section of  the Tool Reference located here:

https://pro.arcgis.com/en/pro-app/2.8/tool-reference/big-data-analytics/summarize-attributes.htm

I haven't so far been able to find a snippet or example of the tool being called by the sdk

 

Pete

0 Kudos
KenBuja
MVP Esteemed Contributor

The first thing you'll want to do is to check in the ArcGIS Pro Geoprocessing History to see if the tool ran properly and what errors it encountered if it didn't. You should also be able to tell whether your inputs are what you expected.

0 Kudos
PeteVitt
Occasional Contributor III

I'm not seeing any of the sdk initiated geoprocessing calls (even successful ones) in the geoprocessing history

0 Kudos
KenBuja
MVP Esteemed Contributor

You can use the flag GPExecuteToolFlags.AddToHistory. This is an example of one of my calls

IGPResult result = await Geoprocessing.ExecuteToolAsync("management.Dissolve", Geoprocessing.MakeValueArray(arguments.ToArray()), null, null, null, GPExecuteToolFlags.AddToHistory);

0 Kudos
CharlesMacleod
Esri Regular Contributor

First thing I notice is that the toolname is "SummarizeAttributes" rather than "Summarize" as u have it so that needs to be changed.

Second thing I noticed was that using the toolname "geoanalytics.SummarizeAttributes" I actually got a _server_ flavor of the tool and not the _desktop_ flavor (not that I knew there were two as I am not personally familiar with the Geoanalytics toolbox).

I wonder if you have a similar issue and are invoking the Geoanalytics server tool and not the desktop tool?

GP_tool3.png

Well, eventually, I ended up with a toolname of "gapro.SummarizeAttributes" which seemed to do the trick and invoke the desktop toolbox. You can try my code below. (I am using the "Interacting with Maps.gdb\Crimes" dataset from the pro community sample data fyi).

internal class GPTestButton : Button {

  protected async override void OnClick()
  {
     var sum_tool_name = "gapro.SummarizeAttributes";
     //var sum_tool_name = "geoanalytics.SummarizeAttributes";
     var extent = MapView.Active.Extent;
     var summarizeLayer = MapView.Active.Map.GetLayersAsFlattenedList()
            .OfType<FeatureLayer>().FirstOrDefault(l => l.Name == "Crimes");
     if (summarizeLayer == null) return;

     var gdb = Project.Current.DefaultGeodatabasePath;
     //output table
     var out_table = System.IO.Path.Combine(gdb, "crimes_summary");

     var fields = new List<string>() { "Major_Offense_Type", "Police_Precinct" };
     var summary_fields = new List<string>() { "Major_Offense_Type COUNT" };

     var sum_params = await QueuedTask.Run(() =>
     {
        return Geoprocessing.MakeValueArray(
           new object[] { summarizeLayer, out_table, fields, summary_fields });
     });

     await Geoprocessing.ExecuteToolAsync(sum_tool_name, sum_params,
               null, CancelableProgressor.None,
               GPExecuteToolFlags.InheritGPOptions);
     //Or open the dialog
     //Geoprocessing.OpenToolDialog(sum_tool_name, sum_params);
  }
}

 

Gives:

GP_tool2.png

This is the tool dialog invoked via Geoprocessing.OpenToolDialog

GP_tool1.png

 

Note: To resolve the toolname I had to look in the GP tool cache to see what it (GP) was looking for. You can find that here: C:\Users\your_username\AppData\Local\ESRI\Local Caches. Despite their rather intimidating appearance they are just text files. Just drag and drop one of them into notepad. I searched for "SummarizeAttributes" and came up with two hits: "geoanalytics.SummarizeAttributes" and "gapro.SummarizeAttributes". As I had already tried "geoanalytics.SummarizeAttributes", I went with the other one.

GP_tool4.png

 

GP_tool5.png

 

PeteVitt
Occasional Contributor III

Thanks Charles that worked

 

Pete

0 Kudos