I'm trying to build on the example given in "Working with data inside feature sets" found here and am having trouble finding a way to sum the DBH_TRUNK values for the tree points that intersect the neighborhoods. I thought I could use
Solved! Go to Solution.
Hi Gregory Bacon ,
I guess you can do something like this:
var intersecting_trees = Intersects(FeatureSetByName($map,"Urban Forestry"), $feature);
// var count_trees = Count(intersecting_trees);
return Sum(intersecting_trees, "DBH_TRUNK");
In your code you were trying to loop through a count. I you want to use a loop and not the Sum function, you could do something like this:
var intersecting_trees = Intersects(FeatureSetByName($map,"Urban Forestry"), $feature);
var sum_dbh = 0;
for (var tree in intersecting_trees) {
sum_dbh += tree.DBH_TRUNK;
}
return sum_dbh;
Hi Gregory Bacon ,
I guess you can do something like this:
var intersecting_trees = Intersects(FeatureSetByName($map,"Urban Forestry"), $feature);
// var count_trees = Count(intersecting_trees);
return Sum(intersecting_trees, "DBH_TRUNK");
In your code you were trying to loop through a count. I you want to use a loop and not the Sum function, you could do something like this:
var intersecting_trees = Intersects(FeatureSetByName($map,"Urban Forestry"), $feature);
var sum_dbh = 0;
for (var tree in intersecting_trees) {
sum_dbh += tree.DBH_TRUNK;
}
return sum_dbh;
Thank you. Both of these work for me. I think I was very close to success using your first method in one of my early attempts but I deleted $feature by mistake in Line 1 somehow. With respect to the for loop in method 2, you're assigning a starting value, zero, for the sum_dbh variable and the Add Assign += operator accesses the intersected features and performs 0 + DBH_TRUNK1 + DBH_TRUNK2+...etc, correct? Is either method more efficient than the other? They seem to take the same amount of time to populate the pop-up (3-4 seconds).
Hi Gregory Bacon ,
You were close indeed. Only of the problems was looping through a count when that should have been a featureset. You are right about the logic. We start off with a variable sum_dbh which is set to 0 before looping through the features. For each feature the dbh_trunk is added to the running total stored in sum_dbh and that value is returned at the end of the loop.
Regarding performance, both the sum and manually looping through the features could cost the same time as you are experiencing. In theory, it should be less time when less requests are made to the server. This could be done by using a single line:
return Sum(Intersects(FeatureSetByName($map,"Urban Forestry"), $feature), "DBH_TRUNK");
Not sure though if you will notice any difference. Another aspect could be to limit the number of fields returned by the FeatureSetByName:
return Sum(Intersects(FeatureSetByName($map,"Urban Forestry", ["DBH_TRUNK"], False), $feature), "DBH_TRUNK");
(note entirely sure if I use the correct field list)