Use a geoprocessing service and a set of features to identify statistically significant hot spots and cold spots.
Use case
This tool identifies statistically significant spatial clusters of high values (hot spots) and low values (cold spots). For example, a hotspot analysis based on the frequency of 911 calls within a set region.
How to use the sample
Select a date range (between 1998-01-01 and 1998-05-31) from the dialog and tap on Analyze. The results will be shown on the map upon successful completion of the AGSGeoprocessingJob
.
How it works
- Create an
AGSGeoprocessingTask
with the URL set to the endpoint of a geoprocessing service. - Create a query string with the date range as an input of
AGSGeoprocessingParameters
. - Use the
AGSGeoprocessingTask
to create anAGSGeoprocessingJob
with theAGSGeoprocessingParameters
instance. - Start the
AGSGeoprocessingJob
and wait for it to complete and return anAGSGeoprocessingResult
. - Get the resulting
AGSArcGISMapImageLayer
. - Add the layer to the map's operational layers.
Relevant API
- AGSGeoprocessingJob
- AGSGeoprocessingParameters
- AGSGeoprocessingResult
- AGSGeoprocessingTask
Tags
analysis, density, geoprocessing, hot spots, hotspots
Sample Code
// 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 AnalyzeHotspotsViewController: UIViewController, HotspotSettingsViewControllerDelegate {
@IBOutlet var mapView: AGSMapView!
/// Geoprocessing task with the url of the service
private let geoprocessingTask = AGSGeoprocessingTask(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/911CallsHotspot/GPServer/911%20Calls%20Hotspot")!)
private var graphicsOverlay = AGSGraphicsOverlay()
private var geoprocessingJob: AGSGeoprocessingJob?
private let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
return dateFormatter
}()
override func viewDidLoad() {
super.viewDidLoad()
// add the source code button item to the right of navigation bar
(navigationItem.rightBarButtonItem as! SourceCodeBarButtonItem).filenames = [
"AnalyzeHotspotsViewController",
"HotspotSettingsViewController"
]
// initialize map with basemap
let map = AGSMap(basemapStyle: .arcGISTopographic)
// assign map to map view
mapView.map = map
let center = AGSPoint(x: -13671170.647485, y: 5693633.356735, spatialReference: .webMercator())
mapView.setViewpoint(AGSViewpoint(center: center, scale: 57779))
}
private func analyzeHotspots(_ fromDate: Date, toDate: Date) {
// cancel previous job request
self.geoprocessingJob?.progress.cancel()
let fromDateString = dateFormatter.string(from: fromDate)
let toDateString = dateFormatter.string(from: toDate)
// parameters
let params = AGSGeoprocessingParameters(executionType: .asynchronousSubmit)
params.processSpatialReference = self.mapView.map?.spatialReference
params.outputSpatialReference = self.mapView.map?.spatialReference
// query string
let queryString = "(\"DATE\" > date '\(fromDateString) 00:00:00' AND \"DATE\" < date '\(toDateString) 00:00:00')"
params.inputs["Query"] = AGSGeoprocessingString(value: queryString)
// job
let geoprocessingJob = geoprocessingTask.geoprocessingJob(with: params)
self.geoprocessingJob = geoprocessingJob
// start job
geoprocessingJob.start(statusHandler: { (status: AGSJobStatus) in
// show progress hud with job status
UIApplication.shared.showProgressHUD(message: status.statusString())
}, completion: { [weak self] (result: AGSGeoprocessingResult?, error: Error?) in
// dismiss progress hud
UIApplication.shared.hideProgressHUD()
guard let self = self else { return }
if let error = error {
// show error
self.presentAlert(error: error)
} else {
// a map image layer is generated as a result
// remove any layer previously added to the map
self.mapView.map?.operationalLayers.removeAllObjects()
// add the new layer to the map
self.mapView.map?.operationalLayers.add(result!.mapImageLayer!)
// set map view's viewpoint to the new layer's full extent
(self.mapView.map?.operationalLayers.firstObject as! AGSLayer).load { [weak self] (error: Error?) in
guard let self = self else { return }
if let error = error {
self.presentAlert(error: error)
} else {
// set viewpoint as the extent of the mapImageLayer
if let extent = result?.mapImageLayer?.fullExtent {
self.mapView.setViewpointGeometry(extent)
}
}
}
}
})
}
// MARK: - HotspotSettingsViewControllerDelegate
func hotspotSettingsViewController(_ hotspotSettingsViewController: HotspotSettingsViewController, didSelectDates fromDate: Date, toDate: Date) {
hotspotSettingsViewController.dismiss(animated: true)
analyzeHotspots(fromDate, toDate: toDate)
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let navController = segue.destination as? UINavigationController,
let controller = navController.viewControllers.first as? HotspotSettingsViewController {
controller.delegate = self
}
}
}