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 (
WebMap,
MapView,
FeatureLayer,
Legend,
Expand,
clusterLabelCreator
) {
const layer = new FeatureLayer({
portalItem: {
id: "eb54b44c65b846cca12914b87b315169"
}
});
const map = new WebMap({
basemap: {
portalItem: {
id: "75a08e8cd8b64dcfa6945bb7f624ccc5"
}
},
layers: [layer]
});
const view = new MapView({
container: "viewDiv",
map,
extent: {
spatialReference: {
latestWkid: 3857,
wkid: 102100
},
xmin: -15327459,
ymin: 2740044,
xmax: -6076744,
ymax: 6878650
},
popup: {
dockEnabled: true,
dockOptions: {
breakpoint: false,
position: "top-right"
}
}
});
view.ui.add(
new Expand({
content: new Legend({ view }),
view
}),
"top-left"
);
layer
.when()
.then(generateClusterConfig)
.then(async (featureReduction) => {
// sets generated cluster configuration on the layer
layer.featureReduction = featureReduction;
// 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) {
layer.featureReduction =
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
.getLabelSchemes({
layer,
view
})
.then((labelSchemes) => labelSchemes.primaryScheme);
return {
type: "cluster",
popupTemplate,
labelingInfo,
clusterMinSize
};
}
});
</script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>