在弹出窗口中汇总相交点

尝试一下在线预览

这个例子演示了如何在 PopupTemplate 中使用 Arcade 表达式 来总结一个图层中与另一个图层中的多边形相交的点。

这个示例中的地图包含两个图层:代表区块组的多边形图层和代表犯罪地点的点图层。每个犯罪都有一个描述犯罪类型的 desc_ 字段。它还有一个 is_night 字段,包含1 (夜间犯罪)或 0 (白天犯罪)。这个应用程序使用一个单一的 Arcade 表达式来完成以下操作,每次用户点击一个功能并打开弹出窗口:

  1. 查询与选定多边形相交的犯罪信息。
  2. 按犯罪类型对这些交叉点进行分组,并返回每个类别中的总计数和平均 is_night  值。
  3. 按计数降序排列组。

这三个步骤反映在下面的表达式中。

                                                
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
<script type="text/arcgis-arcade" id="crimes-arcade">
  // 1. Query the number of crimes that intersect a selected polygon
  var 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 crimes
  var 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 display
  for(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>

GroupBy()  的 Arcade 文档包含有关如何通过单个函数调用在 Arcade 中查询多个统计信息(例如总和、最小值、最大值、平均值、标准差、方差)的附加信息。

在编写了 Arcade 表达式之后,你可以在 JavaScript 中引用它作为字符串值,并将其设置为 popupTemplate 中  expressionInfos  的 expression  属性。

                    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const arcadeScript = document.getElementById("crimes-arcade").text;

const blockGroups = new FeatureLayer({
        title: "U.S. Census Block Groups",
        portalItem: {
          id: "181b322639d44fcba6e37d8b82910daf"
        },
        popupTemplate: {
          title: "Tract: {Tract}, Block Group: {BLKGRP}",
          content: "Top 3 crimes: <br\><br\>" +
            "{expression/top-crimes}",
          expressionInfos: [{
            // the name is used to reference the expression value in the template
            name: "top-crimes",
            title: "Top crimes",
            // the Arcade expression stored as text
            expression: arcadeScript
          }]
        }
      });

有关为弹出窗口编写 Arcade 表达式的更多信息,请参阅 Arcade 表达式指南的 Popup profile在 PopupTemplate 中引用 Arcade 表达式

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