Hello,
I am trying to use an arcade expressions to combine three feature layers (pt, line, polygon) into one data table to graph a shared attributes. I have found many examples of others that have successfully deployed this idea using the information in this link. https://github.com/Esri/arcade-expressions/blob/master/dashboard_data/CombineMultipleLayers(SerialC...
I am not an arcade expert so I have been trying to follow this code and replace their attributes/ names with mine. Unfortunately I keep getting a errors. Specially "Execution Error:json" which does not mean much to me. I am wondering if anyone would be able to help figure out where I am going wrong. Here is the code that I have come up with following the github link.
Any advice or suggestions would be awesome!
Thanks in advance
var portal = Portal('PORTAL_URL');
var point = GroupBy(FeatureSetByPortalItem(
portal,
'FEATURE_ID',
0,
['RBU','CatCode28'],
false),
['RBU'],
[
{ name: 'CatCode28_point', expression: 'Point_CatCode28', statistic: 'SUM' },
]
);
var line = GroupBy(
FeatureSetByPortalItem(portal,'eba1d0d180a0419499566e4aa1336eea',0,
['RBU','CatCode28'],
false),
['RBU'],
[
{ name: 'CatCode28_line', expression: 'Line_CatCode28', statistic: 'SUM' },
]
);
var poly = GroupBy(
FeatureSetByPortalItem(portal,'111b954c606045e18180d507fcc40aef',0,
['RBU','CatCode28'],
false),
['RBU'],
[
{ name: 'CatCode28_poly', expression: 'Poly_CatCode28', statistic: 'SUM' },
]
);
// Create empty array for features, feat object to populate array
var features = [];
var feat;
// Loop through each of the three FeatureSets and populate feature array.
for (var p in point) {
feat = {
attributes: {
type: 'Points',
RBU: p['RBU'],
count_of_found: SUM(p['CatCode28_point']),
},
};
Push(features, feat);
}
for (var l in line) {
feat = {
attributes: {
type: 'Line',
RBU: l['RBU'],
count_of_found: SUM(l['CatCode28_line']),
},
};
Push(features, feat);
}
for (var g in polygon) {
feat = {
attributes: {
type: 'Polygon',
RBU: g['RBU'],
count_of_found: SUM(g['CatCode28_poly']),
},
};
Push(features, feat);
}
var combinedDict = {
fields: [
{ name: 'RBU', type: 'esriFieldTypeString' },
{ name: 'CateCode28', type: 'esriFieldTypeString' },
{ name: 'count_of_found', type: 'esriFieldTypeInteger' },
],
geometryType: '',
features: features,
};
// Return dictionary cast as a feature set
return FeatureSet(Text(combinedDict));
First of all, note the code sample button on here. It can really help others read and understand your expressions.
Second, point is an Arcade function, so try using a different name for that FeatureSet.
Third, in GroupBy, if your grouping and statistics parameters only have a single item, you don't need to put them inside an array.
Fourth, your GroupBy expressions don't match the requested field names. This is not necessarily a problem, but know that the expression you're supplying must match a field name in the service. You don't have to have a field called Point_CatCode28 in your requested list of fields, but it needs to be in the service, otherwise the expression won't evaluate.
Fifth, and this is where you may be getting errors, I think: you're calling the SUM function on individual features, but the function is expecting a FeatureSet, an array, or a list. Calling it on a feature won't work. Also, you're already summing this field in your GroupBy functions. You can replace this with the name of the attribute you created in the GroupBy statement.
count_of_found: p['CatCode28_poly']
If you're trying to get a per-type sum, keep in mind that once your data expression comes into the dashboard, many of the dashboard's own widgets can handle additional grouping and aggregating for you.
Finally, and this could also be the source of the error, your feature attributes objects don't match the fields in your combinedDict. If your combinedDict has fields CatCode28 and count_of_found, then you must have corresponding attributes in your features, otherwise these fields will be null. If you really want your "type" attributes to come through in the output, you need a field for that, too.
It's hard to know exactly how to re-write this without more information about the data and what exactly you're envisioning to come out of it, but here's a version of your expression with some of these changes in place.
var portal = Portal('PORTAL_URL');
var pt = GroupBy(
FeatureSetByPortalItem(
portal,
'FEATURE_ID',
0,
['RBU','CatCode28'],
false
),
'RBU',
{
name: 'CatCode28_point',
expression: 'Point_CatCode28',
statistic: 'SUM'
}
);
var ln = GroupBy(
FeatureSetByPortalItem(
portal,
'eba1d0d180a0419499566e4aa1336eea',
0,
['RBU','CatCode28'],
false
),
'RBU',
{
name: 'CatCode28_line',
expression: 'Line_CatCode28',
statistic: 'SUM'
}
);
var poly = GroupBy(
FeatureSetByPortalItem(
portal,
'111b954c606045e18180d507fcc40aef',
0,
['RBU','CatCode28'],
false
),
'RBU',
{
name: 'CatCode28_poly',
expression: 'Poly_CatCode28',
statistic: 'SUM'
}
);
// Create empty array for features, feat object to populate array
var features = [];
var feat;
// Loop through each of the three FeatureSets and populate feature array.
for (var p in point) {
feat = {
attributes: {
geom_type: 'Points',
RBU: p['RBU'],
count_of_found: p['CatCode28_point']
},
};
Push(features, feat);
}
for (var l in line) {
feat = {
attributes: {
geom_type: 'Line',
RBU: l['RBU'],
count_of_found: l['CatCode28_line']
}
};
Push(features, feat);
}
for (var g in polygon) {
feat = {
attributes: {
geom_type: 'Polygon',
RBU: g['RBU'],
count_of_found: g['CatCode28_poly'],
}
};
Push(features, feat);
};
var combinedDict = {
fields: [
{name: 'geom_type', type: 'esriFieldTypeString'},
{name: 'RBU', type: 'esriFieldTypeString' },
{name: 'count_of_found', type: 'esriFieldTypeInteger' }
],
geometryType: '',
features: features
};
// Return dictionary cast as a feature set
return FeatureSet(Text(combinedDict));
Hi Josh,
Thanks for the quick response. Looking over your comments it is clear to me that I do not know what I am doing and copy and pasting my attributes into an existing code did not do the trick. My goal is to combine my three asset features (point, line, polygon) into one table. I would like to sum the CatCode28 (found/ not found) attribute in each feature and separate the total sums by RBU (responsible business unit). Essentially I want to know how many assets from each RBU within my three features have been found or not found (CatCode28), then present the data in a serial chart (bar graph) within dashboard for the RBU managers. Hopefully that sheds some light on what I am trying to do. I have attempted to touch up the code with the guidance you gave but at a certain point I am just blindingly trying things with no idea what I am doing. Hopefully my explanation will give you more to work with. I really appreciate the help.
So, when you say "sum the CatCode28 attribute in each feature", what does that mean? How can you sum a single attribute inside of a single feature? Do you mean you want to sum it by feature type? Or sum all the features by CatCode?
Now that you ask that I realize that the sum of those attributes might not be what I need. A count of the the different types of CatCode28 is really what I need. I want to display how many assets in pt, line and polygon, have been Found or Not found, which is represented in the CatCode28 attribute as text. Then filter the data for all three layers by RBU.