Statistical query

View inC++QMLView on GitHubSample viewer app

Query a table to get aggregated statistics back for a specific field.

screenshot

Use case

For example, a county boundaries table with population information can be queried to return aggregated results for total, average, maximum, and minimum population, rather than downloading the values for every county and calculating statistics manually.

How to use the sample

Pan and zoom to define the extent for the query. Use the 'Cities in current extent' checkbox to control whether the query only includes features in the visible extent. Use the 'Cities grater than 5M' checkbox to filter the results to only those cities with a population greater than 5 million people. Click 'Get statistics' to perform the query. The query will return population-based statistics from the combined results of all features matching the query criteria.

How it works

  1. Create a ServiceFeatureTable with a URL to the feature service.
  2. Create StatisticsQueryParameters, and StatisticDefinition objects, and add to the parameters.
  3. Execute queryStatistics on the ServiceFeatureTable. Depending on the state of the two checkboxes, additional parameters are set.
  4. Display each StatisticRecord in the first returned QueryStatisticsResult.

Relevant API

  • FeatureLayer
  • QueryParameters
  • ServiceFeatureTable
  • StatisticDefinition
  • StatisticRecord
  • StatisticsQueryParameters
  • StatisticsQueryResult
  • StatisticType

Tags

analysis, average, bounding geometry, filter, intersect, maximum, mean, minimum, query, spatial query, standard deviation, statistics, sum, variance

Sample Code

StatisticalQuery.qml
Use dark colors for code blocksCopy
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
// [WriteFile Name=StatisticalQuery, Category=Analysis]
// [Legal]
// Copyright 2017 Esri.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Esri.ArcGISRuntime

Rectangle {
    clip: true
    width: 800
    height: 600

    readonly property url worldCitiesUrl: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer/0"
    readonly property alias extentOnly: extentCheckbox.checked
    readonly property alias bigCitiesOnly: bigCitiesCheckbox.checked
    property string resultsText: ""

    MapView {
        id: mapView
        anchors.fill: parent

        Component.onCompleted: {
            // Set the focus on MapView to initially enable keyboard navigation
            forceActiveFocus();
        }

        // Create a new Map with the world streets vector basemap
        Map {
            Basemap {
                initStyle: Enums.BasemapStyleArcGISStreets
            }

            // Create a new feature layer to display features in the world cities table
            FeatureLayer {
                // Create feature table using the world cities URL
                ServiceFeatureTable {
                    id: featureTable
                    url: worldCitiesUrl

                    // Setup signal handler for queryStatisticsStatusChanged signal
                    onQueryStatisticsStatusChanged: {
                        if (queryStatisticsStatus !== Enums.TaskStatusCompleted)
                            return;

                        if (!queryStatisticsResult)
                            return;

                        // Reset the result text
                        resultsText = "";

                        // Iterate through the results
                        const iter = queryStatisticsResult.iterator;
                        while (iter.hasNext) {
                            const record = iter.next();
                            for (let key in record.statistics) {
                                if (record.statistics.hasOwnProperty(key)) {
                                    resultsText += key + ": " + record.statistics[key] + "\n";
                                }
                            }
                        }

                        // Display the results
                        msgDialog.open();
                    }

                    onErrorChanged: {
                        resultsText = error.message;
                        msgDialog.open();
                    }
                }
            }
        }
    }

    Rectangle {
        anchors {
            fill: controlColumn
            margins: -10
        }
        color: "#E9E9EA"
        radius: 3
        opacity: 0.8
        border {
            color: "black"
            width: 1
        }
    }

    Column {
        id: controlColumn
        anchors {
            left: parent.left
            top: parent.top
            margins: 15
        }
        spacing: 5

        CheckBox {
            id: extentCheckbox
            text: "Only cities in current extent"
        }

        CheckBox {
            id: bigCitiesCheckbox
            text: "Only cities greater than 5M"
        }

        Button {
            anchors.horizontalCenter: parent.horizontalCenter
            text: "Get Statistics"
            onClicked: {
                // If only using features in the current extent, set up the spatial filter for the statistics query parameters
                if (extentOnly) {
                    // Set the statistics query parameters geometry with the envelope
                    queryParameters.geometry = mapView.currentViewpointExtent.extent;

                    // Set the spatial relationship to Intersects (which is the default)
                    queryParameters.spatialRelationship = Enums.SpatialRelationshipIntersects;
                } else {
                    // Reset the query parameters
                    queryParameters.geometry = null;
                }

                // If only evaluating the largest cities (over 5 million in population), set up an attribute filter
                if (bigCitiesOnly)
                    queryParameters.whereClause = "POP_RANK = 1";
                else
                    queryParameters.whereClause = "1=1";

                // Execute the statistical query with these parameters
                featureTable.queryStatistics(queryParameters);
            }
        }
    }

    // Declare a StatisticsQueryParameters object
    StatisticsQueryParameters {
        id: queryParameters

        // Add the Statistic Definitions
        StatisticDefinition { onFieldName: "POP"; statisticType: Enums.StatisticTypeAverage}
        StatisticDefinition { onFieldName: "POP"; statisticType: Enums.StatisticTypeMinimum}
        StatisticDefinition { onFieldName: "POP"; statisticType: Enums.StatisticTypeMaximum}
        StatisticDefinition { onFieldName: "POP"; statisticType: Enums.StatisticTypeSum}
        StatisticDefinition { onFieldName: "POP"; statisticType: Enums.StatisticTypeStandardDeviation}
        StatisticDefinition { onFieldName: "POP"; statisticType: Enums.StatisticTypeVariance}
        StatisticDefinition { onFieldName: "POP"; statisticType: Enums.StatisticTypeCount; outputAlias: "CityCount"}
    }

    // Create a dialog to display the result
    Dialog {
        id: msgDialog
        modal: true
        x: Math.round(parent.width - width) / 2
        y: Math.round(parent.height - height) / 2
        standardButtons: Dialog.Ok
        property alias text : textLabel.text
        property alias informativeText : detailsLabel.text
        ColumnLayout {
            Text {
                id: textLabel
                text: "Query Statistics Results:"
            }
            Text {
                id: detailsLabel
                text: resultsText
            }
        }
    }
}

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