private async void ExecuteStatisticsQuery(object sender, EventArgs e) { // Verify that there is at least one statistic definition if (!_statisticDefinitions.Any()) { // Warn the user to define a statistic to query ShowMessage("Please define at least one statistic for the query.", "Statistical Query"); return; } // Create the statistics query parameters, pass in the list of statistic definitions StatisticsQueryParameters statQueryParams = new StatisticsQueryParameters(_statisticDefinitions); // Specify the selected group fields (if any) if (_groupByFields != null) { // Find fields in the dictionary with a 'true' value and add them to the group by field names foreach (KeyValuePair <string, bool> groupField in _groupByFields.Where(field => field.Value)) { statQueryParams.GroupByFieldNames.Add(groupField.Key); } } // Specify the fields to order by (if any) if (_orderByFields != null) { foreach (OrderFieldOption orderBy in _orderByFields) { statQueryParams.OrderByFields.Add(orderBy.OrderInfo); } } // Ignore counties with missing data statQueryParams.WhereClause = "\"State\" IS NOT NULL"; try { // Execute the statistical query with these parameters and await the results StatisticsQueryResult statQueryResult = await _usStatesTable.QueryStatisticsAsync(statQueryParams); // Get results formatted as a dictionary (group names and their associated dictionary of results) Dictionary <string, IReadOnlyDictionary <string, object> > resultsLookup = statQueryResult.ToDictionary(r => string.Join(", ", r.Group.Values), r => r.Statistics); // Create an instance of a custom list adapter that has logic to show results as expandable groups ExpandableResultsListAdapter expandableListAdapter = new ExpandableResultsListAdapter(this, resultsLookup); // Create an expandable list view and assign the expandable adapter ExpandableListView expandableResultsListView = new ExpandableListView(this); expandableResultsListView.SetAdapter(expandableListAdapter); // Show the expandable list view in a dialog AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); dialogBuilder.SetView(expandableResultsListView); dialogBuilder.Show(); } catch (Exception ex) { new AlertDialog.Builder(this).SetMessage(ex.ToString()).SetTitle("Error").Show(); } }
// Execute a statistical query using the parameters defined by the user and display the results private async void OnExecuteStatisticsQueryClicked(object sender, RoutedEventArgs e) { // Verify that there is at least one statistic definition if (_statDefinitions.Count == 0) { ShowMessage("Please define at least one statistic for the query.", "Statistical Query"); return; } // Create the statistics query parameters, pass in the list of statistic definitions StatisticsQueryParameters statQueryParams = new StatisticsQueryParameters(_statDefinitions); // Specify the group fields (if any) foreach (string groupField in _groupByFields) { statQueryParams.GroupByFieldNames.Add(groupField); } // Specify the fields to order by (if any) foreach (OrderFieldOption orderBy in _orderByFields) { statQueryParams.OrderByFields.Add(orderBy.OrderInfo); } // Execute the statistical query with these parameters and await the results StatisticsQueryResult statQueryResult = await _usStatesTable.QueryStatisticsAsync(statQueryParams); // Format the output for display of grouped results in the list view IEnumerable <IGrouping <string, IReadOnlyDictionary <string, object> > > groupedResults = statQueryResult.GroupBy(r => string.Join(", ", r.Group.Values), r => r.Statistics); // Apply the results to the list view data source GroupedResultData.Source = groupedResults; }
// Execute a statistical query using the parameters defined by the user and display the results private async void OnExecuteStatisticsQueryClicked(object sender, EventArgs e) { // Verify that there is at least one statistic definition if (!_statDefinitions.Any()) { await((Page)Parent).DisplayAlert("Please define at least one statistic for the query.", "Statistical Query", "OK"); return; } // Create the statistics query parameters, pass in the list of statistic definitions StatisticsQueryParameters statQueryParams = new StatisticsQueryParameters(_statDefinitions); // Specify the group fields (if any) foreach (string groupField in _groupByFields) { statQueryParams.GroupByFieldNames.Add(groupField); } // Specify the fields to order by (if any) foreach (OrderFieldOption orderBy in _orderByFields) { statQueryParams.OrderByFields.Add(orderBy.OrderInfo); } // Execute the statistical query with these parameters and await the results StatisticsQueryResult statQueryResult = await _usStatesTable.QueryStatisticsAsync(statQueryParams); // Get results formatted as a lookup (list of group names and their associated dictionary of results) ILookup <string, IReadOnlyDictionary <string, object> > resultsLookup = statQueryResult.ToLookup(result => string.Join(", ", result.Group.Values), result => result.Statistics); // Loop through the formatted results and build a list of classes to display as grouped results in the list view ObservableCollection <ResultGroup> resultsGroupCollection = new ObservableCollection <ResultGroup>(); foreach (IGrouping <string, IReadOnlyDictionary <string, object> > group in resultsLookup) { // Create a new group ResultGroup resultGroup = new ResultGroup() { GroupName = group.Key }; // Loop through all the results for this group and add them to the collection foreach (IReadOnlyDictionary <string, object> resultSet in group) { foreach (KeyValuePair <string, object> result in resultSet) { resultGroup.Add(new StatisticResult { FieldName = result.Key, StatValue = result.Value }); } } // Add the group of results to the collection resultsGroupCollection.Add(resultGroup); } // Apply the results to the list view data source and show the results grid StatResultsList.ItemsSource = resultsGroupCollection; ResultsGrid.IsVisible = true; }
private async void ExecuteStatisticsQuery(object sender, EventArgs e) { // Remove the placeholder "Add statistic" row (if it exists) StatisticDefinition placeholderRow = _statisticDefinitions.LastOrDefault(); if (placeholderRow != null && placeholderRow.OutputAlias == "") { _statisticDefinitions.Remove(placeholderRow); } // Verify that there is at least one statistic definition if (_statisticDefinitions.Count() == 0) { ShowAlert("Statistical Query", "Please define at least one statistic for the query."); return; } // Create the statistics query parameters, pass in the list of statistic definitions StatisticsQueryParameters statQueryParams = new StatisticsQueryParameters(_statisticDefinitions); // Specify the selected group fields (if any) if (_groupByFields != null) { foreach (KeyValuePair <string, bool> groupField in _groupByFields.Where(field => field.Value == true)) { statQueryParams.GroupByFieldNames.Add(groupField.Key); } } // Specify the fields to order by (if any) if (_orderByFields != null) { foreach (OrderFieldOption orderBy in _orderByFields) { statQueryParams.OrderByFields.Add(orderBy.OrderInfo); } } // Execute the statistical query with these parameters and await the results StatisticsQueryResult statQueryResult = await _usStatesTable.QueryStatisticsAsync(statQueryParams); // Get results formatted as a dictionary (group names and their associated dictionary of results) Dictionary <string, IReadOnlyDictionary <string, object> > resultsLookup = statQueryResult.ToDictionary(result => string.Join(", ", result.Group.Values), result => result.Statistics); // Create an instance of a custom data source to display the results StatisticQueryResultsDataSource statResultsDataSource = new StatisticQueryResultsDataSource(resultsLookup); // Create a new table with a grouped style for displaying rows UITableViewController statResultsTable = new UITableViewController(UITableViewStyle.Grouped); // Set the table view data source statResultsTable.TableView.Source = statResultsDataSource; // Show the table view this.NavigationController.PushViewController(statResultsTable, true); }
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"; } // 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); }
// Execute a statistical query using the parameters defined by the user and display the results private async void OnExecuteStatisticsQueryClicked(object sender, RoutedEventArgs e) { // Verify that there is at least one statistic definition if (_statDefinitions.Count == 0) { MessageBox.Show("Please define at least one statistic for the query.", "Statistical Query"); return; } // Create the statistics query parameters, pass in the list of statistic definitions StatisticsQueryParameters statQueryParams = new StatisticsQueryParameters(_statDefinitions); // Specify the group fields (if any) foreach (string groupField in _groupByFields) { statQueryParams.GroupByFieldNames.Add(groupField); } // Specify the fields to order by (if any) foreach (OrderBy orderBy in _orderByFields) { statQueryParams.OrderByFields.Add(orderBy); } // Ignore counties with missing data statQueryParams.WhereClause = "\"State\" IS NOT NULL"; try { // Execute the statistical query with these parameters and await the results StatisticsQueryResult statQueryResult = await _usStatesTable.QueryStatisticsAsync(statQueryParams); // Format the output, and display results in the tree view ILookup <string, IReadOnlyDictionary <string, object> > groupedResults = statQueryResult.ToLookup(r => string.Join(", ", r.Group.Values), r => r.Statistics); ResultsTreeView.ItemsSource = groupedResults; } catch (Exception ex) { MessageBox.Show(ex.Message, "Invalid statistics definitions"); } }
private async void QueryStatistics(bool onlyInExtent, bool onlyLargePop) { // 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 (onlyInExtent) { // 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 current extent. 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 (onlyLargePop) { // 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); // Get the first (only) StatisticRecord in the results. StatisticRecord record = statQueryResult.FirstOrDefault(); // Make sure a record was returned. if (record == null || record.Statistics.Count == 0) { ShowMessage("No result", "No results were returned."); return; } // Display results. ShowStatsList(record.Statistics); } catch (ArcGISWebException exception) { ShowMessage("There was a problem running the query.", exception.ToString()); } }
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 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.On) { // 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.On) { // Set a where clause to get the largest cities (could also use "POP_CLASS = '5,000,000 and greater'") statQueryParams.WhereClause = "POP_RANK = 1"; } // Execute the statistical query with these parameters and await the results StatisticsQueryResult statQueryResult = await _worldCitiesTable.QueryStatisticsAsync(statQueryParams); // Get the first (only) StatisticRecord in the results StatisticRecord record = statQueryResult.FirstOrDefault(); // Make sure a record was returned if (record == null || record.Statistics.Count == 0) { // Notify the user that no results were returned UIAlertView alert = new UIAlertView(); alert.Message = "No results were returned"; alert.Title = "Statistical Query"; alert.Show(); return; } // Display results IReadOnlyDictionary <string, object> statistics = record.Statistics; ShowStatsList(statistics); }