Click or drag to resize

StatisticsQueryParameters Class

Represents a type used for defining a statistics query.
Inheritance Hierarchy
SystemObject
  Esri.ArcGISRuntime.DataStatisticsQueryParameters

Namespace:  Esri.ArcGISRuntime.Data
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.7.0.0
Syntax
C#
public class StatisticsQueryParameters

The StatisticsQueryParameters type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleStatisticsQueryParameters
Initializes a new instance of the StatisticsQueryParameters class.
Top
Properties
  NameDescription
Public propertyCode exampleGeometry
Gets or sets the Geometry used to filter the results.
Public propertyCode exampleGroupByFieldNames
Gets the fields by which statistical query results will be grouped.
Public propertyCode exampleOrderByFields
Gets the fields by which statistical query results will be ordered.
Public propertyCode exampleSpatialRelationship
Gets or sets the SpatialRelationship that (along with the Geometry) defines features to be included in the statistical query.
Public propertyStatisticDefinitions
Gets a list of statistic definitions (input field, output name, statistic type) to include in a statistical query of a table.
Public propertyTimeExtent
Gets or sets the TimeExtent of the query
Public propertyCode exampleWhereClause
Gets or sets the attribute expression that defines features to be included in the statistics query.
Top
Examples

Android

Example Name: StatisticalQuery

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

Code example screen shot.

C#
// 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.

using Android.App;
using Android.OS;
using Android.Widget;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ArcGISRuntime.Samples.StatisticalQuery
{
    [Activity (ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Statistical query",
        "Data",
        "This sample demonstrates how to query a feature table to get statistics for a specified field.",
        "Check the appropriate boxes to filter features by attributes and/or within the current extent. Click the button to see basic statistics displayed for world cities.")]
    public class StatisticalQuery : Activity
    {
        // Hold a reference to the map view
        private MapView _myMapView;

        // URI for the world cities map service layer
        private Uri _worldCitiesServiceUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer/0");

        // World cities feature table
        private FeatureTable _worldCitiesTable;

        // Linear layout UI control for arranging query controls
        private LinearLayout _controlsLayout;

        // UI controls (switches) that will need to be referenced
        private Switch _onlyInExtentSwitch;
        private Switch _onlyBigCitiesSwitch;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Title = "Statistical query";

            // Create the UI
            CreateLayout();

            // Initialize the map and layers
            Initialize();
        }

        private void Initialize()
        {
            // Create a new Map with the world streets basemap
            Map myMap = new Map(Basemap.CreateStreets());

            // Create feature table using the world cities URI
            _worldCitiesTable = new ServiceFeatureTable(_worldCitiesServiceUri);

            // Create a new feature layer to display features in the world cities table
            FeatureLayer worldCitiesLayer = new FeatureLayer(_worldCitiesTable);

            // Add the world cities layer to the map
            myMap.OperationalLayers.Add(worldCitiesLayer);

            // Assign the map to the MapView
            _myMapView.Map = myMap;
        }

        private async void OnExecuteStatisticsQueryClicked(object sender, EventArgs e)
        {
            // Create definitions for each statistic to calculate
            StatisticDefinition statDefinitionAvgPop = new StatisticDefinition("POP", StatisticType.Average, "");
            StatisticDefinition statDefinitionMinPop = new StatisticDefinition("POP", StatisticType.Minimum, "");
            StatisticDefinition statDefinitionMaxPop = new StatisticDefinition("POP", StatisticType.Maximum, "");
            StatisticDefinition statDefinitionSumPop = new StatisticDefinition("POP", StatisticType.Sum, "");
            StatisticDefinition statDefinitionStdDevPop = new StatisticDefinition("POP", StatisticType.StandardDeviation, "");
            StatisticDefinition statDefinitionVarPop = new StatisticDefinition("POP", StatisticType.Variance, "");

            // Create a definition for count that includes an alias for the output
            StatisticDefinition statDefinitionCount = new StatisticDefinition("POP", StatisticType.Count, "CityCount");

            // Add the statistics definitions to a list
            List<StatisticDefinition> statDefinitions = new List<StatisticDefinition>
                          { statDefinitionAvgPop,
                            statDefinitionCount,
                            statDefinitionMinPop,
                            statDefinitionMaxPop,
                            statDefinitionSumPop,
                            statDefinitionStdDevPop,
                            statDefinitionVarPop
                          };

            // Create the statistics query parameters, pass in the list of definitions
            StatisticsQueryParameters statQueryParams = new StatisticsQueryParameters(statDefinitions);

            // If only using features in the current extent, set up the spatial filter for the statistics query parameters
            if (_onlyInExtentSwitch.Checked)
            {
                // Get the current extent (envelope) from the map view
                Envelope currentExtent = _myMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry as Envelope;

                // Set the statistics query parameters geometry with the envelope
                statQueryParams.Geometry = currentExtent;

                // Set the spatial relationship to Intersects (which is the default)
                statQueryParams.SpatialRelationship = SpatialRelationship.Intersects;
            }

            // If only evaluating the largest cities (over 5 million in population), set up an attribute filter
            if (_onlyBigCitiesSwitch.Checked)
            {
                // Set a where clause to get the largest cities (could also use "POP_CLASS = '5,000,000 and greater'")
                statQueryParams.WhereClause = "POP_RANK = 1";
            }

            try
            {
                // Execute the statistical query with these parameters and await the results
                StatisticsQueryResult statQueryResult = await _worldCitiesTable.QueryStatisticsAsync(statQueryParams);

                // Display results in a list in a dialog
                List<KeyValuePair<string,object>> statsList = statQueryResult.First().Statistics.ToList();
                ShowStatsList(statsList);
            }
            catch (Exception ex)
            {
                new AlertDialog.Builder(this).SetMessage(ex.Message).SetTitle("Error").Show();
            }
        }

        private void ShowStatsList(IList<KeyValuePair<string, object>> stats)
        {
            // Create a list of statistics results (field names and values) to show in the list
            IList<string> statInfoList = new List<string>();
            foreach (KeyValuePair<string,object> kvp in stats)
            {
                // If the value is null, display "--"
                string displayString = "--";

                if (kvp.Value != null)
                {
                    displayString = kvp.Value.ToString();
                }

                statInfoList.Add(kvp.Key + " : " + displayString);
            }

            // Create an array adapter for the stats list
            ArrayAdapter<string> statsArrayAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, statInfoList);
            statsArrayAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleListItem1);

            // Create a list view to display the results
            ListView statsListView = new ListView(this)
            {
                Adapter = statsArrayAdapter
            };

            // Show the list view in a dialog
            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
            dialogBuilder.SetView(statsListView);
            dialogBuilder.Show();
        }

        private void CreateLayout()
        {
            // Create a new vertical layout for the app
            _controlsLayout = new LinearLayout(this) { Orientation = Orientation.Vertical };

            // Create switches for controlling which features are included in the query
            _onlyBigCitiesSwitch = new Switch(this)
            {
                Text = "Only cities over 5M"
            };
            _onlyInExtentSwitch = new Switch(this)
            {
                Text = "Only in current extent"
            };

            // Create a Button to execute the statistical query
            Button getStatsButton = new Button(this)
            {
                Text = "Get Statistics"
            };
            getStatsButton.Click += OnExecuteStatisticsQueryClicked;

            // Add the query controls to the layout
            _controlsLayout.AddView(_onlyBigCitiesSwitch);
            _controlsLayout.AddView(_onlyInExtentSwitch);
            _controlsLayout.AddView(getStatsButton);

            // Add the map view to the layout
            _myMapView = new MapView(this);
            _controlsLayout.AddView(_myMapView);

            // Show the layout in the app
            SetContentView(_controlsLayout);
        }
    }
}
Examples

