Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Time-based query

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

This sample demonstrates how to query data using a time extent. This workflow can be used to return records that are between a specified start and end date. For example, you could specify to only show records that are before September 16, 2000.

screenshot

How to use the sample

Run the sample, and a subset of records will be displayed on the map.

How it works

  1. An instance of ServiceFeatureTable is created by passing a URL to the REST endpoint of a time-enabled service. Time-enabled services will have TimeInfo defined in the service description. This information is specified in ArcMap or ArcGIS Pro prior to publishing the service.
  2. The feature request mode of the ServiceFeatureTable is set to ManualCache, so that the developer can control how and when the feature table is populated with data.
  3. A FeatureLayer is created by passing in the instance of the ServiceFeatureTable.
  4. A TimeExtent object is created by specifying start and end date/time objects.
  5. A QueryParmaters object is created with the TimeExtent.
  6. ServiceFeatureTable.PopulateFromService is executed by passing in the QueryParameters.
  7. The feature table is populated with data that matches the provided query.

Relevant API

  • QueryParameters
  • ServiceFeatureTable.PopulateFromService
  • TimeExtent

Tags

Query, Time, Time Extent

Sample Code

<UserControl
    x:Class="ArcGISRuntime.UWP.Samples.TimeBasedQuery.TimeBasedQuery"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esriUI="using:Esri.ArcGISRuntime.UI.Controls">
    <Grid>
        <esriUI:MapView x:Name="MyMapView" />
    </Grid>
</UserControl>
// 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;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Mapping;
using System;
using Windows.UI.Popups;

namespace ArcGISRuntime.UWP.Samples.TimeBasedQuery
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Time-based query",
        "Layers",
        "This sample demonstrates how to apply a time-based parameter to a feature layer query.",
        "")]
    public partial class TimeBasedQuery
    {
        // Hold a URI pointing to the feature service
        private Uri _serviceUri = new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Hurricanes/MapServer/0");

        // Hold a reference to the feature table used by the sample
        private ServiceFeatureTable _myFeatureTable;

        public TimeBasedQuery()
        {
            InitializeComponent();

            // Initialize the map
            Initialize();
        }

        private void Initialize()
        {
            // Create a new map with oceans basemap
            Map myMap = new Map(Basemap.CreateOceans());

            // Create feature table for the hurricane feature service
            _myFeatureTable = new ServiceFeatureTable(_serviceUri)
            {

                // Define the request mode
                FeatureRequestMode = FeatureRequestMode.ManualCache
            };

            // When feature table is loaded, populate data
            _myFeatureTable.LoadStatusChanged += OnLoadedPopulateData;

            // Create FeatureLayer that uses the created table
            FeatureLayer myFeatureLayer = new FeatureLayer(_myFeatureTable);

            // Add created layer to the map
            myMap.OperationalLayers.Add(myFeatureLayer);

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

        private async void OnLoadedPopulateData(object sender, LoadStatusEventArgs e)
        {
            // If layer isn't loaded, do nothing
            if (e.Status != LoadStatus.Loaded) { return; }

            // Create new query object that contains a basic 'include everything' clause
            QueryParameters queryParameters = new QueryParameters()
            {
                WhereClause = "1=1"
            };

            // Create a new time extent that covers the desired interval (beginning of time to September 16th, 2000)
            TimeExtent myExtent = new TimeExtent(new DateTime(1, 1, 1), new DateTime(2000, 9, 16));

            // Apply the time extent to the query parameters
            queryParameters.TimeExtent = myExtent;

            // Create list of the fields that are returned from the service
            string[] outputFields = { "*" };

            try
            {
                // Populate feature table with the data based on query
                await _myFeatureTable.PopulateFromServiceAsync(queryParameters, true, outputFields);
            }
            catch (Exception ex)
            {
                await new MessageDialog(ex.ToString(), "Error").ShowAsync();
            }
        }
    }
}