-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary-statistics.html
265 lines (234 loc) · 7.37 KB
/
summary-statistics.html
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"/>
<title>Arcade profiles - summary statistics by geometry</title>
<style>
html,
body,
#viewDiv{
padding: 0;
margin: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
#instructions {
padding: 10px;
text-align: center;
}
#uiDiv {
padding: 10px;
max-width: 400px;
}
#sliderDiv {
padding: 10px;
width: 350px;
height: 50px;
}
</style>
<!-- ArcGIS JS API -->
<link rel="stylesheet" href="https://js.arcgis.com/4.24/esri/themes/light/main.css"/>
<script src="https://js.arcgis.com/4.24/"></script>
<script>
require([
"esri/views/MapView",
"esri/WebMap",
"esri/widgets/Slider",
"esri/Graphic",
"esri/arcade"
], function (
MapView,
WebMap,
Slider,
Graphic,
arcade
) {
(async () => {
const webmap = new WebMap({
basemap: "gray-vector"
});
const view = new MapView({
map: webmap,
container: "viewDiv",
center: [-117.15176, 32.72355 ],
scale: 36111,
popup: {
dockEnabled: true,
dockOptions: {
breakpoint: false,
position: "top-right"
}
},
constraints: {
snapToZoom: false
}
});
const displayDiv = document.getElementById("displayDiv");
const distanceSlider = new Slider({
min: 0,
max: 1,
steps: 0.05,
visibleElements: {
labels: true
},
precision: 2,
values: [ 0.2 ],
labelFormatFunction: (value, type) => {
return `${value.toString()} mi`;
},
container: "sliderDiv"
});
view.ui.add("uiDiv", "top-right");
await view.when();
const dataProfile = {
variables: [
{
name: "$mapClick",
type: "geometry"
},
{
name: "$bufferProps",
type: "dictionary",
properties: [
{
name: "distance",
type: "number"
},
{
name: "units",
type: "text"
}
]
}
]
};
const arcadeExpression = `
var p = Portal('https://www.arcgis.com');
var features = FeatureSetByPortalItem(p, '03c5236e3d7f496baca4c992f3c2e89c', 0, ["is_night", "type"], true);
var bufferGeometry = BufferGeodetic(
$mapClick,
$bufferProps.distance,
$bufferProps.units
);
var topCrimes = Top(
OrderBy(
GroupBy(
Intersects( features, Generalize(bufferGeometry, 10, true, "meters") ),
"type",
[
{ name: "total", expression: "1", statistic: "count" },
{ name: "nightPercent", expression: "is_night", statistic: "avg" }
]
),
"total desc"),
5
);
if(Count(topCrimes) == 0){
return {
table: "No crimes reported in this area",
buffer: bufferGeometry
};
}
// Build an HTML table to display the summary of
// crimes by count and how many occurred at night
var cssGray = "style='background-color:#e3e3e3;'";
var cssRight = "style='text-align: right;'";
var cssCenter = "style='text-align: center;'";
var table = "<table style='width: 100%'>";
table += \`<tr>
<td \${cssCenter}><b>Category</b></td>
<td \${cssCenter}><b>Count</b></td>
<td \${cssCenter}><b>% at night</b></td>
</tr>\`;
var i = 0;
for(var item in topCrimes){
var num_crimes = Text(item.total, "#,###");
var night_percent = Text(item.nightPercent, "#%");
var crimeType = item["type"];
table += \`<tr \${IIF( i % 2 != 0, cssGray, null )}>
<td>\${crimeType}</td>
<td \${cssRight}>\${num_crimes}</td>
<td \${cssRight}>\${night_percent}</td>
</tr>\`;
i++;
}
table += "</table>";
return {
table: table,
buffer: bufferGeometry
};
`;
// Compile the color variable expression and create an executor
const dataArcadeExecutor = await arcade.createArcadeExecutor(
arcadeExpression,
dataProfile
);
let currentPoint = null;
async function fetchSummaryStatistics({
mapPoint,
distance
}) {
const arcadeResult = await dataArcadeExecutor.executeAsync({
$mapClick: mapPoint,
$bufferProps: {
distance,
units: "miles"
}
}, {
spatialReference: view.spatialReference
});
displayDiv.innerHTML = arcadeResult.table;
addBufferToView(arcadeResult.buffer);
}
view.on("click", async (event) => {
const { mapPoint } = event;
currentPoint = mapPoint;
const distance = distanceSlider.values[0];
document.body.style.cursor = "wait";
await fetchSummaryStatistics({ mapPoint, distance });
document.body.style.cursor = "default";
});
distanceSlider.on(["thumb-drag", "track-click", "thumb-change"], async ({ state, value }) => {
if(state && state !== "stop" || !currentPoint){
return;
}
document.body.style.cursor = "wait";
await fetchSummaryStatistics({ mapPoint: currentPoint, distance: value });
document.body.style.cursor = "default";
});
function addBufferToView(buffer){
view.graphics.removeAll();
view.graphics.add(new Graphic({
attributes: {
OBJECTID: 1
},
geometry: buffer,
symbol: {
type: "simple-fill",
style: "forward-diagonal",
color: "purple",
outline: {
style: "dash",
width: 3,
color: "purple"
}
}
}));
}
})()
});
</script>
</head>
<body>
<div id="viewDiv"></div>
<div id="uiDiv" class="esri-widget">
<div id="instructions">
<h2>Search crime statistics</h2>
<p>Click any location in the San Diego area to view crime statistics within a distance specified on the slider.</p>
<div id="sliderDiv"></div>
</div>
<div id="displayDiv" class="esri-widget"></div>
</div>
</body>
</html>