{
//Consult https://github.com/Esri/arcade-expressions/ and
//https://developers.arcgis.com/arcade/ for more examples
//and arcade reference
// Note: the following should be embedded in a QueuedTask.Run() statement
{
//construct a query
var query = new StringBuilder();
//https://developers.arcgis.com/arcade/function-reference/featureset_functions/
//Assume we have two layers - a polygon and a points layer.
//Select all points within the relevant polygon boundaries and sum the count
query.AppendLine("var results = [];");
query.AppendLine("var polygons = FeatureSetByName($map, 'PolygonLayerName', ['*'], true);");
query.AppendLine("var sel_polygons = Filter(polygons, 'IDField IN (2, 15, 16)');");
query.AppendLine("for(var polygon in sel_polygons) {");
query.AppendLine(" var name = polygon.Name;");
query.AppendLine(" var PointsInPolygon = Count(Intersects($layer, Geometry(polygon)));");
query.AppendLine(" Insert(results, 0, pointsInPolygon);");
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, add-in 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'
List<KeyValuePair<string, object>> variables = [
new("$layer", pointsLayer),
new("$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}' points count: {count}");
}
}
//handle any exceptions
catch (InvalidProfileVariableException)
{
//something wrong with the profile variable specified
//TODO...
}
catch (EvaluationException)
{
//something wrong with the query evaluation
//TODO...
}
}
}