//Consult https://github.com/Esri/arcade-expressions/ and
//https://developers.arcgis.com/arcade/ for more examples
//and arcade reference
var map = MapView.Active.Map;
QueuedTask.Run(() =>
{
//construct a query
var query = new StringBuilder();
//https://developers.arcgis.com/arcade/function-reference/featureset_functions/
//Assume we have two layers - Oregon Counties (poly) and Crimes (points). Crimes
//is from the Pro SDK community sample data.
//Select all crime points within the relevant county boundaries and sum the count
query.AppendLine("var results = [];");
query.AppendLine("var counties = FeatureSetByName($map, 'Oregon_Counties', ['*'], true);");
//'Clackamas','Multnomah','Washington'
query.AppendLine("var sel_counties = Filter(counties, 'DHS_Districts IN (2, 15, 16)');");
query.AppendLine("for(var county in sel_counties) {");
query.AppendLine(" var name = county.County_Name;");
query.AppendLine(" var cnt_crime = Count(Intersects($layer, Geometry(county)));");
query.AppendLine(" Insert(results, 0, cnt_crime);");
query.AppendLine(" Insert(results, 0, name);");
query.AppendLine("}");
query.AppendLine("return Concatenate(results,'|');");
//construct a CIMExpressionInfo
var arcade_expr = new CIMExpressionInfo()
{
Expression = query.ToString(),
//Return type can be string, numeric, or default
//When set to default, addin is responsible for determining
//the return type
ReturnType = ExpressionReturnType.Default
};
//Construct an evaluator
//select the relevant profile - it must support Pro and it must
//contain any profile variables you are using in your expression.
//Consult: https://developers.arcgis.com/arcade/profiles/
using (var arcade = ArcadeScriptEngine.Instance.CreateEvaluator(
arcade_expr, ArcadeProfile.Popups))
{
//Provision values for any profile variables referenced...
//in our case '$layer' and '$map'
var variables = new List<KeyValuePair<string, object>>() {
new KeyValuePair<string, object>("$layer", crimes_layer),
new KeyValuePair<string, object>("$map", map)
};
//evaluate the expression
try
{
var result = arcade.Evaluate(variables).GetResult();
var results = result.ToString().Split('|', StringSplitOptions.None);
var entries = results.Length / 2;
int i = 0;
for (var e = 0; e < entries; e++)
{
var name = results[i++];
var count = results[i++];
System.Diagnostics.Debug.WriteLine($"'{name}' crime count: {count}");
}
}
//handle any exceptions
catch (InvalidProfileVariableException ipe)
{
//something wrong with the profile variable specified
//TODO...
}
catch (EvaluationException ee)
{
//something wrong with the query evaluation
//TODO...
}
}
});