Line of sight (geoelement)

View inC++QMLView on GitHubSample viewer app

Show a line of sight between two moving objects.

screenshot

Use case

A line of sight between GeoElements (i.e. observer and target) will not remain constant whilst one or both are on the move.

A GeoElementLineOfSight is therefore useful in cases where visibility between two GeoElements requires monitoring over a period of time in a partially obstructed field of view (such as buildings in a city).

How to use the sample

A line of sight will display between a point on the Empire State Building (observer) and a taxi (target). The taxi will drive around a block and the line of sight should automatically update. The taxi will be highlighted when it is visible. You can change the observer height with the slider to see how it affects the target's visibility.

How it works

  1. Instantiate an AnalysisOverlay and add it to the SceneView's analysis overlays collection.
  2. Instantiate a GeoElementLineOfSight, passing in observer and target GeoElements (features or graphics). Add the line of sight to the analysis overlay's analyses collection.
  3. To get the target visibility when it changes, react to the target visibility changing on the GeoElementLineOfSight instance.

Relevant API

  • AnalysisOverlay
  • GeoElementLineOfSight
  • LineOfSight::targetVisibility

Offline data

Link Local Location
Taxi CAD <userhome>/ArcGIS/Runtime/Data/3D/dolmus\_3ds/dolmus.zip

Tags

3D, line of sight, visibility, visibility analysis

Sample Code

LineOfSightGeoElement.cppLineOfSightGeoElement.cppLineOfSightGeoElement.hLineOfSightGeoElement.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// [WriteFile Name=LineOfSightGeoElement, Category=Analysis]
// [Legal]
// Copyright 2019 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 "LineOfSightGeoElement.h"

#include "ArcGISSceneLayer.h"
#include "ArcGISTiledElevationSource.h"
#include "GeoElementLineOfSight.h"
#include "GeometryEngine.h"
#include "GraphicsOverlay.h"
#include "GraphicsOverlayListModel.h"
#include "ModelSceneSymbol.h"
#include "PointBuilder.h"
#include "Scene.h"
#include "SceneQuickView.h"
#include "SceneViewTypes.h"
#include "SimpleRenderer.h"
#include "MapTypes.h"
#include "Surface.h"
#include "ElevationSourceListModel.h"
#include "LayerListModel.h"
#include "LayerSceneProperties.h"
#include "RendererSceneProperties.h"
#include "SymbolTypes.h"
#include "GraphicListModel.h"
#include "Error.h"
#include "AttributeListModel.h"
#include "AnalysisOverlay.h"
#include "AnalysisOverlayListModel.h"
#include "AnalysisListModel.h"
#include "Camera.h"
#include "GeodeticDistanceResult.h"
#include "LinearUnit.h"
#include "AngularUnit.h"
#include "SpatialReference.h"
#include "Point.h"
#include "Graphic.h"
#include "SimpleMarkerSymbol.h"

#include <array>
#include <QFuture>
#include <QtCore/qglobal.h>
#include <QStandardPaths>

using namespace Esri::ArcGISRuntime;

// helper method to get cross platform data path
namespace
{
  QString defaultDataPath()
  {
    QString dataPath;

  #ifdef Q_OS_IOS
    dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
  #else
    dataPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
  #endif

    return dataPath;
  }
} // namespace

namespace
{

// Initial fixed point to observe taxi.
const Point observationPoint(-73.9853, 40.7484, 200, SpatialReference::wgs84());

// Waypoints around the block for taxi to drive.
const std::array<Point, 4> waypoints = {{
                                          { -73.984513, 40.748469, 2, SpatialReference::wgs84() },
                                          { -73.985068, 40.747786, 2, SpatialReference::wgs84() },
                                          { -73.983452, 40.747091, 2, SpatialReference::wgs84() },
                                          { -73.982961, 40.747762, 2, SpatialReference::wgs84() }
                                        }};
}

LineOfSightGeoElement::LineOfSightGeoElement(QObject* parent /* = nullptr */):
  QObject(parent),
  m_scene(new Scene(BasemapStyle::ArcGISImageryStandard, this))
{
  // create a new elevation source from Terrain3D rest service
  ArcGISTiledElevationSource* elevationSource = new ArcGISTiledElevationSource(
        QUrl("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"), this);

  // add the elevation source to the scene to display elevation
  m_scene->baseSurface()->elevationSources()->append(elevationSource);

  // Load up the buildings that will block the line of sight.
  ArcGISSceneLayer* buildings = new ArcGISSceneLayer(
        QUrl("https://tiles.arcgis.com/tiles/z2tnIkrLQ2BRzr6P/arcgis/rest/services/New_York_LoD2_3D_Buildings/SceneServer/layers/0"));
  m_scene->operationalLayers()->append(buildings);

  // Trigger animation of taxi every 100ms.
  m_animation.setInterval(100);
  m_animation.callOnTimeout(this, &LineOfSightGeoElement::animate);
}

LineOfSightGeoElement::~LineOfSightGeoElement() = default;

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

