I am clustering points in a web map and I have an Arcade script that summarizes some of the attribute values from the clustered points.
I am finding some cases where the number of features reported to be in the cluster (and the number of records I get when I click on "Browse Features") matches up with the counts that are being returned by my Arcade script, but other cases where these numbers do not match up.
I thought at first that there might be a problem with the logic of my script, but after reviewing it and adding some Console() statements to the script, I am finding that the "Count($aggregatedFeatures) returns a lower number than what the feature cluster contains.
Example of a cluster where there is a mismatch:
Example of Arcade editor log with Console(Count($aggregatedFeatures)) statement - the layer has been filtered to just the 7 points that are in the cluster from the above screenshot:
The full Arcade script I am attempting to use:
// List the fields referenced by the expression.
Expects($aggregatedFeatures, "Project_Type", "Project_Status");
Console(Count($aggregatedFeatures))
// Create dict to store summaries of project type and status
var typeSummary = {};
// Go over each feature and add/update the counts of project type and status to the dict
for (feature in $aggregatedFeatures) {
// Set variables for type and status, setting nulls to 'null' string for proper handling
// While setting, replace spaces with _ so that dict keys can be created
var type = When(!IsEmpty(feature['Project_Type']), Replace(feature['Project_Type'], ' ', '_'), 'null');
var status = When(!IsEmpty(feature['Project_Status']), Replace(feature['Project_Status'], ' ', '_'), 'null');
// If dict doesn't have this type, add everything with an intitial count of 1
if (!HasKey(typeSummary, type)) {
typeSummary[type] = {
'count': 1,
'statusSummary': {}
}
typeSummary[type]['statusSummary'][status] = 1
} else {
// Dict has type, increment type count by 1
typeSummary[type]['count']++ ;
// Check if status exists for this type, set to 1 or increment by 1 as necessary
if (!HasKey(typeSummary[type]['statusSummary'], status)) {
typeSummary[type]['statusSummary'][status] = 1
} else {
typeSummary[type]['statusSummary'][status]++
}
}
}
// Prepare to build html list output with top level list
var html = '<ul>'
// Each project type will get a list item in the top level list
for (type in typeSummary) {
html += `\n\t<li>${Replace(type, '_', ' ')} - ${typeSummary[type]['count']} projects</li>\n\t<ul>`
// Each project status within each project type will get a list item in the nested list
for (status in typeSummary[type]['statusSummary']) {
html += `\n\t\t<li>${Replace(status, '_', ' ')} - ${typeSummary[type]['statusSummary'][status]} projects</li>`
}
html += '\n\t</ul>'
}
html += '\n</ul>'
return {
type : 'text',
text : html //this property supports html tags
}
Solved! Go to Solution.
I believe I have found the cause of this issue. I added some different Console() statements to the script so that I could compare Count($aggregatedFeatures) to $feature["cluster_count"] as I clicked on clusters, and I found that this warning was showing up in DevTools when I clicked on a cluster for which there were discrepancies between the two counts:
It seems that the issue was happening when points in the cluster had values for one of the fields that were not included in the domain for that field. I added the new value to the domain, and am now seeing the result that I was expecting returning from the Arcade expression.
This is odd. I haven't observed it before. Are you able to share the dataset or reproduce it with a public layer?
I attempted to reproduce this with the USA Airports layer from the Living Atlas. I took the exact same Arcade code and just replaced the fields, "Project_Type" and "Project_Status", with "Fac_Type" and "Fac_Use". It seems to be working as expected using this layer - the results in the Arcade element match the cluster count in the cluster summary as far as I can tell.
Unfortunately, I don't have the ability to share layers publicly, but I could add the layer/map that I'm having the issue with to an Any organization group to share it that way.
I believe I have found the cause of this issue. I added some different Console() statements to the script so that I could compare Count($aggregatedFeatures) to $feature["cluster_count"] as I clicked on clusters, and I found that this warning was showing up in DevTools when I clicked on a cluster for which there were discrepancies between the two counts:
It seems that the issue was happening when points in the cluster had values for one of the fields that were not included in the domain for that field. I added the new value to the domain, and am now seeing the result that I was expecting returning from the Arcade expression.