Calculating New Values for Indicator Widget

466
2
Jump to solution
07-01-2022 08:48 AM
Labels (1)
Calvin_Wong
New Contributor II

I'm wondering if I could get some help making an indicator widget that would dynamically calculate the sum of the product of two fields. Particularly, the product of a water mains leak rate and leak time, then sum each product to get a cumulative leak loss. Here's what I have so far, I'm not very familiar with Arcade, so forgive me. 

 

var portal = Portal('https://www.arcgis.com');
var fs = FeatureSetByPortalItem(
    portal,
    '9ad3138c16dd4c20aca6ca2a1b14dc89',
    9,
    [
        'LEAKRATE',
        'LeakDays'
    ],
    false
);

var ratioDict = { 
    'fields':
    {'name':'leaksum', 'type':'esriFieldTypeDouble'}, 
    'geometryType': '', 
    'features': 
    [{'attributes':
        {'leaksum': (fs, 'LEAKRATE') * (fs, 'LeakDays')
    }}]
};

return FeatureSet(SUM(ratioDict));

 

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Data Expressions can be a bit much to wrap your head around if you're new to Arcade, but they're not terrible once you sort of understand how they work.

When you create your ratioDict, that's going to be the entire FeatureSet that you get out at the end. But initially, it's just a dictionary, which is why the examples all use Text(dict) to get it as a string, and FeatureSet(string) to parse the text into a FeatureSet.

Because your ratioDict is just a dictionary, you can't use functions like Sum on it the way you could on a real FeatureSet.

However! If all you want is to calculate the sum of that calculation, you can use GroupBy on the original fs FeatureSet. GroupBy returns a FeatureSet, so you don't have to bother with a dictionary at all.

var portal = Portal('https://www.arcgis.com');
var fs = FeatureSetByPortalItem(
    portal,
    '9ad3138c16dd4c20aca6ca2a1b14dc89',
    9,
    [
        'LEAKRATE',
        'LeakDays'
    ],
    false
);

return GroupBy(
    fs,
    'objectid',
    {
        name: 'leak_amount',
        expression: 'LEAKRATE * LeakDays',
        statistic: 'SUM'
    }
)

Mind, this expression will return a FeatureSet that is the same length as the input, but with a newly calculated field leak_amount. Since you're using an Indicator, you can just apply the Sum there, instead of shoehorning it into the expression.

The benefit of doing it this way, too, is that keeping the output FeatureSet with the objectID intact will make it so that your indicator can interact with other elements of your Dashboard. 

- Josh Carlson
Kendall County GIS

View solution in original post

2 Replies
jcarlson
MVP Esteemed Contributor

Data Expressions can be a bit much to wrap your head around if you're new to Arcade, but they're not terrible once you sort of understand how they work.

When you create your ratioDict, that's going to be the entire FeatureSet that you get out at the end. But initially, it's just a dictionary, which is why the examples all use Text(dict) to get it as a string, and FeatureSet(string) to parse the text into a FeatureSet.

Because your ratioDict is just a dictionary, you can't use functions like Sum on it the way you could on a real FeatureSet.

However! If all you want is to calculate the sum of that calculation, you can use GroupBy on the original fs FeatureSet. GroupBy returns a FeatureSet, so you don't have to bother with a dictionary at all.

var portal = Portal('https://www.arcgis.com');
var fs = FeatureSetByPortalItem(
    portal,
    '9ad3138c16dd4c20aca6ca2a1b14dc89',
    9,
    [
        'LEAKRATE',
        'LeakDays'
    ],
    false
);

return GroupBy(
    fs,
    'objectid',
    {
        name: 'leak_amount',
        expression: 'LEAKRATE * LeakDays',
        statistic: 'SUM'
    }
)

Mind, this expression will return a FeatureSet that is the same length as the input, but with a newly calculated field leak_amount. Since you're using an Indicator, you can just apply the Sum there, instead of shoehorning it into the expression.

The benefit of doing it this way, too, is that keeping the output FeatureSet with the objectID intact will make it so that your indicator can interact with other elements of your Dashboard. 

- Josh Carlson
Kendall County GIS
Calvin_Wong
New Contributor II

Josh,

This is exactly what I'm looking for! Thank you! I do all of my automation in python and arcpy so trying to wrap my head around Arcade is challenging. 

0 Kudos