Query data using a time extent.
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
- 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 theAGSServiceFeatureTable
is set tomanualCache
, 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 theAGSServiceFeatureTable
. - An
AGSTimeExtent
object is created by specifying start and end date/time objects. - Apply the
AGSTimeExtent
to anAGSQueryParameters
object. - Pass the
AGSQueryParameters
intoAGSServiceFeatureTable.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
//
// 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)")
}
}
}
}