Select to view content in your preferred language

Determine Count of features with Arcade to be used in Pop-Up

1876
2
03-23-2023 10:39 AM
Labels (1)
JeremyMcDonald1
New Contributor II

Hello!

I have two point feature layers, one is  for Mine Sites and the second is for Mine Features. What I want to do is write an arcade expression to be used in the pop-up which displays the count of Mine Features for each Mine Site.  They do not have a relate to connect them but I assumed it would be possible to pull the data through FeatureSetByPortalItem. Both feature layers contain the matching fields such as "OFFICIAL_N".

 

I have it written two ways but I'm sure I'm missing a step somewhere to make it work in the pop-up. As neither are giving me the result I want. 

 

This option gives me the counts based on the Official Name which is the same between both feature layers. but when I try to use this expression in the pop-up it returns [object Object] instead of the numbered Counts. But when I run it in the Arcade Window it does give me the total counts for each Name. 

var fs = FeatureSetByPortalItem(Portal('https://www.arcgis.com'),
'ID', 0, ['OFFICIAL_N'], False);

var counts = Groupby(fs,['OFFICIAL_N'],
[{name:'Total', expression: 'OFFICIAL_N',
statistic: 'COUNT'}
]);
return counts
 
Or this code which has an additional for loop but in the pop-up it says that there are 3 features a every site which is incorrect.
 
var fs = FeatureSetByPortalItem(Portal('https://www.arcgis.com'),
'ID', 0, ['OFFICIAL_N'], False);

var counts = Groupby(fs,['OFFICIAL_N'],
[{name:'Total', expression: 'OFFICIAL_N',
statistic: 'COUNT'}
]);

for (var item in counts){
  var totalcount = item['Total']
 var result = TextFormatting.NewLine + 'Total Number of Features at Mine Site: ' + totalcount
 }
 return result
 
I know I am probably missing something somewhere in the code but can't figure it out so any help would be greatly appreciated as I am new to Arcade!
0 Kudos
2 Replies
JohannesLindner
MVP Frequent Contributor

Your first example doesn't work because GroupBy() returns a featureset (a collection of features), but the popup expects the expression to return a string value.

Your second example doesn't work because you keep overwriting the result variable in your for loop. So the returned value will always be the count of the last mine site.

 

You don't need GroupBy() here. You can just use Filter():

var fs = FeatureSetByPortalItem(Portal('https://www.arcgis.com'), 'ID', 0, ['OFFICIAL_N'], False)

var offilical_n = $feature.OFFICIAL_N
var mine_features_at_this_site = Filter(fs, "OFFICIAL_N = @official_n")
return Count(mine_features_at_this_site)

Have a great day!
Johannes
0 Kudos
avonmoos
Occasional Contributor
// Get all Mine Features
var allMineFeatures = FeatureSetByPortalItem(Portal('portal-id'), 'item-id', 0, ['OFFICIAL_N', 'SHAPE']);

// Get matching Mine Features for the current Mine Site
var matchingMineFeatures = Filter(allMineFeatures, $feature.OFFICIAL_N == $feature.OFFICIAL_N && Within($feature.SHAPE, Geometry($feature)));

// Return the count of matching Mine Features
return Count(matchingMineFeatures);