Select to view content in your preferred language

Executing management.CalculateFields GP tool from Pro SDK (3.4)

370
5
Jump to solution
3 weeks ago
azlotin
Emerging Contributor

I would like to batch calculate two attributes in selected features in a feature layer (the layer data source is a feature service). My function is below. IGPResult returns "Enumeration yielded no results" error message and the fields are not being updated. Is there anything wrong with the code, specifically the list of arguments being passed to the tool? Thanks!

----------------------------------------------------------------------------------------------------------------------------------------------

private async Task UpdateLayerWithCalculateFields(FeatureLayer featureLayer, List<long> oids,
string field1value, string field2value)
{
QueryFilter filter = new QueryFilter() { ObjectIDs = oids };
featureLayer.Select(filter);

List<object> fieldArgs = new List<object> { new List<object> { "Field1", field1value }, new List<object> { "Field2", field2value } };
List<object> argsCalculate = new List<object> { featureLayer.Name, "PYTHON3", fieldArgs };
IGPResult result = await Geoprocessing.ExecuteToolAsync("management.CalculateFields", Geoprocessing.MakeValueArray(argsCalculate.ToArray()), null, null, null, GPExecuteToolFlags.None);
}

 

0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Next issue is your fieldArgs parameter. It must be like below (this is for numeric fields):

string fieldArgsStr = "'field1' 1; 'field2' 2"; //fieldname1 space fieldvalue1 separator fieldname2 space fieldvalue2
var argsCalculate = Geoprocessing.MakeValueArray(featureLayer, "PYTHON3", fieldArgsStr);

I don't know how fields list parameter must look with text fields.

View solution in original post

0 Kudos
5 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

Try to change last two lines from your function to lines below:

            var argsCalculate = Geoprocessing.MakeValueArray(featureLayer, "PYTHON3", fieldArgs);
            IGPResult result = await Geoprocessing.ExecuteToolAsync("management.CalculateFields", argsCalculate, null, null, null, GPExecuteToolFlags.None);

Yours referencing to feature layer is invalid. Your function will calculate only TEXT fields (depending on your input parameters). Change them from string to object if you want to work with all types of fields.

0 Kudos
azlotin
Emerging Contributor

Thanks! Here is my updated code:

List<object> fieldArgs = new List<object> { new List<object> { "Field1", field1value }, new List<object> { "Field2", field2value } };
var argsCalculate = Geoprocessing.MakeValueArray(featureLayer, "PYTHON3", fieldArgs);
IGPResult result = await Geoprocessing.ExecuteToolAsync("management.CalculateFields", argsCalculate, null, null, null, GPExecuteToolFlags.None);

I am now seeing this error: "ERROR 000499: table is not editable". 

Single-field calculation does work however:

List<object> argsCalculate = new List<object> { featureLayer, "Field1", $"'{field1value}'", "PYTHON3" };
IGPResult result = await Geoprocessing.ExecuteToolAsync("management.CalculateField", Geoprocessing.MakeValueArray(argsCalculate.ToArray()), null, null, null, GPExecuteToolFlags.None);

 

0 Kudos
GKmieliauskas
Esri Regular Contributor

Next issue is your fieldArgs parameter. It must be like below (this is for numeric fields):

string fieldArgsStr = "'field1' 1; 'field2' 2"; //fieldname1 space fieldvalue1 separator fieldname2 space fieldvalue2
var argsCalculate = Geoprocessing.MakeValueArray(featureLayer, "PYTHON3", fieldArgsStr);

I don't know how fields list parameter must look with text fields.

0 Kudos
azlotin
Emerging Contributor

This code works with text fields:

string fieldArgsStr = $"'Field1' '\"{field1value}\"'; 'Field2' '\"{field2value}\"'";
var argsCalculate = Geoprocessing.MakeValueArray(featureLayer, "PYTHON3", fieldArgsStr);
IGPResult result = await Geoprocessing.ExecuteToolAsync("management.CalculateFields", argsCalculate, null, null, null, GPExecuteToolFlags.None);

Thanks for the help, Gintautas!

 

 

GKmieliauskas
Esri Regular Contributor

I would recommend for issues with geoprocessing parameters use ExecuteToolAsync method with GPToolExecuteEventHandler parameter. Sample you can find in documentation here, or in Community samples.

0 Kudos