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
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.
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.
An AGSFeatureLayer is created by passing in the instance of the AGSServiceFeatureTable.
An AGSTimeExtent object is created by specifying start and end date/time objects.
Apply the AGSTimeExtent to an AGSQueryParameters object.
Pass the AGSQueryParameters into AGSServiceFeatureTable.populateFromService(with:clearCache:outFields:completion:).
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
classTimeBasedQueryViewController: UIViewController{
@IBOutletvar mapView: AGSMapView!
privatevar map: AGSMap!
privatevar featureTable: AGSServiceFeatureTable!
overridefuncviewDidLoad() {
super.viewDidLoad()
// initialize map with oceans basemapself.map =AGSMap(basemapStyle: .arcGISOceans)
// assign map to the map viewself.mapView.map =self.map
// create feature table using a urlself.featureTable =AGSServiceFeatureTable(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Hurricanes/MapServer/0")!)
// define the request modeself.featureTable.featureRequestMode = .manualCache
// create feature layer using the feature tablelet layer =AGSFeatureLayer(featureTable: self.featureTable)
// add feature layer to map's operational layersself.map.operationalLayers.add(layer)
// populate features based on a time-based queryself.populateFeaturesWithQuery()
// add the source code button item to the right of navigation bar (self.navigationItem.rightBarButtonItem as!SourceCodeBarButtonItem).filenames = ["TimeBasedQueryViewController"]
}
funcpopulateFeaturesWithQuery() {
// create query parameterslet queryParams =AGSQueryParameters()
// create a new time extent that covers the desired interval from September 1st to September 22th, 2000let 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 parametersself.featureTable.populateFromService(with: queryParams, clearCache: true, outFields: ["*"]) { [weakself] (result: AGSFeatureQueryResult?, error: Error?) in// check for erroriflet error = error {
self?.presentAlert(error: error)
} else {
// the resulting features should be displayed on the map// you can print the count of featuresprint("Hurriance features during the time interval: \(result?.featureEnumerator().allObjects.count ??0)")
}
}
}
}