Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Query feature count and extent

This code sample is available for these platforms:
View Sample on GitHub

Zoom to features matching a query and count the features in the current visible extent.

Image of query feature count and extent

Use case

Queries can be used to search for features in a feature table using text entry. This is helpful for finding a specific feature by name in a large feature table. A query can also be used to count features in an extent. This could be used to count the number of a traffic incidents in a particular region when working with an incident dataset for a larger area.

How to use the sample

Use the button to zoom to the extent of the state specified (by abbreviation) in the textbox or use the button to count the features in the current extent.

How it works

Querying by state abbreviation:

  1. A QueryParameters object is created with a WhereClause.
  2. FeatureTable.QueryExtentAsync is called with the QueryParameters object to obtain the extent that contains all matching features.
  3. The extent is converted to a Viewpoint, which is passed to MapView.SetViewpointAsync.

Counting features in the current extent:

  1. The current visible extent is obtained from a call to MapView.GetCurrentViewpoint(ViewpointType).
  2. A QueryParameters object is created with the visible extent and a defined SpatialRelationship (in this case 'intersects').
  3. The count of matching features is obtained from a call to FeatureTable.QueryFeatureCountAsync.

Relevant API

  • FeatureTable.QueryExtentAsync
  • FeatureTable.QueryFeatureCountAsync
  • MapView.GetCurrentViewpoint(ViewpointType)
  • QueryParameters
  • QueryParameters.Geometry
  • QueryParameters.SpatialRelationship
  • QueryParameters.WhereClause

About the data

See the Medicare Hospital Spending per Patient, 2016 layer on ArcGIS Online.

Hospitals in blue/turquoise spend less than the national average. Red/salmon indicates higher spending relative to other hospitals, while gray is average.


count, feature layer, feature table, features, filter, number, query

Sample Code

<?xml version="1.0" encoding="utf-8" ?>
            BindingContext="{x:Reference Name=ResponsiveFormContainer}"
            Style="{StaticResource MapWithFormStyle}" />
        <sampleViewer:ResponsiveFormContainer x:Name="ResponsiveFormContainer">
                    TextColor="Black" />
                <Button Clicked="BtnZoomToFeatures_Click" Text="Zoom to matching" />
                <Button Clicked="BtnCountFeatures_Click" Text="Count in extent" />
                <Label x:Name="txtResults" Style="{StaticResource LabelStyle}" />
// Copyright 2018 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:
// 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 Xamarin.Forms;

namespace ArcGISRuntime.Samples.QueryFeatureCountAndExtent
        name: "Query feature count and extent",
        category: "Analysis",
        description: "Zoom to features matching a query and count the features in the current visible extent.",
        instructions: "Use the button to zoom to the extent of the state specified (by abbreviation) in the textbox or use the button to count the features in the current extent.",
        tags: new[] { "count", "feature layer", "feature table", "features", "filter", "number", "query" })]
    public partial class QueryFeatureCountAndExtent : ContentPage
        // URL to the feature service.
        private readonly Uri _medicareHospitalSpendLayer =
            new Uri("");

        // Feature table to query.
        private ServiceFeatureTable _featureTable;

        public QueryFeatureCountAndExtent()

        private async void Initialize()
            // Create the map with a basemap.
            Map myMap = new Map(Basemap.CreateDarkGrayCanvasVector());

            // Create the feature table from the service URL.
            _featureTable = new ServiceFeatureTable(_medicareHospitalSpendLayer);

            // Create the feature layer from the table.
            FeatureLayer myFeatureLayer = new FeatureLayer(_featureTable);

            // Add the feature layer to the map.

                // Wait for the feature layer to load.
                await myFeatureLayer.LoadAsync();

                // Set the map initial extent to the extent of the feature layer.
                myMap.InitialViewpoint = new Viewpoint(myFeatureLayer.FullExtent);

                // Add the map to the MapView.
                MyMapView.Map = myMap;
            catch (Exception e)
                await Application.Current.MainPage.DisplayAlert("Error", e.ToString(), "OK");

        private async void BtnZoomToFeatures_Click(object sender, EventArgs e)
            // Create the query parameters.
            QueryParameters queryStates = new QueryParameters { WhereClause = $"upper(State) LIKE '%{txtStateEntry.Text.ToUpper()}%'" };

                // Get the extent from the query.
                Envelope resultExtent = await _featureTable.QueryExtentAsync(queryStates);

                // Return if there is no result (might happen if query is invalid).
                if (resultExtent?.SpatialReference == null)

                // Create a viewpoint from the extent.
                Viewpoint resultViewpoint = new Viewpoint(resultExtent);

                // Zoom to the viewpoint.
                await MyMapView.SetViewpointAsync(resultViewpoint);

                // Update the UI.
                txtResults.Text = $"Zoomed to features in {txtStateEntry.Text}";
            catch (Exception ex)
                await Application.Current.MainPage.DisplayAlert("Error", ex.ToString(), "OK");

        private async void BtnCountFeatures_Click(object sender, EventArgs e)
            // Get the current visible extent.
            Geometry currentExtent = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry;

            // Create the query parameters.
            QueryParameters queryCityCount = new QueryParameters
                Geometry = currentExtent,
                // Specify the interpretation of the Geometry query parameters.
                SpatialRelationship = SpatialRelationship.Intersects

                // Get the count of matching features.
                long count = await _featureTable.QueryFeatureCountAsync(queryCityCount);

                // Update the UI.
                txtResults.Text = $"{count} features in extent";
            catch (Exception ex)
                await Application.Current.MainPage.DisplayAlert("Error", ex.ToString(), "OK");