Xamarin Forms Android

Example Name: StatisticalQuery

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

Code example screen shot.

C#
// 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.

using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;

namespace ArcGISRuntime.Samples.StatisticalQuery
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Statistical query",
        "Data",
        "This sample demonstrates how to query a feature table to get statistics for a specified field.",
        "Check the appropriate boxes to filter features by attributes and/or within the current extent. Click the button to see basic statistics displayed for world cities.")]
    public partial class StatisticalQuery : ContentPage
    {
        // URI for the world cities map service layer
        private Uri _worldCitiesServiceUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/SampleWorldCities/MapServer/0");

        // World cities feature table
        private FeatureTable _worldCitiesTable;

        public StatisticalQuery()
        {
            InitializeComponent();

            // Initialize the base map and world cities feature layer
            Initialize();
        }

        private void Initialize()
        {
            // Create a new Map with the world streets vector basemap
            Map myMap = new Map(Basemap.CreateStreets());

            // Create feature table using the world cities URI
            _worldCitiesTable = new ServiceFeatureTable(_worldCitiesServiceUri);

            // Create a new feature layer to display features in the world cities table
            FeatureLayer worldCitiesLayer = new FeatureLayer(_worldCitiesTable);

            // Add the world cities layer to the map
            myMap.OperationalLayers.Add(worldCitiesLayer);

            // Assign the map to the MapView
            MyMapView.Map = myMap;
        }

        private async void OnExecuteStatisticsQueryClicked(object sender, EventArgs e)
        {
            // Create definitions for each statistic to calculate
            StatisticDefinition statDefinitionAvgPop = new StatisticDefinition("POP", StatisticType.Average, "");
            StatisticDefinition statDefinitionMinPop = new StatisticDefinition("POP", StatisticType.Minimum, "");
            StatisticDefinition statDefinitionMaxPop = new StatisticDefinition("POP", StatisticType.Maximum, "");
            StatisticDefinition statDefinitionSumPop = new StatisticDefinition("POP", StatisticType.Sum, "");
            StatisticDefinition statDefinitionStdDevPop = new StatisticDefinition("POP", StatisticType.StandardDeviation, "");
            StatisticDefinition statDefinitionVarPop = new StatisticDefinition("POP", StatisticType.Variance, "");

            // Create a definition for count that includes an alias for the output
            StatisticDefinition statDefinitionCount = new StatisticDefinition("POP", StatisticType.Count, "CityCount");

            // Add the statistics definitions to a list
            List<StatisticDefinition> statDefinitions = new List<StatisticDefinition>
                          { statDefinitionAvgPop,
                            statDefinitionCount,
                            statDefinitionMinPop,
                            statDefinitionMaxPop,
                            statDefinitionSumPop,
                            statDefinitionStdDevPop,
                            statDefinitionVarPop
                          };

            // Create the statistics query parameters, pass in the list of definitions
            StatisticsQueryParameters statQueryParams = new StatisticsQueryParameters(statDefinitions);

            // If only using features in the current extent, set up the spatial filter for the statistics query parameters
            if (OnlyInExtentSwitch.IsToggled)
            {
                // Get the current extent (envelope) from the map view
                Envelope currentExtent = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry as Envelope;

                // Set the statistics query parameters geometry with the envelope
                statQueryParams.Geometry = currentExtent;

                // Set the spatial relationship to Intersects (which is the default)
                statQueryParams.SpatialRelationship = SpatialRelationship.Intersects;
            }

            // If only evaluating the largest cities (over 5 million in population), set up an attribute filter
            if (OnlyBigCitiesSwitch.IsToggled)
            {
                // Set a where clause to get the largest cities (could also use "POP_CLASS = '5,000,000 and greater'")
                statQueryParams.WhereClause = "POP_RANK = 1";
            }

            try
            {
                // Execute the statistical query with these parameters and await the results
                StatisticsQueryResult statQueryResult = await _worldCitiesTable.QueryStatisticsAsync(statQueryParams);

                // Display results in the list box
                StatResultsList.ItemsSource = statQueryResult.First().Statistics.Select(m =>$"{m.Key}:{m.Value}").ToList();
                ResultsGrid.IsVisible = true;
            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
            }
        }

        private void HideResults(object sender, EventArgs e)
        {
            ResultsGrid.IsVisible = false;
        }
    }
}
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"             
             xmlns:esriUI="clr-namespace:Esri.ArcGISRuntime.Xamarin.Forms;assembly=Esri.ArcGISRuntime.Xamarin.Forms"
             xmlns:mapping="clr-namespace:Esri.ArcGISRuntime.Mapping;assembly=Esri.ArcGISRuntime" 
             x:Class="ArcGISRuntime.Samples.StatisticalQuery.StatisticalQuery">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="auto" />
      <RowDefinition />
    </Grid.RowDefinitions>
        <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <Switch x:Name="OnlyInExtentSwitch"
               Grid.Row="0" Grid.Column="0"
               HorizontalOptions="End"/>
        <Label Grid.Row="0" Grid.Column="1"
               Margin="10,0"
               VerticalOptions="Center"            
               Text="Cities in current extent"/>
        <Switch x:Name="OnlyBigCitiesSwitch"
               Grid.Row="1" Grid.Column="0"
               HorizontalOptions="End"/>
        <Label Grid.Row="1" Grid.Column="1"
               Margin="10,0"
               VerticalOptions="Center"
               Text="Cities larger than 5M"/>
        <Button x:Name="GetStatsButton"
               Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
               Margin="5,0"
               Text="Get Statistics"
               Clicked="OnExecuteStatisticsQueryClicked"/>
        </Grid>

        <esriUI:MapView x:Name="MyMapView"
                Grid.Row="1"/>
        <Grid x:Name="ResultsGrid"
               Grid.Row="1"
               BackgroundColor="Gainsboro"
               IsVisible="False">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="50"/>
            </Grid.RowDefinitions>
            <ListView x:Name="StatResultsList"/>
            <Button Text="Dismiss"
                    Grid.Row="1"
                    Margin="5"
                    Clicked="HideResults"/>
        </Grid>
  </Grid>
</ContentPage>
See Also
Additional Examples
Hyperlink to ExampleDescription
StatisticalQueryQuery a table to get aggregated statistics back for a specific field.
StatsQueryGroupAndSortQuery a feature table for statistics, grouping and sorting by different fields.