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
Category | FeatureCount | FID |
A - No Impact | 35 | 0 |
B - Material Excavation | 2 | 1 |
D - Habitat Restoration | 3 | 2 |
E - Stormwater Excavation | 8 | 3 |
F - Utilities | 2 | 4 |
H - Temporary Impact | 14 | 5 |
Summary 2 - NO_LOSS_TYPE2
Category | FeatureCount | FID |
B - Material Excavation | 1 | 0 |
E - Stormwater Excavation | 3 | 1 |
F - Utilities | 1 | 2 |
H - Temporary Impact | 2 | 3 |
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:
Category | FeatureCount | FID |
A - No Impact | 35 | 0 |
B - Material Excavation | 3 (i.e., 2 + 1) | 1 |
D - Habitat Restoration | 3 | 2 |
E - Stormwater Excavation | 11 (i.e,. 8 + 3) | 3 |
F - Utilities | 3 (i.e., 3 + 1) | 4 |
H - Temporary Impact | 16 (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!
Solved! Go to Solution.
// 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))
// 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))
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))