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
<scripttype="text/arcgis-arcade"id="crimes-arcade">// 1. Query the number of crimes that intersect a selected polygonvar crimes = Intersects(
$feature,
FeatureSetByName($map,"San Diego crimes", ["desc_", "is_night"])
);
// 2. Group the intersecting points by crime type and return the total count// and average `is_night` value within each category of crimesvar stats = GroupBy(crimes, ["desc_"],
[ { name: "total", expression: "1", statistic: "count" },
{ name: "night_avg", expression: "is_night", statistic: "avg" }
]
);
// 3. Sort the crime types in descending order by count.var topCrimes = Top(OrderBy(Filter(stats, "desc_ <> ''"), "total desc"), 3);
var output = "";
if(Count(topCrimes) == 0){
return"No crimes committed in this area";
}
var num = 0;
// Format the results for displayfor(var item in topCrimes){
num++;
var num_crimes = item.total;
var crimeType = item["desc_"];
// The isNight field has values of either 1 or 0.// If the average value (night_avg) is high, then most crimes// occurred at night. If the average is low, then// the crimes typically occurred during daytime hours.var timeOfDay = When(
item.night_avg >= 0.6, "at night",
item.night_avg <= 0.4, " during the daytime hours",
" at both night and day");
// Display crime type with count using template literals output += `${num}. ${crimeType} -- Total offenses: ${Text(num_crimes, "#,###")} -- Most crimes were reported ${timeOfDay} `;
}
return output;
</script>