Select to view content in your preferred language

Esri Dashboard - Sum Different Layers (ArcGIS Online)

343
3
11-20-2023 02:23 AM
SIG-EscutismoCNE
New Contributor II

Hello,

I’m trying to add two different layers but without success. Any suggestions?

 

var portal = Portal('https://www.arcgis.com/')

var fsets = [
  FeatureSetByPortalItem(portal, '8fadffa6772f428db8ffa703f9ebc61b', layerID, ['n_lobitos'], false),
  FeatureSetByPortalItem(portal, '43e99df1123d454cb3d3e48ae5cfa462', layerID, ['n_lobitos'], false),
]

var running_sum = 0

for (var set in fsets) {
  running_sum += Sum(set, 'n_lobitos')
}

return FeatureSet({
  fields: {name: 'sum_n_lobitos', type: 'esriFieldTypeInteger'}, // or whatever field type it is
  geometryType: '',
  features: [{attributes: {sum_n_lobitos: running_sum}}]
})
Tags (2)
0 Kudos
3 Replies
jcarlson
MVP Esteemed Contributor

The problem is right here:

for (var set in fsets) {
running_sum += Sum(set, 'n_lobitos')
}

When you iterate over an array, that nested var is the index, not the item. If you included the line Console(set) in the loop, you'd see it return 0 and 1. That means your Sum expressions are:

Sum(0, 'n_lobitos')

 and

Sum(1, 'n_lobitos')

Which of course, won't return anything.

The way to get each item of an array is to call the original array and then use the index variable to access it. Try using these lines in replacement of the ones quoted earlier:

for (var set in fsets) {
running_sum += Sum(fsets[set], 'n_lobitos')
}
- Josh Carlson
Kendall County GIS
SIG-EscutismoCNE
New Contributor II

Hello Josh, 

Thank you for your attention,

 

However I changed as suggested but still keeps the error.

SIGEscutismoCNE_0-1701211047267.png

 

Do you have any other suggestions?

Thanks,

 

0 Kudos
jcarlson
MVP Esteemed Contributor

Sprinkle some Console functions in there to see how far it gets. When you test the expression, the Console tab will show you the output of those functions.

For instance, you could try this for the for loop:

for (var set in fsets) {
  var set_sum = Sum(fsets[set], 'n_lobitos')
  Console('featureset sum:', set_sum)
  running_sum += set_sum
  Console('running sum:', running_sum)
}

This will output what the individual FeatureSet's sum is, as well as the running sum for that iteration of the loop.

You might also try outputting information about the FeatureSets themselves, to see if your FeatureSetByPortalItem functions are returning legitimate sets.

for (var set in fsets) {
  Console(Count(fset))
}

 

- Josh Carlson
Kendall County GIS