点聚类 - 过滤弹出窗口要素

尝试一下在线预览

此示例演示如何使用聚类弹出窗口中的 Arcade 表达式查询和过滤由点聚类表示的要素。

通常,聚类显示聚类中所有要素的基本摘要信息,例如要素总数、主要类型或数值字段的平均值。有时,您可能希望显示有关重要要素子集的信息,例如表示车祸的聚类中的死亡人数。

此示例中的聚类弹出窗口显示使用煤炭发电的发电厂总数,以及它们产生的电量与聚类中产生的所有功率的比率。这一切都是通过 Arcade  表达式完成的。在聚类弹出窗口的上下文中,您可以使用 Arcade 过滤聚类的要素并对这些要素执行统计。

使用 Arcade 汇总聚类

通过聚类弹出窗口中的 Arcade 表达式,您可以访问聚类图形本身(即 $feature)以及组成聚类的要素(即 $aggregatedFeatures)。

$feature

$feature 全局允许引用渲染器使用的聚合字段,例如 $feature.cluster_count。如果渲染器使用数值 POP 字段可视化人口,则您可以通过在表达式中引用 $feature.cluster_avg_POP 来访问聚类中要素的平均人口。

$aggregatedFeatures

$aggregatedFeatures 全局表示聚类中包含的所有要素。您可以使用此全局变量来循环访问要素、对其进行过滤以及计算要素的统计数据。

   
1
2
3
// returns the total number of features in the cluster
// this will be the same as $feature.cluster_count
Count($aggregatedFeatures)
  
1
2
// returns the count of features of fuel type 'Coal' within the cluster
Count(Filter($aggregatedFeatures, "fuel1 = 'Coal'"))

Expects Arcade 函数告诉聚类图层请求弹出窗口正常运行所需的额外字段。

                
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)"

若要在弹出窗口中使用从 Arcade 表达式返回的值,必须在 popupTemplate 的 expressionInfos 的属性中设置 Arcade 表达式,然后使用 {expression/expression-name} 语法引用它。

Calculate cluster statistics
107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 107 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 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135
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
148
149
150
151
152
153
154
155
156
157
158
159
160
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="initial-scale=1,maximum-scale=1,user-scalable=no"
    />

    <title>Point clustering - filter popup features | Sample | GeoScene API for JavaScript 4.22</title>

    <link rel="stylesheet" href="https://js.geoscene.cn/4.23/geoscene/themes/light/main.css" />
    <script src="https://js.geoscene.cn/4.23/"></script>

    <style>
      html,
      body,
      #viewDiv {
        height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
    </style>

    <script>
      require([
        "geoscene/WebMap",
        "geoscene/views/MapView",
        "geoscene/layers/FeatureLayer",
        "geoscene/widgets/Legend",
        "geoscene/widgets/Expand",
        "geoscene/smartMapping/labels/clusters"
      ], function (
      ) {
        const layer = new FeatureLayer({
          portalItem: {
            id: "eb54b44c65b846cca12914b87b315169"
        const map = new WebMap({
          basemap: {
            portalItem: {
              id: "75a08e8cd8b64dcfa6945bb7f624ccc5"
          layers: [layer]
        const view = new MapView({
          container: "viewDiv",
          extent: {
            spatialReference: {
              latestWkid: 3857,
              wkid: 102100
            xmin: -15327459,
            ymin: 2740044,
            xmax: -6076744,
            ymax: 6878650
          popup: {
            dockEnabled: true,
            dockOptions: {
              breakpoint: false,
              position: "top-right"
          new Expand({
            content: new Legend({ view }),
          "top-left"
          .then(async (featureReduction) => {
            // sets generated cluster configuration on the layer
            // Disable clustering when user zooms beyond a 1:50,000 scale level
            // Re-enable clustering when the user zooms out to a scale smaller than 1:50,000
            view.watch("scale", function (scale) {
                view.scale > 50000 ? featureReduction : null;
          .catch((error) => {
            console.error(error);
        async function generateClusterConfig(layer) {
          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")})\`;
              `
            }]
          };
          // generates default labelingInfo
          const { labelingInfo, clusterMinSize } = await clusterLabelCreator
            .then((labelSchemes) => labelSchemes.primaryScheme);
          return {
            type: "cluster",
    </script>
  </head>

  <body>
    <div id="viewDiv"></div>
  </body>
</html>

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.