Statistical query

Loading

Code

#include "StatisticalQuery.h"

#include "Map.h"
#include "MapQuickView.h"
#include "ArcGISVectorTiledLayer.h"
#include "FeatureLayer.h"
#include "ServiceFeatureTable.h"
#include "StatisticalQuery.h"
#include "StatisticsQueryParameters.h"
#include "StatisticDefinition.h"
#include "StatisticRecord.h"
#include "StatisticRecordIterator.h"
#include "Viewpoint.h"

#include <QUrl>

using namespace Esri::ArcGISRuntime;

StatisticalQuery::StatisticalQuery(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent)
{
}

void StatisticalQuery::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<StatisticalQuery>("Esri.Samples", 1, 0, "StatisticalQuerySample");
}

void StatisticalQuery::componentComplete()
{
  QQuickItem::componentComplete();

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");

  // Create a new Map with the world streets vector basemap
  m_map = new Map(Basemap::streetsVector(this), this);

  // Create feature table using the world cities URL
  m_featureTable = new ServiceFeatureTable(QUrl("https://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer/0"), this);

  // Create a new feature layer to display features in the world cities table
  FeatureLayer* featureLayer = new FeatureLayer(m_featureTable, this);
  m_map->operationalLayers()->append(featureLayer);

  // Set map to map view
  m_mapView->setMap(m_map);

  // connect to queryStatisticsCompleted
  connect(m_featureTable, &ServiceFeatureTable::queryStatisticsCompleted, this, [this](QUuid, StatisticsQueryResult* result)
  {
    // Iterate through the results
    QObject parent;
    QString resultText;
    StatisticRecordIterator iter = result->iterator();
    while (iter.hasNext())
    {
      StatisticRecord* record = iter.next(&parent);
      for (auto key : record->statistics().keys())
      {
        resultText += QString("%1: %2\n").arg(key, record->statistics().value(key).toString());
      }
    }

    // Display the Results
    emit showStatistics(resultText);
  });
}

void StatisticalQuery::queryStatistics(bool extentOnly, bool bigCitiesOnly)
{
  // create the parameters
  StatisticsQueryParameters queryParameters;

  // Add the Statistic Definitions
  QList<StatisticDefinition> definitions
  {
    StatisticDefinition("POP", StatisticType::Average, ""),
    StatisticDefinition("POP", StatisticType::Minimum, ""),
    StatisticDefinition("POP", StatisticType::Maximum, ""),
    StatisticDefinition("POP", StatisticType::Sum, ""),
    StatisticDefinition("POP", StatisticType::StandardDeviation, ""),
    StatisticDefinition("POP", StatisticType::Variance, ""),
    StatisticDefinition("POP", StatisticType::Count, "CityCount")
  };
  queryParameters.setStatisticDefinitions(definitions);

  // 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.setGeometry(m_mapView->currentViewpoint(ViewpointType::BoundingGeometry).targetGeometry());

    // Set the spatial relationship to Intersects (which is the default)
    queryParameters.setSpatialRelationship(SpatialRelationship::Intersects);
  }

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

  // Execute the statistical query with these parameters
  m_featureTable->queryStatistics(queryParameters);
}


In this topic
  1. Code