Time based query

View on GitHubSample viewer app

Query data using a time extent.

Time based query sample

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. Pan and zoom to explore.

How it works

  1. An instance of AGSServiceFeatureTable 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 featureRequestMode of the AGSServiceFeatureTable is set to manualCache, so that the developer can control how and when the feature table is populated with data.
  3. An AGSFeatureLayer is created by passing in the instance of the AGSServiceFeatureTable.
  4. An AGSTimeExtent object is created by specifying start and end date/time objects.
  5. Apply the AGSTimeExtent to an AGSQueryParameters object.
  6. Pass the AGSQueryParameters into AGSServiceFeatureTable.populateFromService(with:clearCache:outFields:completion:).
  7. The feature table is populated with data that matches the provided query.

Relevant API

  • AGSQueryParameters
  • AGSServiceFeatureTable.populateFromService
  • AGSTimeExtent

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

TimeBasedQueryViewController.swift
Use dark colors for code blocksCopy
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
//
// 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.
//

import UIKit
import ArcGIS

class TimeBasedQueryViewController: UIViewController {
    @IBOutlet var mapView: AGSMapView!

    private var map: AGSMap!

    private var featureTable: AGSServiceFeatureTable!

    override func viewDidLoad() {
        super.viewDidLoad()

        // initialize map with oceans basemap
        self.map = AGSMap(basemapStyle: .arcGISOceans)

        // assign map to the map view
        self.mapView.map = self.map

        // create feature table using a url
        self.featureTable = AGSServiceFeatureTable(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Hurricanes/MapServer/0")!)

        // define the request mode
        self.featureTable.featureRequestMode = .manualCache

        // create feature layer using the feature table
        let layer = AGSFeatureLayer(featureTable: self.featureTable)

        // add feature layer to map's operational layers
        self.map.operationalLayers.add(layer)

        // populate features based on a time-based query
        self.populateFeaturesWithQuery()

        // add the source code button item to the right of navigation bar
        (self.navigationItem.rightBarButtonItem as! SourceCodeBarButtonItem).filenames = ["TimeBasedQueryViewController"]
    }

    func populateFeaturesWithQuery() {
        // create query parameters
        let queryParams = AGSQueryParameters()

        // create a new time extent that covers the desired interval from September 1st to September 22th, 2000
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy/MM/dd HH:mm"
        let startTime = formatter.date(from: "2000/9/1 00:00")
        let endTime = formatter.date(from: "2000/9/22 00:00")

        let timeExtent = AGSTimeExtent(startTime: startTime, endTime: endTime)

        // apply the time extent to query parameters to filter features based on time
        queryParams.timeExtent = timeExtent

        // populate features based on query parameters
        self.featureTable.populateFromService(with: queryParams, clearCache: true, outFields: ["*"]) { [weak self] (result: AGSFeatureQueryResult?, error: Error?) in
            // check for error
            if let error = error {
                self?.presentAlert(error: error)
            } else {
                // the resulting features should be displayed on the map
                // you can print the count of features
                print("Hurriance features during the time interval: \(result?.featureEnumerator().allObjects.count ?? 0)")
            }
        }
    }
}

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