Query a feature table for statistics, grouping and sorting by different fields.
Use case
You can use statistical queries, grouping and sorting to process large amounts of data saved in feature tables. This is helpful for identifying trends and relationships within the data, which can be used to support further interpretations and decisions. For example, a health agency can use information on medical conditions occurring throughout a country to identify at-risk areas or demographics, and decide on further action and preventive measures.
How to use the sample
The sample will start with some default options selected. You can immediately click the "Get Statistics" button to see the results for these options. There are several ways to customize your queries:
- You can add statistic definitions to the top-left table using the combo boxes and "Add" button. Select a table row and click "Remove" to remove a definition.
- To change the Group-by fields, check the box by the field you want to group by in the bottom-left list view.
- To change the Order-by fields, select a Group-by field (it must be checked) and click the ">>" button to add it to the Order-by table. To remove a field from the Order-by table, select it and click the "<<" button. To change the sort order of the Order-by field, the cells of the "Sort Order" column are combo-boxes that may be either ASCENDING or DESCENDING.
How it works
- Create a
ServiceFeatureTable
using the URL of a feature service and load the table. - Get the feature tables field names list with
featureTable::getFields()
. - Create
StatisticDefinition
s specifying the field to compute statistics on and theStatisticType
to compute. - Create
StatisticsQueryParameters
passing in the list of statistic definitions. - To have the results grouped by fields, add the field names to the query parameters'
groupByFieldNames
collection. - To have the results ordered by fields, create
OrderBy
s, specifying the field name andSortOrder
. Pass theseOrderBy
s to the parameters'orderByFields
collection. - To execute the query, call
featureTable::queryStatistics(queryParameters)
. - Get the
StatisticQueryResult
. From this, you can get an iterator ofStatisticRecord
s to loop through and display.
Relevant API
- Field
- OrderBy
- QueryParameters
- ServiceFeatureTable
- StatisticDefinition
- StatisticRecord
- StatisticsQueryParameters
- StatisticsQueryResult
- StatisticType
About the data
This sample uses a Diabetes, Obesity, and Inactivity by US County feature layer hosted on ArcGIS Online.
Tags
correlation, data, fields, filter, group, sort, statistics, table
Sample Code
// [WriteFile Name=StatisticalQueryGroupSort, 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]
#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD
#include "StatisticalQueryGroupSort.h"
#include "ServiceFeatureTable.h"
#include "StatisticDefinition.h"
#include "StatisticRecord.h"
#include "StatisticRecordIterator.h"
#include "StatisticsQueryParameters.h"
#include "StatisticsQueryResult.h"
#include "Field.h"
#include "StatisticResultListModel.h"
#include <QStringList>
#include <QVariantList>
#include <QList>
#include <memory>
using namespace Esri::ArcGISRuntime;
StatisticalQueryGroupSort::StatisticalQueryGroupSort(QQuickItem* parent /* = nullptr */):
QQuickItem(parent),
m_resultsModel(new StatisticResultListModel(this))
{
}
void StatisticalQueryGroupSort::init()
{
qmlRegisterType<StatisticalQueryGroupSort>("Esri.Samples", 1, 0, "StatisticalQueryGroupSortSample");
}
void StatisticalQueryGroupSort::componentComplete()
{
QQuickItem::componentComplete();
// Create the Service Feature Table
m_featureTable = new ServiceFeatureTable(QUrl("https://services.arcgis.com/jIL9msH9OI208GCb/arcgis/rest/services/Counties_Obesity_Inactivity_Diabetes_2013/FeatureServer/0"), this);
connectSignals();
m_featureTable->load();
// Setup default values for the Options page
addStatisticDefinition("Diabetes_Percent", "Average");
addStatisticDefinition("Diabetes_Percent", "Count");
addStatisticDefinition("Diabetes_Percent", "Standard Deviation");
addOrderBy("State", "Ascending");
m_statisticTypes << "Average" << "Count" << "Maximum" << "Minimum"
<< "Standard Deviation" << "Sum" << "Variance";
emit statisticTypesChanged();
}
void StatisticalQueryGroupSort::connectSignals()
{
connect(m_featureTable, &ServiceFeatureTable::doneLoading, this, [this](Error error)
{
if (!error.isEmpty())
return;
const auto fields = m_featureTable->fields();
for (const Field& field : fields)
{
m_fields << field.name();
}
emit fieldsChanged();
});
connect(m_featureTable, &ServiceFeatureTable::errorOccurred, this, [this](Error error)
{
if (error.isEmpty())
return;
m_resultsModel->clear();
addResultToModel("", QString("Error. %1").arg(error.message()));
});
connect(m_featureTable, &ServiceFeatureTable::queryStatisticsCompleted, this, [this](QUuid, StatisticsQueryResult* rawResult)
{
// Delete rawResult when we leave local scope.
auto result = std::unique_ptr<StatisticsQueryResult>(rawResult);
if (!result)
return;
// clear previous results
m_resultsModel->clear();
// iterate the results and add to a model
StatisticRecordIterator iter = result->iterator();
while (iter.hasNext())
{
// get the statistic record
StatisticRecord* record = iter.next();
// get the group string
QStringList sectionStrings;
const QVariantMap& groupsMap = record->group();
for (auto it = groupsMap.cbegin(); it != groupsMap.cend(); ++it)
{
sectionStrings << QString("\"%1\":\"%2\"").arg(it.key(), it.value().toString());
}
const QString sectionString = sectionStrings.join(',');
// obtain the statistics
const QVariantMap& statsMap = record->statistics();
for (auto it = statsMap.cbegin(); it != statsMap.cend(); ++it)
{
const QString statString = QString("%1: %2").arg(it.key(), it.value().toString());
addResultToModel(sectionString, statString);
}
}
});
}
void StatisticalQueryGroupSort::queryStatistics()
{
// create the parameter object
StatisticsQueryParameters params;
// add the statistic definitions
QList<StatisticDefinition> statisticDefinitionList;
for (const QVariant& def : qAsConst(m_statisticDefinitions))
{
QVariantMap definitionMap = def.toMap();
StatisticDefinition statisticDefinition;
statisticDefinition.setOnFieldName(definitionMap["field"].toString());
statisticDefinition.setStatisticType(statisticStringToEnum(definitionMap["statistic"].toString()));
statisticDefinitionList.append(statisticDefinition);
}
params.setStatisticDefinitions(statisticDefinitionList);
// set the grouping fields
params.setGroupByFieldNames(m_groupingFields);
// add the order by objects
QList<OrderBy> orderByList;
for (const QVariant& ob : qAsConst(m_orderBys))
{
QVariantMap orderByMap = ob.toMap();
OrderBy orderBy;
orderBy.setFieldName(orderByMap["field"].toString());
orderBy.setSortOrder(orderStringToEnum(orderByMap["order"].toString()));
orderByList.append(orderBy);
}
params.setOrderByFields(orderByList);
// ignore counties with missing data
params.setWhereClause("\"State\" IS NOT NULL");
// execute the query
m_featureTable->queryStatistics(params);
}
void StatisticalQueryGroupSort::addStatisticDefinition(const QString& field, const QString& statistic)
{
// only add if the definition does not already exist
for (const QVariant& def : qAsConst(m_statisticDefinitions))
{
QVariantMap definitionMap = def.toMap();
if (field == definitionMap["field"].toString())
{
if (statistic == definitionMap["statistic"].toString())
return;
}
}
// add the definition to the list
QVariantMap statisticDefinition;
statisticDefinition["field"] = field;
statisticDefinition["statistic"] = statistic;
m_statisticDefinitions.append(statisticDefinition);
emit statisticDefinitionsChanged();
}
void StatisticalQueryGroupSort::removeStatisticDefinition(int index)
{
m_statisticDefinitions.removeAt(index);
emit statisticDefinitionsChanged();
}
void StatisticalQueryGroupSort::addOrderBy(const QString& field, const QString& order)
{
// only add if the orderby is not in the list
for (const QVariant& orderBy : qAsConst(m_orderBys))
{
QVariantMap orderByMap = orderBy.toMap();
if (field == orderByMap["field"].toString())
{
if (order == orderByMap["order"].toString())
{
emit orderBysChanged();
return;
}
}
}
// add the order by to the list
QVariantMap orderBy;
orderBy["field"] = field;
orderBy["order"] = order;
m_orderBys.append(orderBy);
emit orderBysChanged();
}
void StatisticalQueryGroupSort::removeOrderBy(const QString& field)
{
// remove the order by if it exists in the list
for (int i = 0; i < m_orderBys.size(); i++)
{
QVariantMap orderByMap = m_orderBys.at(i).toMap();
if (field == orderByMap["field"].toString())
{
m_orderBys.removeAt(i);
emit orderBysChanged();
return;
}
}
}
void StatisticalQueryGroupSort::removeOrderBy(int index)
{
// remove the order by at a given index
m_orderBys.removeAt(index);
emit orderBysChanged();
}
void StatisticalQueryGroupSort::updateOrder(int index)
{
// get the existing values
QVariantMap orderByMap;
orderByMap["field"] = m_orderBys.at(index).toMap()["field"].toString();
const QString currentOrder = m_orderBys.at(index).toMap()["order"].toString();
// update the order text
if (currentOrder == "Ascending")
orderByMap["order"] = "Descending";
else
orderByMap["order"] = "Ascending";
// update the variant list with the new value
m_orderBys.removeAt(index);
m_orderBys.insert(index, orderByMap);
emit orderBysChanged();
}
void StatisticalQueryGroupSort::addGroupingField(const QString& field)
{
// add a new grouping field
if (!m_groupingFields.contains(field))
{
m_groupingFields.append(field);
emit groupingFieldsChanged();
}
}
void StatisticalQueryGroupSort::removeGroupingField(const QString& field)
{
// remove a grouping field
if (m_groupingFields.contains(field))
{
int i = m_groupingFields.indexOf(field);
m_groupingFields.removeAt(i);
emit groupingFieldsChanged();
// also update the orderby list
removeOrderBy(field);
}
}
// helper to convert from statistic type string to enum
StatisticType StatisticalQueryGroupSort::statisticStringToEnum(const QString& statistic) const
{
if (statistic == "Average")
return StatisticType::Average;
else if (statistic == "Count")
return StatisticType::Count;
else if (statistic == "Maximum")
return StatisticType::Maximum;
else if (statistic == "Minimum")
return StatisticType::Minimum;
else if (statistic == "Standard Deviation")
return StatisticType::StandardDeviation;
else if (statistic == "Sum")
return StatisticType::Sum;
else
return StatisticType::Variance;
}
// helper to convert from sort order string to enum
SortOrder StatisticalQueryGroupSort::orderStringToEnum(const QString& order) const
{
if (order == "Ascending")
return SortOrder::Ascending;
else
return SortOrder::Descending;
}
void StatisticalQueryGroupSort::addResultToModel(const QString& section, const QString& resultString)
{
m_resultsModel->addStatisticResult(section, resultString);
emit resultsModelChanged();
}
QAbstractListModel* StatisticalQueryGroupSort::resultsModel() const
{
return m_resultsModel;
}