Time-based query

View inWPFUWPFormsiOSAndroid
View on GitHub

Query data using a time extent.

Image of time-based query

Use case

This workflow can be used to return records that are between a specified start and end date. For example, records of Canada goose sightings over time could be queried to only show sightings during the winter migration time period.

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

About the data

This sample uses Atlantic hurricane data from the year 2000. The data is from the National Hurricane Center (NOAA / National Weather Service).

Tags

query, time, time extent

Sample Code

TimeBasedQuery.cs
                                                                                                                          
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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 System;
using Esri.ArcGISRuntime;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using Foundation;
using UIKit;

namespace ArcGISRuntime.Samples.TimeBasedQuery
{
    [Register("TimeBasedQuery")]
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Time-based query",
        category: "Layers",
        description: "Query data using a time extent. ",
        instructions: "Run the sample, and a subset of records will be displayed on the map.",
        tags: new[] { "query", "time", "time extent" })]
    public class TimeBasedQuery : UIViewController
    {
        // Hold references to UI controls.
        private MapView _myMapView;

        // Hold a URI pointing to the feature service.
        private readonly 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()
        {
            Title = "Time-based query";
        }

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

            // 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.Loaded += 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, EventArgs e)
        {
            // Unsubscribe from events.
            _myFeatureTable.Loaded -= OnLoadedPopulateData;

            // Create new query object that contains a basic 'include everything' clause.
            QueryParameters queryParameters = new QueryParameters
            {
                WhereClause = "1=1",
                // Restrict query with a time extent that covers the desired interval (beginning of time to September 16th, 2000).
                TimeExtent = new TimeExtent(new DateTime(1, 1, 1), new DateTime(2000, 9, 16))
            };

            // 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)
            {
                new UIAlertView("Error", ex.ToString(), (IUIAlertViewDelegate) null, "OK", null).Show();
            }
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            Initialize();
        }

        public override void LoadView()
        {
            // Create the views.
            View = new UIView() { BackgroundColor = ApplicationTheme.BackgroundColor };

            _myMapView = new MapView();
            _myMapView.TranslatesAutoresizingMaskIntoConstraints = false;

            // Add the views.
            View.AddSubviews(_myMapView);

            // Lay out the views.
            NSLayoutConstraint.ActivateConstraints(new[]
            {
                _myMapView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
                _myMapView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor),
                _myMapView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
                _myMapView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor)
            });
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.