This sample demonstrates how to query and filter features represented by clusters using Arcade expressions in a cluster's popup.
Typically clusters display basic summary information about all features in the cluster, such as the total number of features, the predominant type, or the average value of a numeric field. Sometimes you may want to display information about a subset of important features, such as the number of fatalities in a cluster representing car crashes.
The cluster popup in this example displays the total number of power plants that generate power with coal, along with the amount of power they produce as a ratio to all power produced in the cluster. This is all accomplished with Arcade expressions. Within the context of a cluster popup, you can use Arcade to filter a cluster's features and perform statistics on those features.
NoteClustering is a method of reducing features in a FeatureLayer, CSVLayer, GeoJSONLayer, WFSLayer, or OGCFeatureLayer by grouping them based on their spatial proximity to one another. Typically, clusters are proportionally sized based on the number of features within each cluster. See the Intro to clustering sample if you are unfamiliar with clustering.
Use Arcade to summarize clustersArcade expressions in cluster popups give you access to the cluster graphic itself (i.e. $feature
) and the features that make up the cluster (i.e. $aggregatedFeatures
).
The $feature
profile variable allows you to reference aggregate fields used by the renderer, such as $feature.cluster_count
. If the renderer visualizes population with a numeric POP
field, you will have access to the average population of features in the cluster by referencing $feature.cluster_avg_POP
within the expression.
The $aggregatedFeatures
profile variable represents all features included in the cluster. You can use this profile variable to iterate through features, filter them, and calculate statistics with the features.
Use dark colors for code blocks Copy
1
2
3
// returns the total number of features in the cluster
// this will be the same as $feature.cluster_count
Count($aggregatedFeatures)
Use dark colors for code blocks Copy
1
2
// returns the count of features of fuel type 'Coal' within the cluster
Count(Filter($aggregatedFeatures, "fuel1 = 'Coal'"))
The Expects
Arcade function tells the clustered layer to request extra fields required for the popup to function properly.
Use dark colors for code blocks Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// returns the amount of power generated by coal power plants
// as a percentage of total power produced in the cluster along
// with the total number of coal power plants
// Since the capacity_mw field may not be available on the client
// at runtime, we tell the Arcade engine to request it from the layer
Expects($aggregatedFeatures, "capacity_mw")
var mWtotal = Sum($aggregatedFeatures, "capacity_mw");
var mWCoal = Sum(Filter($aggregatedFeatures, "fuel1 = 'Coal'"), "capacity_mw");
if(mWCoal < 1){
return "none";
}
var mWratio = Round(mWCoal / mWtotal, 4);
return \`\${Text(mWratio, "#.#%")} (\${Text(mWCoal, "#,### mW")})\`;
// the output will look like "15% (6,500 mW)"
To use values returned from Arcade expressions within a popup, you must set the Arcade expressions in the expressionInfos
property of the popupTemplate, then reference it using the {expression/expression-name}
syntax.
Calculate cluster statistics
Use dark colors for code blocks Copy
91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122 122
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
let popupTemplate = {
title: "Coal summary",
content: `<p>This cluster represents <b>{cluster_count}</b> power plants. <b>{expression/Coal-count}</b> of these plants are fueled by Coal.</p><p>Coal plants produce <b>{expression/Coal-mw-sum}</b> of the total power generated in this cluster.</p>`,
expressionInfos: [
{
name: "Coal-count",
title: "Coal-count",
expression: `
// returns the total number of coal power plants
Count(Filter($aggregatedFeatures, "fuel1 = 'Coal'"))
`,
},
{
name: "Coal-mw-sum",
title: "Coal-mw-sum",
expression: `
// returns the amount of electricity generated by coal
// in this cluster as a ratio to the total capacity
// of plants included in the cluster
Expects($aggregatedFeatures, "capacity_mw")
var mWtotal = Sum($aggregatedFeatures, "capacity_mw");
var mWCoal = Sum(Filter($aggregatedFeatures, "fuel1 = 'Coal'"), "capacity_mw");
if(mWCoal < 1){
return "none";
}
var mWratio = Round(mWCoal / mWtotal, 4);
return \`\${Text(mWratio, "#.#%")} (\${Text(mWCoal, "#,### mW")})\`;
`,
},
],
};
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4