Select to view content in your preferred language

Populating DataGrid from quering online image services

1227
2
Jump to solution
03-10-2021 09:39 PM
ManishShrivastav
Occasional Contributor

Hi,

I am trying to develop a WPF app that can populate a datagrid by querying an online image service. http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer  Particularly, I want to search for a name from the sublayers (search for a city, county, or state) and populate the result in a datagrid. I am attaching an image of the result that I want to have and an image of error message that I got. Also I am attaching the code file.

please help

 

using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Tasks;
using Esri.ArcGISRuntime.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MapApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Map myMap = new Map(Basemap.CreateImagery());
            ArcGISMapImageLayer layer = new ArcGISMapImageLayer(new Uri("http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer"));
            myMap.OperationalLayers.Add(layer);
            MyMapView.Map = myMap; 
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            ArcGISMapImageLayer usaMapImageLayer = (ArcGISMapImageLayer)MyMapView.Map.OperationalLayers[0];

            try
            {
                // Use a utility method on the map image layer to load all the sublayers and tables.
                await usaMapImageLayer.LoadTablesAndLayersAsync();

                // Get the sublayers of interest.
                ArcGISMapImageSublayer citiesSublayer = (ArcGISMapImageSublayer)usaMapImageLayer.Sublayers[0];
                ArcGISMapImageSublayer countiesSublayer = (ArcGISMapImageSublayer)usaMapImageLayer.Sublayers[3];
                ArcGISMapImageSublayer statesSublayer = (ArcGISMapImageSublayer)usaMapImageLayer.Sublayers[2];

                // Get the service feature table for each of the sublayers.
                ServiceFeatureTable citiesTable = citiesSublayer.Table;
                ServiceFeatureTable countiesTable = countiesSublayer.Table;
                ServiceFeatureTable statesTable = statesSublayer.Table;

                // Create the query parameters that will find features.
                QueryParameters citiesNameQuery = new QueryParameters
                {
                    WhereClause = "AREANAME=" + SearchTextBox.Text,
                    Geometry = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry,
                    OutSpatialReference = MyMapView.SpatialReference
                };
                QueryParameters countiesNameQuery = new QueryParameters
                {
                    WhereClause = "NAME=" + SearchTextBox.Text,
                    Geometry = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry,
                    OutSpatialReference = MyMapView.SpatialReference
                };
                QueryParameters statesNameQuery = new QueryParameters
                {
                    WhereClause = "STATE_NAME=" + SearchTextBox.Text,
                    Geometry = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry,
                    OutSpatialReference = MyMapView.SpatialReference
                };

                // Query each of the sublayers with the query parameters.
                FeatureQueryResult citiesQueryResult = await citiesTable.QueryFeaturesAsync(citiesNameQuery);
                FeatureQueryResult countiesQueryResult = await citiesTable.QueryFeaturesAsync(countiesNameQuery);
                FeatureQueryResult statesQueryResult = await citiesTable.QueryFeaturesAsync(statesNameQuery);

                var foundCities = 0;
                var foundCounties = 0;
                var foundStates = 0;

                // Loop through results, count the matches found in each layer.
                foreach (Feature city in citiesQueryResult)
                {
                    foundCities++;
                }

                foreach (Feature county in countiesQueryResult)
                {
                    foundCounties++;
                }
                foreach (Feature states in statesQueryResult)
                {
                    foundStates++;
                }

                // Report the number of matches for each layer.
                var msg = $"Found {foundCities} cities, {foundCounties} counties, and {foundStates} states containing {SearchTextBox.Text}" +
                    $"in a Name attribute.";
                MessageBox.Show(msg);

                // Bind the results to a DataGrid Control on the page
                MyDataGrid.ItemsSource = citiesQueryResult;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error");
            }
        }
    }
}

Thanks

Capture.JPGError.JPG

Tags (1)
0 Kudos
2 Solutions

Accepted Solutions
MaximilianGlas
Esri Contributor

You have 2 issues in your code. That's the correct way.

WhereClauses are missing a quote:

                QueryParameters citiesNameQuery = new QueryParameters
                {
                    WhereClause = $"AREANAME='{SearchTextBox.Text}'",
                    Geometry = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry,
                    OutSpatialReference = MyMapView.SpatialReference
                };
                QueryParameters countiesNameQuery = new QueryParameters
                {
                    WhereClause = $"NAME='{SearchTextBox.Text}'",
                    Geometry = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry,
                    OutSpatialReference = MyMapView.SpatialReference
                };
                QueryParameters statesNameQuery = new QueryParameters
                {
                    WhereClause = $"STATE_NAME='{SearchTextBox.Text}'",
                    Geometry = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry,
                    OutSpatialReference = MyMapView.SpatialReference
                };

 

