Merge two result sets?

450
2
Jump to solution
12-14-2021 01:15 PM
ToddLusk
New Contributor III

Greetings all-

I need to merge two summary results together to use in a serial chart in Dashboard.  The summary tables/results look like the following:

Summary 1 - NO_LOSS_TYPE1

CategoryFeatureCountFID
A - No Impact350
B - Material Excavation21
D - Habitat Restoration32
E - Stormwater Excavation83
F - Utilities24
H - Temporary Impact145

 

Summary 2 - NO_LOSS_TYPE2

CategoryFeatureCountFID
B - Material Excavation10
E - Stormwater Excavation31
F - Utilities12
H - Temporary Impact23

 

As you can see, sometimes the "Category" value matches.  If it matches the two result sets, I want to add the "FeatureCount" values together.  If the "Category" value doesn't exist, I want to append/add it to the output results.

So I'd ultimately like to end up with something like the following:

CategoryFeatureCountFID
A - No Impact350
B - Material Excavation3  (i.e., 2 + 1)1
D - Habitat Restoration32
E - Stormwater Excavation11 (i.e,. 8 + 3)3
F - Utilities3 (i.e., 3 + 1)4
H - Temporary Impact16 (i.e., 14 + 2)5

 

I've managed to get the summary results okay, but am struggling with where to go next.  I've added those summary results to a dictionary, but then have trouble finding the corresponding keys in either of the dictionaries while trying to "merge" the two of them together.  I've been looping through one dictionary and using "HasKey()" to see if it has a corresponding key from the second dictionary, but that hasn't been finding the keys.  My "input" dictionaries look like this:

Dictionary 1

{"fields":[{"name":"Category","type":"esriFieldTypeString"},{"name":"FeatureCount","type":"esriFieldTypeInteger"}],"geometryType":"","features":[{"attributes":{"Category":"A - No Impact","FeatureCount":35}},{"attributes":{"Category":"B - Material Excavation","FeatureCount":2}},{"attributes":{"Category":"D - Habitat Restoration","FeatureCount":3}},{"attributes":{"Category":"E - Stormwater Excavation","FeatureCount":8}},{"attributes":{"Category":"F - Utilities","FeatureCount":2}},{"attributes":{"Category":"H - Temporary Impact","FeatureCount":14}}]}

Dictionary 2

{"fields":[{"name":"Category","type":"esriFieldTypeString"},{"name":"FeatureCount","type":"esriFieldTypeInteger"}],"geometryType":"","features":[{"attributes":{"Category":"B - Material Excavation","FeatureCount":1}},{"attributes":{"Category":"E - Stormwater Excavation","FeatureCount":3}},{"attributes":{"Category":"F - Utilities","FeatureCount":1}},{"attributes":{"Category":"H - Temporary Impact","FeatureCount":2}}]}

Any help is greatly appreciated!

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor
// summary FeatureSets
var no_loss_type_1 = {"fields":[{"name":"Category","type":"esriFieldTypeString"},{"name":"FeatureCount","type":"esriFieldTypeInteger"}],"geometryType":"","features":[{"attributes":{"Category":"A - No Impact","FeatureCount":35}},{"attributes":{"Category":"B - Material Excavation","FeatureCount":2}},{"attributes":{"Category":"D - Habitat Restoration","FeatureCount":3}},{"attributes":{"Category":"E - Stormwater Excavation","FeatureCount":8}},{"attributes":{"Category":"F - Utilities","FeatureCount":2}},{"attributes":{"Category":"H - Temporary Impact","FeatureCount":14}}]}
var no_loss_type_2 = {"fields":[{"name":"Category","type":"esriFieldTypeString"},{"name":"FeatureCount","type":"esriFieldTypeInteger"}],"geometryType":"","features":[{"attributes":{"Category":"B - Material Excavation","FeatureCount":1}},{"attributes":{"Category":"E - Stormwater Excavation","FeatureCount":3}},{"attributes":{"Category":"F - Utilities","FeatureCount":1}},{"attributes":{"Category":"H - Temporary Impact","FeatureCount":2}}]}
no_loss_type_1 = FeatureSet(Text(no_loss_type_1))
no_loss_type_2 = FeatureSet(Text(no_loss_type_2))

// This is ugly, but it works
var count_dict = {}  // dict of {Category: FeatureCount}
var cat_array = []  // to keep track of the dict keys
var fs_array = [no_loss_type_1, no_loss_type_2]  // FeatureSets to combine
for(var i in fs_array) {
    for(var f in fs_array[i]) {
        // get category and feature count
        var cat = f.Category
        var cnt = f.FeatureCount
        if(Includes(cat_array, cat)) {
            // cat already in dict? -> add the dict's value to cnt
            cnt += count_dict[cat]
        } else {
            // new cat? -> append cat to cat_array
            Push(cat_array, cat)
        }
        // add/update the count in count_dict
        count_dict[cat] = cnt
    }
}

// Create output FeatureSet
var out_fs = {
    "fields":[
        {"name":"Category","type":"esriFieldTypeString"},
        {"name":"FeatureCount","type":"esriFieldTypeInteger"},
        ],
    "geometryType":"",
    "features":[]
    }
// add features
for(var i in cat_array) {
    var cat = cat_array[i]
    var f = {"attributes": {"Category": cat, "FeatureCount": count_dict[cat]}}
    Push(out_fs.features, f)
}

