Analyze hotspots

View on GitHubSample viewer app

Use a geoprocessing service and a set of features to identify statistically significant hot spots and cold spots.

Analysis date picker Analysis results

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

  1. Create an AGSGeoprocessingTask with the URL set to the endpoint of a geoprocessing service.
  2. Create a query string with the date range as an input of AGSGeoprocessingParameters.
  3. Use the AGSGeoprocessingTask to create an AGSGeoprocessingJob with the AGSGeoprocessingParameters instance.
  4. Start the AGSGeoprocessingJob and wait for it to complete and return an AGSGeoprocessingResult.
  5. Get the resulting AGSArcGISMapImageLayer.
  6. Add the layer to the map's operational layers.

Relevant API

  • AGSGeoprocessingJob
  • AGSGeoprocessingParameters
  • AGSGeoprocessingResult
  • AGSGeoprocessingTask

Tags

analysis, density, geoprocessing, hot spots, hotspots

Sample Code

AnalyzeHotspotsViewController.swiftAnalyzeHotspotsViewController.swiftHotspotSettingsViewController.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
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
// 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
        }
    }
}

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