I'm currently using the Statistics
geoprocessing tool (called Summary Statistics in the UI) from ArcObjects. This tool accepts two "complex" (for lack of a better term) arguments:
- Case fields parameter: a list of attributes that are used for grouping the statistics
- Statistics field parameter: a list of pairs of attribute and statistic type, indicating what to calculate for the given attribute
The only way I've been able to get it to work from ArcObjects so far is by passing strings in the format that you can see logged in the results window when you run it manually:
- List (case fields parameter): a semicolon separating each element (
"ELEMENT1;ELEMENT2"
) - List of pairs (statistics field parameter): the pair itself separated by a space, then each pair separated by a semicolon (
"PAIR1ITEM1 PAIR1ITEM2;PAIR2ITEM1 PAIR2ITEM2"
)
Like this:
var statsGP = new Statistics()
{
in_table = @"some\gdb\path\afeatureclass",
out_table = @"some\gdb\path\outputtable",
statistics_fields = "OBJECTID COUNT;SOME_OTHER_ATTR SUM",
case_field = "GROUP_ATTR1;GROUP_ATTR2"
};
var GP = new Geoprocessor();
GP.Execute((IGPProcess)statsGP, null);
The Python examples show that you just pass in a list
, but .NET arrays (a close equivalent) don't seem to work:
var statsGP = new Statistics()
{
in_table = @"some\gdb\path\afeatureclass",
out_table = @"some\gdb\path\outputtable",
statistics_fields = new string[][] {new string[]{"OBJECTID", "COUNT"}, new string[]{"SOME_OTHER_ATTR","SUM"}},
case_field = new string[]{"GROUP_ATTR1", "GROUP_ATTR2"}
};
I get the error System.Runtime.InteropServices.SafeArrayTypeMismatchException: Specified array was not of the expected type.
. I also tried ESRI's VarArray
as a wild stab in the dark after trying to figure out some example Java code:
static IVariantArray MakeVarArray(params object[] elements)
{
var result = new VarArray();
foreach (var e in elements)
{
result.Add(e);
}
return result;
}
...
var statsGP = new Statistics()
{
in_table = @"some\gdb\path\afeatureclass",
out_table = @"some\gdb\path\outputtable",
statistics_fields = MakeVarArray(MakeVarArray("OBJECTID", "COUNT"), MakeVarArray("OBJECTID", "COUNT")),
case_field = MakeVarArray("GROUP_ATTR1", "GROUP_ATTR2")
};
But it just gave a different error:
ERROR 000622: Failed to execute (Summary Statistics). Parameters are not valid.
ERROR 000623: Invalid value type for parameter statistics_fields.
Is there a cleaner way to pass complex arguments into a geoeprocessing tool in ArcObjects? I really don't want to litter my code with string building functionscode.