return FeatureSet(Text(out_fs))

JohannesLindner_0-1639557965858.png

 


Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor
// summary FeatureSets
var no_loss_type_1 = {"fields":[{"name":"Category","type":"esriFieldTypeString"},{"name":"FeatureCount","type":"esriFieldTypeInteger"}],"geometryType":"","features":[{"attributes":{"Category":"A - No Impact","FeatureCount":35}},{"attributes":{"Category":"B - Material Excavation","FeatureCount":2}},{"attributes":{"Category":"D - Habitat Restoration","FeatureCount":3}},{"attributes":{"Category":"E - Stormwater Excavation","FeatureCount":8}},{"attributes":{"Category":"F - Utilities","FeatureCount":2}},{"attributes":{"Category":"H - Temporary Impact","FeatureCount":14}}]}
var no_loss_type_2 = {"fields":[{"name":"Category","type":"esriFieldTypeString"},{"name":"FeatureCount","type":"esriFieldTypeInteger"}],"geometryType":"","features":[{"attributes":{"Category":"B - Material Excavation","FeatureCount":1}},{"attributes":{"Category":"E - Stormwater Excavation","FeatureCount":3}},{"attributes":{"Category":"F - Utilities","FeatureCount":1}},{"attributes":{"Category":"H - Temporary Impact","FeatureCount":2}}]}
no_loss_type_1 = FeatureSet(Text(no_loss_type_1))
no_loss_type_2 = FeatureSet(Text(no_loss_type_2))

// This is ugly, but it works
var count_dict = {}  // dict of {Category: FeatureCount}
var cat_array = []  // to keep track of the dict keys
var fs_array = [no_loss_type_1, no_loss_type_2]  // FeatureSets to combine
for(var i in fs_array) {
    for(var f in fs_array[i]) {
        // get category and feature count
        var cat = f.Category
        var cnt = f.FeatureCount
        if(Includes(cat_array, cat)) {
            // cat already in dict? -> add the dict's value to cnt
            cnt += count_dict[cat]
        } else {
            // new cat? -> append cat to cat_array
            Push(cat_array, cat)
        }
        // add/update the count in count_dict
        count_dict[cat] = cnt
    }
}

// Create output FeatureSet
var out_fs = {
    "fields":[
        {"name":"Category","type":"esriFieldTypeString"},
        {"name":"FeatureCount","type":"esriFieldTypeInteger"},
        ],
    "geometryType":"",
    "features":[]
    }
// add features
for(var i in cat_array) {
    var cat = cat_array[i]
    var f = {"attributes": {"Category": cat, "FeatureCount": count_dict[cat]}}
    Push(out_fs.features, f)
}

return FeatureSet(Text(out_fs))

JohannesLindner_0-1639557965858.png

 


Have a great day!
Johannes
ToddLusk
New Contributor III

Thanks so much!  That was just about perfect but got me headed in the right direction.  Here was is my final implementation:

var fs = FeatureSetByPortalItem(
    Portal('https://dakotacounty.maps.arcgis.com'),
    '3f1ab7bbf9674ccc851176de9d922450',
    5,
    ['OBJECTID', 'NO_LOSS_TYPE1', 'NO_LOSS_TYPE2'],
    false
);

// summary FeatureSets
var no_loss_type_1 = Filter(fs, 'NO_LOSS_TYPE1 IS NOT NULL');
var no_loss_type_2 = Filter(fs, 'NO_LOSS_TYPE2 IS NOT NULL');

// Grouped statistics
var group_type_1 = GroupBy(no_loss_type_1,
    [
        {name: 'Category',expression:'NO_LOSS_TYPE1'}
    ],
    [
        { name: 'FeatureCount', expression: 'OBJECTID', statistic: 'COUNT' }
    ]
);
var group_type_2 = GroupBy(no_loss_type_2,
    [
        {name: 'Category', expression:'NO_LOSS_TYPE2'}
    ],
    [
        {name: 'FeatureCount', expression: 'OBJECTID', statistic: 'COUNT' }
    ]
);

// This is ugly, but it works
var count_dict = {}  // dict of {Category: FeatureCount}
var cat_array = []  // to keep track of the dict keys
var fs_array = [group_type_1, group_type_2]  // FeatureSets to combine
for(var i in fs_array) {
    for(var f in fs_array[i]) {
        // get category and feature count
        var cat = f.Category
        var cnt = f.FeatureCount
        if(Includes(cat_array, cat)) {
            // cat already in dict? -> add the dict's value to cnt
            cnt += count_dict[cat]
        } else {
            // new cat? -> append cat to cat_array
            Push(cat_array, cat)
        }
        // add/update the count in count_dict
        count_dict[cat] = cnt
    }
}

// Create output FeatureSet
var out_fs = {
    "fields":[
        {"name":"Category","type":"esriFieldTypeString"},
        {"name":"FeatureCount","type":"esriFieldTypeInteger"},
        ],
    "geometryType":"",
    "features":[]
    }
// add features
for(var i in cat_array) {
    var cat = cat_array[i]
    var f = {"attributes": {"Category": cat, "FeatureCount": count_dict[cat]}}
    Push(out_fs.features, f)
}

return FeatureSet(Text(out_fs))

 

0 Kudos