Distance measurement analysis

View on GitHubSample viewer app

Measure distances between two points in 3D.

Distance measurement analysis

Use case

The distance measurement analysis allows you to add to your app the same interactive measuring experience found in ArcGIS Pro, City Engine, and the ArcGIS API for JavaScript. You can set the unit system of measurement (metric or imperial). The units automatically switch to one appropriate for the current scale.

How to use the sample

Choose a unit system for the measurement in the segmented control. Long press any location in the scene to start measuring. Then drag to an end location, and lift your finger to complete the measure. Tap a new location to clear and start a new measurement.

How it works

  1. Create an AGSAnalysisOverlay object and add it to the analysis overlay collection of the AGSSceneView object.
  2. Specify the start location and end location to create an AGSLocationDistanceMeasurement object. Initially, the start and end locations can be the same point.
  3. Add the location distance measurement analysis to the analysis overlay.
  4. The measurementChangedHandler callback will fire if the distances change. You can get the new values for the directDistance, horizontalDistance, and verticalDistance from the parameters provided by the callback. The distance objects contain both the scalar value and unit of measurement.

Relevant API

  • AGSAnalysisOverlay
  • AGSLocationDistanceMeasurement
  • AGSMeasurementChangedEvent

Additional information

The AGSLocationDistanceMeasurement analysis only performs planar distance calculations. This may not be appropriate for large distances where the Earth's curvature must be considered.

Tags

3D, analysis, distance, measure

Sample Code

DistanceMeasurementAnalysisViewController.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// Copyright © 2018 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 DistanceMeasurementAnalysisViewController: UIViewController, AGSGeoViewTouchDelegate {
    /// The scene displayed in the scene view.
    let scene: AGSScene
    /// The location distance measurement analysis.
    let locationDistanceMeasurement: AGSLocationDistanceMeasurement

    /// The scene view managed by the view controller.
    @IBOutlet weak var sceneView: AGSSceneView!
    @IBOutlet weak var directMeasurementLabel: UILabel!
    @IBOutlet weak var horizontalMeasurementLabel: UILabel!
    @IBOutlet weak var verticalMeasurementLabel: UILabel!

    required init?(coder: NSCoder) {
        // Create the scene.
        scene = AGSScene(basemapStyle: .arcGISTopographic)

        // The elevation image service URL.
        let elevationServiceURL = URL(string: "https://scene.arcgis.com/arcgis/rest/services/BREST_DTM_1M/ImageServer")!

        // Create the surface and set it as the base surface of the scene.
        let elevationSource = AGSArcGISTiledElevationSource(url: elevationServiceURL)
        let surface = AGSSurface()
        surface.elevationSources.append(elevationSource)
        scene.baseSurface = surface

        // The URL of the scene service for buildings in Brest, France.
        let brestBuildingsServiceURL = URL(string: "https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Brest/SceneServer/layers/0")!
        // Create the building layer and add it to the scene.
        let buildingsLayer = AGSArcGISSceneLayer(url: brestBuildingsServiceURL)
        // Offset the altitude to avoid clipping with the elevation source.
        buildingsLayer.altitudeOffset = 2
        scene.operationalLayers.add(buildingsLayer)

        // Create the location distance measurement.
        let startPoint = AGSPoint(x: -4.494677, y: 48.384472, z: 24.772694, spatialReference: .wgs84())
        let endPoint = AGSPoint(x: -4.495646, y: 48.384377, z: 58.501115, spatialReference: .wgs84())
        locationDistanceMeasurement = AGSLocationDistanceMeasurement(startLocation: startPoint, endLocation: endPoint)

        super.init(coder: coder)

        locationDistanceMeasurement.measurementChangedHandler = { [weak self] _, _, _ in
            DispatchQueue.main.async {
                self?.updateMeasurementLabels()
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Configure the scene view.
        sceneView.scene = scene
        sceneView.touchDelegate = self
        let lookAtPoint = AGSEnvelope(min: locationDistanceMeasurement.startLocation, max: locationDistanceMeasurement.endLocation).center
        let camera = AGSCamera(lookAt: lookAtPoint, distance: 200, heading: 0, pitch: 45, roll: 0)
        sceneView.setViewpointCamera(camera)

        // Create the analysis overlay with the location distance measurement
        // analysis and add it to the scene view.
        let analysisOverlay = AGSAnalysisOverlay()
        analysisOverlay.analyses.add(locationDistanceMeasurement)
        sceneView.analysisOverlays.add(analysisOverlay)
        updateMeasurementLabels()

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

    @IBAction func unitSystemSegmentedControl(_ sender: UISegmentedControl) {
        guard let unitSystem = AGSUnitSystem(rawValue: sender.selectedSegmentIndex) else { return }
        locationDistanceMeasurement.unitSystem = unitSystem
    }

    let measurementFormatter: MeasurementFormatter = {
        let measurementFormatter = MeasurementFormatter()
        measurementFormatter.numberFormatter.minimumFractionDigits = 2
        measurementFormatter.numberFormatter.maximumFractionDigits = 2
        return measurementFormatter
    }()

    func updateMeasurementLabels() {
        guard isViewLoaded else { return }
        if locationDistanceMeasurement.startLocation != locationDistanceMeasurement.endLocation,
            let directDistance = locationDistanceMeasurement.directDistance,
            let horizontalDistance = locationDistanceMeasurement.horizontalDistance,
            let verticalDistance = locationDistanceMeasurement.verticalDistance {
            directMeasurementLabel.text = measurementFormatter.string(from: Measurement(distance: directDistance))
            horizontalMeasurementLabel.text = measurementFormatter.string(from: Measurement(distance: horizontalDistance))
            verticalMeasurementLabel.text = measurementFormatter.string(from: Measurement(distance: verticalDistance))
        } else {
            directMeasurementLabel.text = "--"
            horizontalMeasurementLabel.text = "--"
            verticalMeasurementLabel.text = "--"
        }
    }

    // MARK: AGSGeoViewTouchDelegate

    func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
        sceneView.screen(toLocation: screenPoint) { [weak self] mapLocation in
            guard let measurement = self?.locationDistanceMeasurement else { return }
            if measurement.startLocation != measurement.endLocation {
                measurement.startLocation = mapLocation
            }
            measurement.endLocation = mapLocation
        }
    }

    func geoView(_ geoView: AGSGeoView, didLongPressAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
        sceneView.screen(toLocation: screenPoint) { [weak self] mapLocation in
            guard let measurement = self?.locationDistanceMeasurement else { return }
            if measurement.startLocation != measurement.endLocation {
                measurement.startLocation = mapLocation
            }
            measurement.endLocation = mapLocation
        }
    }

    func geoView(_ geoView: AGSGeoView, didMoveLongPressToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
        sceneView.screen(toLocation: screenPoint) { [weak self] mapLocation in
            self?.locationDistanceMeasurement.endLocation = mapLocation
        }
    }
}

extension Measurement where UnitType == Unit {
    /// Creates a measurement from an ArcGIS distance.
    ///
    /// - Parameter distance: An `AGSDistance` object.
    init(distance: AGSDistance) {
        let unit = Unit(symbol: distance.unit.abbreviation)
        let value = distance.value
        self.init(value: value, unit: unit)
    }
}

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