Distance measurement analysis

View inC++QMLView on GitHubSample viewer app

This sample demonstrates measuring 3D distances between two points in a scene.

The distance measurement analysis allows you to add the same measuring experience found in ArcGIS Pro, City Engine, and the ArcGIS API for JavaScript to your app. You can set the unit system of measurement (metric or imperial) and have the units automatically switch to one appropriate for the current scale. The rendering is handled internally so they do not interfere with other analyses like viewsheds.

screenshot

How to use the sample

Choose a unit system for the measurement in the UI dropdown. Click any location in the scene to set the starting measuring point. Press, hold, and drag to a location to update the end location.

How it works

To measure distances with the LocationDistanceMeasurement analysis:

  1. Create an AnalysisOverlay and add it to your scene view's analysis overlay collection.
  2. Create a LocationDistanceMeasurement, specifying the startLocation and endLocation. To start with, these locations can be the same. Add the analysis to the analysis overlay. The measuring line will be drawn between the two points.
  3. The directDistanceChanged, verticalDistanceChanged, and horizontalDistanceChanged signals will emit when the distances change, giving access to the new values for the directDistance, horizontalDistance, and verticalDistance. The distance objects contain both the scalar value and unit of measurement.

Relevant API

  • AnalysisOverlay
  • LocationDistanceMeasurement

Additional information

The LocationDistanceMeasurement analysis only performs planar distance calculations. This may not be appropriate for large distances where the Earth's curvature needs to be taken into account.

Tags

3D, Analysis

Sample Code

DistanceMeasurementAnalysis.qml
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// [WriteFile Name=DistanceMeasurementAnalysis, Category=Analysis]
// [Legal]
// 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.
// [Legal]

import QtQuick
import QtQuick.Controls
import Esri.ArcGISRuntime
import Esri.ArcGISExtras

Rectangle {
    id: rootRectangle
    clip: true
    width: 800
    height: 600

    property bool isNavigating: false

    SceneView {
        id: sceneView
        anchors.fill: parent

        Component.onCompleted: {
            // Set the focus on SceneView to initially enable keyboard navigation
            forceActiveFocus();
        }

        property bool isPressAndHeld: false

        // Declare a Scene
        Scene {
            id: scene
            // Set the basemap
            Basemap {
                initStyle: Enums.BasemapStyleArcGISTopographic
            }

            // Add a Scene Layer
            ArcGISSceneLayer {
                url: "https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Brest/SceneServer/layers/0"
                altitudeOffset: 1
            }

            // Set the Surface
            Surface {
                ArcGISTiledElevationSource {
                    url: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"
                }
            }

            // set initial viewpoint
            ViewpointCenter {
                center: locationDistanceMeasurement.startLocation
                targetScale: 200

                Camera {
                    location: locationDistanceMeasurement.startLocation
                    distance: 400
                    pitch: 45
                    heading: 0
                    roll: 0
                }
            }
        }

        // Declare an AnalysisOverlay
        AnalysisOverlay {
            id: analysisOverlay


            // Declare a Location Distance Measurement
            LocationDistanceMeasurement {
                id: locationDistanceMeasurement

                property string unitLabel: unitSystem === Enums.UnitSystemMetric ? "m" : "ft"

                // set unit system
                unitSystem: Enums.UnitSystemMetric
                // set the start point
                startLocation: Point {
                    x: -4.494677
                    y: 48.384472
                    z: 24.772694
                    spatialReference: SpatialReference { wkid: 4326 }
                }
                // set the end point
                endLocation: Point {
                    x: -4.495646
                    y: 48.384377
                    z: 58.501115
                    spatialReference: SpatialReference { wkid: 4326 }
                }
                // connect to distance change signals
                onDirectDistanceChanged: directDistanceText.text = directDistance.value.toFixed(2) + " %1".arg(directDistance.unit.abbreviation)
                onHorizontalDistanceChanged: horizontalDistanceText.text = horizontalDistance.value.toFixed(2) + " %1".arg(horizontalDistance.unit.abbreviation)
                onVerticalDistanceChanged: verticalDistanceText.text = verticalDistance.value.toFixed(2) + " %1".arg(verticalDistance.unit.abbreviation)
            }
        }

        // handle mouse signals to update the analysis

        // When the mouse is pressed and held, start updating the distance analysis end point
        onMousePressedAndHeld: mouse => {
            isPressAndHeld = true;
            sceneView.screenToLocation(mouse.x, mouse.y);
        }

        // When the mouse is released...
        onMouseReleased: mouse => {
            // Check if the mouse was released from a pan gesture
            if (isNavigating) {
                isNavigating = false;
                return;
            }

            // Ignore if Right click
            if (mouse.button === Qt.RightButton)
                return;

            // If pressing and holding, do nothing
            if (isPressAndHeld)
                isPressAndHeld = false;
            // Else get the location from the screen coordinates
            else
                sceneView.screenToLocation(mouse.x, mouse.y);
        }

        // Set a flag when mousePressed signal emits
        onMousePressed: {
            isNavigating = false;
        }

        // Update the distance analysis when the mouse moves if it is a press and hold movement
        onMousePositionChanged: mouse => {
            if (isPressAndHeld)
                sceneView.screenToLocation(mouse.x, mouse.y);
        }

        // When screenToLocation completes...
        onScreenToLocationCompleted: location => {
            if (isPressAndHeld)
                locationDistanceMeasurement.endLocation = location;
            else
                locationDistanceMeasurement.startLocation = location;
        }

        // Set a flag when viewpointChanged signal emits
        onViewpointChanged: {
            isNavigating = true;
        }
    }

    Rectangle {
        anchors {
            fill: resultsColumn
            margins: -5
        }
        color: "black"
        opacity: 0.5
        radius: 5
    }

    Column {
        id: resultsColumn
        anchors {
            left: parent.left
            top: parent.top
            margins: 10
        }
        spacing: 5

        Row {
            spacing: 5
            Text {
                text: "Direct Distance:"
                color: "white"
            }
            Text {
                id: directDistanceText
                color: "white"
            }
        }
        Row {
            spacing: 5
            Text {
                text: "Vertical Distance:"
                color: "white"
            }
            Text {
                id: verticalDistanceText
                color: "white"
            }
        }
        Row {
            spacing: 5
            Text {
                text: "Horizontal Distance:"
                color: "white"
            }
            Text {
                id: horizontalDistanceText
                color: "white"
            }
        }
        Row {
            spacing: 5
            Text {
                text: "Unit System:"
                color: "white"
            }
            ComboBox {
                id: comboBox
                model: ["Metric", "Imperial"]
                onCurrentTextChanged: {
                    if (currentText === "Metric")
                        locationDistanceMeasurement.unitSystem = Enums.UnitSystemMetric;
                    else
                        locationDistanceMeasurement.unitSystem = Enums.UnitSystemImperial;
                }
            }
        }
    }
}

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