Hello,
I'm trying to create a new Arcade attribute expression in the content of a layer's pop-up window that does the following:
I tried a script like the one below, without success…
var table = FeatureSetByID($datastore,"PARCELS",[‘*’], true);
var total = 0;
for (var t in table) {
total = total + $feature["Shape__Area"]
}
return total;
If anyone can help me, I will accept with pleasure.
Thanks in advance.
Victor
Solved! Go to Solution.
For something as simple as this, you can use the built-in function Sum(), which can take a FeatureSet and field name as parameters.
Also, since the "SHAPE__Area" field is an attribute, you can specify that field alone and skip bringing in the geometry in the FeatureSetByName function to improve performance.
var fs = FeatureSetByName($datastore, "PARCELS", ['SHAPE__Area'], false)
return Sum(fs, 'SHAPE__Area')
That should return something like this:
You were on the right track (although there is an easier solution, as Josh pointed out).
Where you went wrong:
You used $feature in the for loop. $feature is a global variable describing the feature to which a popup/rule/label/etc applies, in your case the feature you click on. If your $feature isn't a polygon, the expression won't work. If your $feature is a polygon, you will get a wrong total (because you multiply the $feature's area instead of adding up the target areas).
This expression should work (although, again, Josh's answer is easier):
// Also, maybe you need to use FeatureSetByName here...
var table = FeatureSetByID($datastore,"PARCELS",[‘*’], true);
var total = 0;
for (var t in table) {
total = total + t["Shape__Area"]
}
return total;
For something as simple as this, you can use the built-in function Sum(), which can take a FeatureSet and field name as parameters.
Also, since the "SHAPE__Area" field is an attribute, you can specify that field alone and skip bringing in the geometry in the FeatureSetByName function to improve performance.
var fs = FeatureSetByName($datastore, "PARCELS", ['SHAPE__Area'], false)
return Sum(fs, 'SHAPE__Area')
That should return something like this:
You were on the right track (although there is an easier solution, as Josh pointed out).
Where you went wrong:
You used $feature in the for loop. $feature is a global variable describing the feature to which a popup/rule/label/etc applies, in your case the feature you click on. If your $feature isn't a polygon, the expression won't work. If your $feature is a polygon, you will get a wrong total (because you multiply the $feature's area instead of adding up the target areas).
This expression should work (although, again, Josh's answer is easier):
// Also, maybe you need to use FeatureSetByName here...
var table = FeatureSetByID($datastore,"PARCELS",[‘*’], true);
var total = 0;
for (var t in table) {
total = total + t["Shape__Area"]
}
return total;
Thank you for your both answer to my topic, it allowed me to find the solution to my issue.
Actually, I have deliberately simplified my calculation so that I can make it more complex then.
I don't think I can use the sum() function because I have to combine several attribute values in my calculation. I will therefore use the second solution.
Thanks again for your help.