double LineOfSightGeoElement::heightZ() const
{
  if (m_observer)
  {
    const Point p = geometry_cast<Point>(m_observer->geometry());
    return p.isValid() ? p.z() : 0.0;
  }
  else
  {
    return 0.0;
  }
}
void LineOfSightGeoElement::setHeightZ(double z)
{
  if (!m_observer)
    return;

  const Point p = geometry_cast<Point>(m_observer->geometry());
  if (p.isValid())
  {
    PointBuilder builder(p);
    builder.setZ(z);
    m_observer->setGeometry(builder.toGeometry());
    emit heightZChanged();
  }
}

SceneQuickView* LineOfSightGeoElement::sceneView() const
{
  return m_sceneView;
}

// Set the view (created in QML)
void LineOfSightGeoElement::setSceneView(SceneQuickView* sceneView)
{
  if (!sceneView || sceneView == m_sceneView)
  {
    return;
  }

  m_sceneView = sceneView;
  m_sceneView->setArcGISScene(m_scene);
  emit sceneViewChanged();

  if (!defaultDataPath().isEmpty() && m_sceneView)
    initialize();
}

void LineOfSightGeoElement::initialize()
{
  // Setup the graphics overlay - ensure that all z-position are relative to the height of the surface.
  GraphicsOverlay* graphicsOverlay = new GraphicsOverlay(this);
  {
    LayerSceneProperties properties = graphicsOverlay->sceneProperties();
    properties.setSurfacePlacement(SurfacePlacement::Relative);
    graphicsOverlay->setSceneProperties(properties);
  }
  m_sceneView->graphicsOverlays()->append(graphicsOverlay);

  // Set up the renderer so that we can orient the taxi using the `HEADING` attribute.
  SimpleRenderer* renderer3D = new SimpleRenderer(this);
  {
    RendererSceneProperties properties = renderer3D->sceneProperties();
    properties.setHeadingExpression("[HEADING]");
    renderer3D->setSceneProperties(properties);
  }
  graphicsOverlay->setRenderer(renderer3D);

  // Create our observation point as a red sphere.
  m_observer = new Graphic(observationPoint, new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, Qt::red, 5.f), this);
  emit heightZChanged(); // We now have a point to extract the Z-height from.
  graphicsOverlay->graphics()->append(m_observer);

  // Get our Taxi model. We will attempt to load it and continue our setup after it has loaded.
  const QUrl modelPath = defaultDataPath() + "/ArcGIS/Runtime/Data/3D/dolmus_3ds/dolmus.3ds";
  ModelSceneSymbol* taxiSymbol = new ModelSceneSymbol(modelPath, 1.0, this);
  taxiSymbol->setAnchorPosition(SceneSymbolAnchorPosition::Bottom);

  connect(taxiSymbol, &ModelSceneSymbol::doneLoading, this,
          [this, graphicsOverlay, taxiSymbol](const Error& error)
  {
    if (!error.isEmpty())
    {
      return;
    }

    // Create taxi from loaded taxi symbol, with an initial "HEADING" attribute for orientation.
    m_taxi = new Graphic(waypoints.front(), taxiSymbol, this);
    m_taxi->attributes()->insertAttribute("HEADING", 0.0);
    graphicsOverlay->graphics()->append(m_taxi);

    // Set up our line of sight analysis.
    AnalysisOverlay*  analysisOverlay = new AnalysisOverlay(this);
    m_sceneView->analysisOverlays()->append(analysisOverlay);

    GeoElementLineOfSight* lineOfSight = new GeoElementLineOfSight(m_observer, m_taxi, this);
    analysisOverlay->analyses()->append(lineOfSight);

    connect(lineOfSight, &GeoElementLineOfSight::targetVisibilityChanged, this,
            [this](LineOfSightTargetVisibility targetVisibility)
    {
      // Select taxi whenever there is a clear line of sight from observer position.
      m_taxi->setSelected(targetVisibility == LineOfSightTargetVisibility::Visible);
    });

    Camera camera(observationPoint, 700, -30, 45, 0);
    m_sceneView->setViewpointCameraAsync(camera, 0);

    m_animation.start();
  });

  taxiSymbol->load();
}

void LineOfSightGeoElement::animate()
{
  // Goal point to travel to
  Point waypoint = waypoints.at(m_waypointIndex);

  // Calculate azimuth between current location and goal location for taxi.
  Point location = geometry_cast<Point>(m_taxi->geometry());

  if (!location.isValid())
    return;

  GeodeticDistanceResult distance = GeometryEngine::distanceGeodetic(
        location, waypoint, LinearUnit::meters(), AngularUnit::degrees(), GeodeticCurveType::Geodesic);

  // Move taxi one metre along the line between its current position and the goal location.
  QList<Point> newPoints = GeometryEngine::moveGeodetic(
  { location }, 1.0, LinearUnit::meters(), distance.azimuth1(), AngularUnit::degrees(), GeodeticCurveType::Geodesic);
  if (newPoints.size() > 0)
  {
    location = newPoints.last();
  }

  // Update taxi position and orientation.
  m_taxi->setGeometry(geometry_cast<Geometry>(location));
  m_taxi->attributes()->replaceAttribute("HEADING", distance.azimuth1());

  // When taxi is close enough to the waypoint then increment the index for a new goal next animation step.
  // Index is cyclic and an element of [0, 3].
  if (distance.distance() <= 2) {
    m_waypointIndex = (m_waypointIndex + 1) % waypoints.size();
  }
}

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