Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for Qt

Statistical query

Sample Viewer View Sample on GitHub

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

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

import QtQuick 2.6
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import Esri.ArcGISRuntime 100.9

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

        // Create a new Map with the world streets vector basemap
        Map {
            BasemapStreetsVector {}

            // 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
            }
        }
    }
}