And you referenced the same table three times, you need to use the associated tables:

                FeatureQueryResult citiesQueryResult = await citiesTable.QueryFeaturesAsync(citiesNameQuery);
                FeatureQueryResult countiesQueryResult = await countiesTable.QueryFeaturesAsync(countiesNameQuery);
                FeatureQueryResult statesQueryResult = await statesTable.QueryFeaturesAsync(statesNameQuery);

 

Then it will work.

View solution in original post

ManishShrivastav
Occasional Contributor

Thanks MaximilianGlas. This worked well for me. But still I am not able show the results in the data grid. I tried to bind the query results to the datagrid using the following code.

Code Snippet

 // Query each of the sublayers with the query parameters.
                FeatureQueryResult citiesQueryResult = await citiesTable.QueryFeaturesAsync(citiesNameQuery);
                FeatureQueryResult countiesQueryResult = await countiesTable.QueryFeaturesAsync(countiesNameQuery);
                FeatureQueryResult statesQueryResult = await statesTable.QueryFeaturesAsync(statesNameQuery);

                var foundCities = 0;
                var foundCounties = 0;
                var foundStates = 0;

                // Loop through results, count the matches found in each layer.
                foreach (Feature city in citiesQueryResult)
                {
                    foundCities++;
                }

                foreach (Feature county in countiesQueryResult)
                {
                    foundCounties++;
                }
                foreach (Feature states in statesQueryResult)
                {
                    foundStates++;
                }

                // Report the number of matches for each layer.
                var msg = $"Found {foundCities} cities, {foundCounties} counties, and {foundStates} states containing {SearchTextBox.Text}" +
                    $" in a Name attribute.";
                MessageBox.Show(msg);

                // Bind the results to a DataGrid Control on the page
                MyDataGrid.ItemsSource = citiesQueryResult;
                MyDataGrid.ItemsSource = countiesQueryResult;
                MyDataGrid.ItemsSource = statesQueryResult;
            }

 

View solution in original post

0 Kudos
2 Replies
MaximilianGlas
Esri Contributor

You have 2 issues in your code. That's the correct way.

WhereClauses are missing a quote:

                QueryParameters citiesNameQuery = new QueryParameters
                {
                    WhereClause = $"AREANAME='{SearchTextBox.Text}'",
                    Geometry = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry,
                    OutSpatialReference = MyMapView.SpatialReference
                };
                QueryParameters countiesNameQuery = new QueryParameters
                {
                    WhereClause = $"NAME='{SearchTextBox.Text}'",
                    Geometry = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry,
                    OutSpatialReference = MyMapView.SpatialReference
                };
                QueryParameters statesNameQuery = new QueryParameters
                {
                    WhereClause = $"STATE_NAME='{SearchTextBox.Text}'",
                    Geometry = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry,
                    OutSpatialReference = MyMapView.SpatialReference
                };

 

And you referenced the same table three times, you need to use the associated tables:

                FeatureQueryResult citiesQueryResult = await citiesTable.QueryFeaturesAsync(citiesNameQuery);
                FeatureQueryResult countiesQueryResult = await countiesTable.QueryFeaturesAsync(countiesNameQuery);
                FeatureQueryResult statesQueryResult = await statesTable.QueryFeaturesAsync(statesNameQuery);

 

Then it will work.

ManishShrivastav
Occasional Contributor

Thanks MaximilianGlas. This worked well for me. But still I am not able show the results in the data grid. I tried to bind the query results to the datagrid using the following code.

Code Snippet

 // Query each of the sublayers with the query parameters.
                FeatureQueryResult citiesQueryResult = await citiesTable.QueryFeaturesAsync(citiesNameQuery);
                FeatureQueryResult countiesQueryResult = await countiesTable.QueryFeaturesAsync(countiesNameQuery);
                FeatureQueryResult statesQueryResult = await statesTable.QueryFeaturesAsync(statesNameQuery);

                var foundCities = 0;
                var foundCounties = 0;
                var foundStates = 0;

                // Loop through results, count the matches found in each layer.
                foreach (Feature city in citiesQueryResult)
                {
                    foundCities++;
                }

                foreach (Feature county in countiesQueryResult)
                {
                    foundCounties++;
                }
                foreach (Feature states in statesQueryResult)
                {
                    foundStates++;
                }

                // Report the number of matches for each layer.
                var msg = $"Found {foundCities} cities, {foundCounties} counties, and {foundStates} states containing {SearchTextBox.Text}" +
                    $" in a Name attribute.";
                MessageBox.Show(msg);

                // Bind the results to a DataGrid Control on the page
                MyDataGrid.ItemsSource = citiesQueryResult;
                MyDataGrid.ItemsSource = countiesQueryResult;
                MyDataGrid.ItemsSource = statesQueryResult;
            }

 

0 Kudos