Distance measurement analysis

View inC++QMLView on GitHubSample viewer app

Measure distances between two points in 3D.

screenshot

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. Click any location in the scene to start measuring. Move the mouse to an end location, and click to complete the measurement. Click a new location to clear and start a new measurement.

How it works

  1. Create an AnalysisOverlay object and add it to the analysis overlay collection of the SceneView object.
  2. Specify the start location and end location to create a LocationDistanceMeasurement 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 measurementChanged signal will trigger if the distances change. You can get the new values for the directDistance, horizontalDistance, and verticalDistance from the MeasurementChanged object returned by the signal.

Relevant API

  • AnalysisOverlay
  • LocationDistanceMeasurement
  • MeasurementChanged

Additional information

The LocationDistanceMeasurement 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

DistanceMeasurementAnalysis.cppDistanceMeasurementAnalysis.cppDistanceMeasurementAnalysis.hDistanceMeasurementAnalysis.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
// [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]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "DistanceMeasurementAnalysis.h"

#include "ArcGISTiledElevationSource.h"
#include "Scene.h"
#include "SceneQuickView.h"
#include "AnalysisOverlay.h"
#include "LocationDistanceMeasurement.h"
#include "Viewpoint.h"
#include "Camera.h"
#include "ArcGISSceneLayer.h"
#include "Point.h"

using namespace Esri::ArcGISRuntime;

DistanceMeasurementAnalysis::DistanceMeasurementAnalysis(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent)
{
}

void DistanceMeasurementAnalysis::init()
{
  // Register classes for QML
  qmlRegisterType<SceneQuickView>("Esri.Samples", 1, 0, "SceneView");
  qmlRegisterType<DistanceMeasurementAnalysis>("Esri.Samples", 1, 0, "DistanceMeasurementAnalysisSample");
}

void DistanceMeasurementAnalysis::componentComplete()
{
  QQuickItem::componentComplete();

  // Get the Scene View
  m_sceneView = findChild<SceneQuickView*>("sceneView");

  // Create a Scene with the topographic basemap
  Scene* scene = new Scene(BasemapStyle::ArcGISTopographic, this);

  // Add a Scene Layer
  ArcGISSceneLayer* sceneLayer = new ArcGISSceneLayer(QUrl("https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Brest/SceneServer/layers/0"), this);
  sceneLayer->setAltitudeOffset(1); // The elevation source is a very fine resolution so we raise the scene layer slightly so it does not clip the surface

  scene->operationalLayers()->append(sceneLayer);

  // Create and set the surface on the scene
  Surface* surface = new Surface(this);
  surface->elevationSources()->append(
        new ArcGISTiledElevationSource(QUrl("https://scene.arcgis.com/arcgis/rest/services/BREST_DTM_1M/ImageServer"),this));
  scene->setBaseSurface(surface);

  // Add Analysis Overlay
  AnalysisOverlay* analysisOverlay = new AnalysisOverlay(this);
  m_sceneView->analysisOverlays()->append(analysisOverlay);

  // Create and add the LocationDistanceMeasurement
  const Point startLocation(-4.494677, 48.384472, 24.772694, SpatialReference::wgs84());
  const Point endLocation(-4.495646, 48.384377, 58.501115, SpatialReference::wgs84());
  m_distanceAnalysis = new LocationDistanceMeasurement(startLocation, endLocation, this);
  m_distanceAnalysis->setUnitSystem(UnitSystem::Metric);
  analysisOverlay->analyses()->append(m_distanceAnalysis);

  // Set initial viewpoint
  constexpr double distance = 400.0;
  constexpr double pitch = 45.0;
  constexpr double heading = 0.0;
  constexpr double roll = 0.0;
  const Camera initCamera(startLocation, distance, heading, pitch, roll);
  const Viewpoint initViewpoint(startLocation, distance, initCamera);
  scene->setInitialViewpoint(initViewpoint);

  // Set the scene on the scene view
  m_sceneView->setArcGISScene(scene);

  connectSignals();
}

void DistanceMeasurementAnalysis::connectSignals()
{
  // connect to signal to obtain updated distances
  connect(m_distanceAnalysis, &LocationDistanceMeasurement::measurementChanged, this, [this](const Distance& directDistance,
                                                                                             const Distance& horizontalDistance,
                                                                                             const Distance& verticalDistance)
  {
    const QString unitLabel = m_distanceAnalysis->unitSystem() == UnitSystem::Metric ? "m" : "ft";
    m_directDistance = QString::number(directDistance.value(), 'f', 2) + QString(" %1").arg(unitLabel);
    m_horizontalDistance = QString::number(horizontalDistance.value(), 'f', 2) + QString(" %1").arg(unitLabel);
    m_verticalDistance = QString::number(verticalDistance.value(), 'f', 2) + QString(" %1").arg(unitLabel);
    emit directDistanceChanged();
    emit horizontalDistanceChanged();
    emit verticalDistanceChanged();
  });

  // connect to mouse signals to update the analysis

  // When the mouse is pressed and held, start updating the distance analysis end point
  connect(m_sceneView, &SceneQuickView::mousePressedAndHeld, this, [this](QMouseEvent& mouseEvent)
  {
    m_isPressAndHold = true;
    m_sceneView->screenToLocation(mouseEvent.x(), mouseEvent.y());
  });

  // When the mouse is released...
  connect(m_sceneView, &SceneQuickView::mouseReleased, this, [this](QMouseEvent& mouseEvent)
  {
    // Check if the mouse was released from a pan gesture
    if (m_isNavigating)
    {
      m_isNavigating = false;
      return;
    }

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

    // If pressing and holding, do nothing
    if (m_isPressAndHold)
      m_isPressAndHold = false;
    // Else get the location from the screen coordinates
    else
      m_sceneView->screenToLocation(mouseEvent.x(), mouseEvent.y());
  });

  // Update the distance analysis when the mouse moves if it is a press and hold movement
  connect(m_sceneView, &SceneQuickView::mouseMoved, this, [this](QMouseEvent& mouseEvent)
  {
    if (m_isPressAndHold)
      m_sceneView->screenToLocation(mouseEvent.x(), mouseEvent.y());
  });

  // Set a flag when mousePressed signal emits
  connect(m_sceneView, &SceneQuickView::mousePressed, this, [this]
  {
    m_isNavigating = false;
  });

  // When screenToLocation completes...
  connect(m_sceneView, &SceneQuickView::screenToLocationCompleted, this, [this](QUuid, Point pt)
  {
    // If it was from a press and hold, update the end location
    if (m_isPressAndHold)
      m_distanceAnalysis->setEndLocation(pt);
    // Else if it was a normal mouse click (press and release), update the start location
    else
      m_distanceAnalysis->setStartLocation(pt);
  });

  // Set a flag when viewpointChanged signal emits
  connect(m_sceneView, &SceneQuickView::viewpointChanged, this, [this]
  {
    m_isNavigating = true;
  });
}

void DistanceMeasurementAnalysis::setUnits(const QString& unitName)
{
  if (!m_distanceAnalysis)
    return;

  if (unitName == "Metric")
    m_distanceAnalysis->setUnitSystem(UnitSystem::Metric);
  else
    m_distanceAnalysis->setUnitSystem(UnitSystem::Imperial);
}

QString DistanceMeasurementAnalysis::directDistance() const
{
  return m_directDistance;
}

QString DistanceMeasurementAnalysis::horizontalDistance() const
{
  return m_horizontalDistance;
}

QString DistanceMeasurementAnalysis::verticalDistance() const
{
  return m_verticalDistance;
}

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