Add all Shape_Area in attribut expression in the contents of a layer's pop-up window on AGOL

615
3
Jump to solution
10-13-2021 05:01 AM
Labels (1)
Victor_M
New Contributor II

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:

  • Go through all elements contained in the layer "PARCELS"
  • add up all the Shape_area of each element
  • display the total

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

0 Kudos
2 Solutions

Accepted Solutions
jcarlson
MVP Esteemed Contributor

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:

jcarlson_0-1634130200021.png

 

- Josh Carlson
Kendall County GIS

View solution in original post

JohannesLindner
MVP Frequent Contributor

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;

 


Have a great day!
Johannes

View solution in original post

3 Replies
jcarlson
MVP Esteemed Contributor

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:

jcarlson_0-1634130200021.png

 

- Josh Carlson
Kendall County GIS
JohannesLindner
MVP Frequent Contributor

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;

 


Have a great day!
Johannes
Victor_M
New Contributor II

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.

0 Kudos