In a data expression, I'm tying to create a dictionary that will be returned as a FeatureSet to support a serial chart in a dashboard - chart will display for each shelter in my dataset the number of beds, occupied beds, and the number of available beds. However, I'm having difficulty building this to include more than two fields (i have four), Ok, I found I left out a single quote to end the field descriptions, silly mistake. There are other issues as i get an error when trying to return a feature set from the dictionary. Code is below, see the comments (//) for a description of what is happening. I was working from the Aggregate By Date of Week (serial chart) example code, is there another documentation reference that discusses creating the json and a resulting feature set in more detail? Any thoughts will help.
// get beds data by shelter
var cabq_agol = Portal("https://cabq.maps.arcgis.com/");
var fs_shelters = FeatureSetByPortalItem(cabq_agol, '4ebc62fe425c42ad909b1f30029ded55',['*']);
var sh_feats = [];
var feat;
for (var shelter in fs_shelters) {
var bed_tot = Sum(shelter['female_total'],shelter['male_total'],shelter['couples_total'],shelter['family_total'], shelter['youth_total'], shelter['open_mf_total'])
var bed_occ = Sum(shelter['female_occupied'],shelter['male_occupied'],shelter['couples_occupied'],shelter['family_occupied'], shelter['youth_occupied'], shelter['open_mf_occupied'])
var avail_bed = bed_tot - bed_occ
feat = {
'attributes': {
'sh_name':shelter['name'],
'bed_tot':[bed_tot],
'bed_occ':[bed_occ],
'avail_bed':[avail_bed]
}
}
}
Push(sh_feats, feat)
Console(sh_feats)//this returns a correct array of values
// corrected the issue with the brackets i was seeing initially by adding in a closing single quote ( ' ),that was left off after esriFieldTypeInteger
var shelterDict = {
'fields': [{'name':'sh_name','type':'esriFieldTypeTxt'},
{'name':'bed_tot','type':'esriFieldTypeInteger'},
{'name':'bed_occ','type':'esriFieldTypeInteger'},
{'name':'avail_bed','type':'esriFieldTypeInteger'}],
'geometryType':'',
'features':sh_feats
};
return FeatureSet(shelterDict);
// this gives me a 'Test execution error: Unknown Error. Verify test data.' warning so something isn't getting working
Solved! Go to Solution.
There were a couple problems with your code. In lines 13-15, you have the values in an array. You should push the feat into the array inside the for loop (switch lines 18 and 19). The correct field type for strings is esriFieldTypeString in line 24.
This should work
// get beds data by shelter
var cabq_agol = Portal("https://cabq.maps.arcgis.com/");
var fs_shelters = FeatureSetByPortalItem(cabq_agol, '4ebc62fe425c42ad909b1f30029ded55',['*']);
var sh_feats = [];
var feat;
for (var shelter in fs_shelters) {
var bed_tot = Sum(shelter['female_total'],shelter['male_total'],shelter['couples_total'],shelter['family_total'], shelter['youth_total'], shelter['open_mf_total'])
var bed_occ = Sum(shelter['female_occupied'],shelter['male_occupied'],shelter['couples_occupied'],shelter['family_occupied'], shelter['youth_occupied'], shelter['open_mf_occupied'])
var avail_bed = bed_tot - bed_occ
feat = {
'attributes': {
'sh_name':shelter['name'],
'bed_tot':bed_tot,
'bed_occ':bed_occ,
'avail_bed':avail_bed
}
}
Push(sh_feats, feat)
}
Console(sh_feats)//this returns a correct array of values
// corrected the issue with the brackets i was seeing initially by adding in a closing single quote ( ' ),that was left off after esriFieldTypeInteger
var shelterDict = {
'fields': [{'name':'sh_name','type':'esriFieldTypeString'},
{'name':'bed_tot','type':'esriFieldTypeInteger'},
{'name':'bed_occ','type':'esriFieldTypeInteger'},
{'name':'avail_bed','type':'esriFieldTypeInteger'}],
'geometryType':'',
'features':sh_feats
};
return FeatureSet(shelterDict);
// this gives me a 'Test execution error: Unknown Error. Verify test data.' warning so something isn't getting working
There were a couple problems with your code. In lines 13-15, you have the values in an array. You should push the feat into the array inside the for loop (switch lines 18 and 19). The correct field type for strings is esriFieldTypeString in line 24.
This should work
// get beds data by shelter
var cabq_agol = Portal("https://cabq.maps.arcgis.com/");
var fs_shelters = FeatureSetByPortalItem(cabq_agol, '4ebc62fe425c42ad909b1f30029ded55',['*']);
var sh_feats = [];
var feat;
for (var shelter in fs_shelters) {
var bed_tot = Sum(shelter['female_total'],shelter['male_total'],shelter['couples_total'],shelter['family_total'], shelter['youth_total'], shelter['open_mf_total'])
var bed_occ = Sum(shelter['female_occupied'],shelter['male_occupied'],shelter['couples_occupied'],shelter['family_occupied'], shelter['youth_occupied'], shelter['open_mf_occupied'])
var avail_bed = bed_tot - bed_occ
feat = {
'attributes': {
'sh_name':shelter['name'],
'bed_tot':bed_tot,
'bed_occ':bed_occ,
'avail_bed':avail_bed
}
}
Push(sh_feats, feat)
}
Console(sh_feats)//this returns a correct array of values
// corrected the issue with the brackets i was seeing initially by adding in a closing single quote ( ' ),that was left off after esriFieldTypeInteger
var shelterDict = {
'fields': [{'name':'sh_name','type':'esriFieldTypeString'},
{'name':'bed_tot','type':'esriFieldTypeInteger'},
{'name':'bed_occ','type':'esriFieldTypeInteger'},
{'name':'avail_bed','type':'esriFieldTypeInteger'}],
'geometryType':'',
'features':sh_feats
};
return FeatureSet(shelterDict);
// this gives me a 'Test execution error: Unknown Error. Verify test data.' warning so something isn't getting working
ah, I already fixed the Push statement, caught that after i posted last night. I should have known better about the fieldtypeString issue though. Thanks for that catch @KenBuja