Viewshed (location)

View inQMLC++
View on GitHub
Sample viewer app

Perform a viewshed analysis from a defined vantage point.

screenshot

Use case

A 3D viewshed analysis is a type of visual analysis you can perform on a scene. The viewshed shows what can be seen from a given location. The output is an overlay with two different colors - one representing the visible areas (green) and the other representing the obstructed areas (red). Viewshed analysis is a form of "exploratory analysis", which means the results are calculated on the current scale of the data, and the results are generated very quickly. If more "conclusive" results are required, consider using a GeoprocessingTask to perform a viewshed instead.

How to use the sample

Use the sliders to change the properties (heading, pitch, etc.), of the viewshed and see them updated in real time. To move the viewshed, double touch and drag your finger across the screen. Lift your finger to stop moving the viewshed.

How it works

  1. Create a LocationViewshed passing in the observer location, heading, pitch, horizontal/vertical angles, and min/max distances.
  2. Set the property values on the viewshed instance for location, direction, range, and visibility properties.

Relevant API

  • AnalysisOverlay
  • ArcGISSceneLayer
  • ArcGISTiledElevationSource
  • LocationViewshed
  • Viewshed

About the data

The scene shows a buildings layer in Brest, France hosted on ArcGIS Online.

Tags

3D, frustum, scene, viewshed, visibility analysis

Sample Code

ViewshedLocation.cppViewshedLocation.cppViewshedLocation.hColorDialog.qmlViewshedSlider.qmlViewshedLocation.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// [WriteFile Name=ViewshedLocation, Category=Analysis]
// [Legal]
// 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.
// [Legal]

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

#include "ViewshedLocation.h"

#include "ArcGISTiledElevationSource.h"
#include "Scene.h"
#include "SceneQuickView.h"
#include "LocationViewshed.h"
#include "Camera.h"
#include "Point.h"
#include "AnalysisOverlay.h"


using namespace Esri::ArcGISRuntime;

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

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

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

  // Create a scene and give it to the SceneView
  m_sceneView = findChild<SceneQuickView*>("sceneView");

  Scene* scene = new Scene(BasemapStyle::ArcGISTopographic, this);
  Surface* surface = new Surface(this);
  surface->elevationSources()->append(
        new ArcGISTiledElevationSource(
          QUrl("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"),
          this));
  scene->setBaseSurface(surface);
  m_sceneView->setArcGISScene(scene);

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

  // set initial viewpoint
  setInitialViewpoint();

  // connect signals
  connectSignals();
}

void ViewshedLocation::setInitialViewpoint()
{
  // Set a viewpoint
  double x = 6.86088;
  double y = 45.3604;
  double z = 3582.55;
  Point point(x, y, z, SpatialReference(4326));
  double heading = 345;
  double pitch = 70;
  double roll = 0;
  Camera camera(point, heading, pitch, roll);
  m_sceneView->setViewpointCamera(camera);
}

void ViewshedLocation::connectSignals()
{
  // on mouse click perform the location viewshed
  connect(m_sceneView, &SceneQuickView::mouseClicked, this, [this](QMouseEvent& event)
  {
    if (!m_locationViewshed)
      createViewshed(event.x(), event.y());
    else
    {
      const Point pt = m_sceneView->screenToBaseSurface(event.x(), event.y());
      m_locationViewshed->setLocation(pt);
    }
  });

  connect(m_sceneView, &SceneQuickView::mousePressedAndHeld, this, [this](QMouseEvent& event)
  {
    if (!m_locationViewshed)
      createViewshed(event.x(), event.y());

    m_calculating = true;
  });

  connect(m_sceneView, &SceneQuickView::mouseMoved, this, [this](QMouseEvent& event)
  {
    if (m_calculating)
    {
      const Point pt = m_sceneView->screenToBaseSurface(event.x(), event.y());
      m_locationViewshed->setLocation(pt);
    }
  });

  connect(m_sceneView, &SceneQuickView::mouseReleased, this, [this]
  {
    m_calculating = false;
  });
}

void ViewshedLocation::createViewshed(double x, double y)
{
  const Point pt = m_sceneView->screenToBaseSurface(x, y);

  // Create the Location Viewshed
  m_locationViewshed = new LocationViewshed(pt, m_heading, m_pitch,
                                            m_horizontalAngle, m_veriticalAngle,
                                            m_minDistance, m_maxDistance, this);
  m_locationViewshed->setVisible(m_viewshedVisible);

  // Add the Viewshed to the Analysis Overlay
  m_analysisOverlay->analyses()->append(m_locationViewshed);

  return;
}

// Getters/Setters for each Q_PROPERTY
bool ViewshedLocation::isViewshedVisible() const
{
  return m_viewshedVisible;
}

void ViewshedLocation::setViewshedVisible(bool viewshedVisible)
{
  if (m_locationViewshed)
  {
    if (m_locationViewshed->isVisible() == viewshedVisible)
      return;

    m_viewshedVisible = viewshedVisible;
    m_locationViewshed->setVisible(viewshedVisible);
  }
  else
  {
    if (m_viewshedVisible == viewshedVisible)
      return;

    m_viewshedVisible = viewshedVisible;
  }

  emit viewshedVisibleChanged();
}

bool ViewshedLocation::isFrustumOutlineVisible() const
{
  return m_locationViewshed ? m_locationViewshed->isFrustumOutlineVisible() : m_frustumVisible;
}

void ViewshedLocation::setFrustumOutlineVisible(bool frustumVisible)
{
  if (m_locationViewshed)
  {
    if (m_locationViewshed->isFrustumOutlineVisible() == frustumVisible)
      return;

    m_frustumVisible = frustumVisible;
    m_locationViewshed->setFrustumOutlineVisible(frustumVisible);
  }
  else
  {
    if (m_frustumVisible == frustumVisible)
      return;

    m_frustumVisible = frustumVisible;
  }

  emit frustumVisibleChanged();
}

double ViewshedLocation::minDistance() const
{
  return m_locationViewshed ? m_locationViewshed->minDistance() : m_minDistance;
}

void ViewshedLocation::setMinDistance(double minDistance)
{
  if (m_locationViewshed)
  {
    if (m_locationViewshed->minDistance() == minDistance)
      return;

    m_minDistance = minDistance;
    m_locationViewshed->setMinDistance(minDistance);
  }
  else
  {
    if (m_minDistance == minDistance)
      return;

    m_minDistance = minDistance;
  }

  emit minDistanceChanged();
}

double ViewshedLocation::maxDistance() const
{
  return m_locationViewshed ? m_locationViewshed->maxDistance() : m_maxDistance;
}

void ViewshedLocation::setMaxDistance(double maxDistance)
{
  m_maxDistance = maxDistance;

  if (m_locationViewshed)
  {
    if (m_locationViewshed->maxDistance() == maxDistance)
      return;

    m_maxDistance = maxDistance;
    m_locationViewshed->setMaxDistance(maxDistance);
  }
  else
  {
    if (m_maxDistance == maxDistance)
      return;

    m_maxDistance = maxDistance;
  }

  emit maxDistanceChanged();
}

double ViewshedLocation::horizontalAngle() const
{
  return m_locationViewshed ? m_locationViewshed->horizontalAngle() : m_horizontalAngle;
}

void ViewshedLocation::setHorizontalAngle(double horizontalAngle)
{
  if (m_locationViewshed)
  {
    if (m_locationViewshed->horizontalAngle() == horizontalAngle)
      return;

    m_horizontalAngle = horizontalAngle;
    m_locationViewshed->setHorizontalAngle(horizontalAngle);
  }
  else
  {
    if (m_horizontalAngle == horizontalAngle)
      return;

    m_horizontalAngle = horizontalAngle;
  }

  emit horizontalAngleChanged();
}

double ViewshedLocation::verticalAngle() const
{
  return m_locationViewshed ? m_locationViewshed->verticalAngle() : m_veriticalAngle;
}

void ViewshedLocation::setVerticalAngle(double verticalAngle)
{
  if (m_locationViewshed)
  {
    if (m_locationViewshed->verticalAngle() == verticalAngle)
      return;

    m_veriticalAngle = verticalAngle;
    m_locationViewshed->setVerticalAngle(verticalAngle);
  }
  else
  {
    if (m_veriticalAngle == verticalAngle)
      return;

    m_veriticalAngle = verticalAngle;
  }

  emit verticalAngleChanged();
}

double ViewshedLocation::heading() const
{
  return m_locationViewshed ? m_locationViewshed->heading() : m_heading;
}

void ViewshedLocation::setHeading(double heading)
{
  if (m_locationViewshed)
  {
    if (m_locationViewshed->heading() == heading)
      return;

    m_heading = heading;
    m_locationViewshed->setHeading(heading);
  }
  else
  {
    if (m_heading == heading)
      return;

    m_heading = heading;
  }

  emit headingChanged();
}

double ViewshedLocation::pitch() const
{
  return m_locationViewshed ? m_locationViewshed->pitch() : m_pitch;
}

void ViewshedLocation::setPitch(double pitch)
{
  if (m_locationViewshed)
  {
    if (m_locationViewshed->pitch() == pitch)
      return;

    m_pitch = pitch;
    m_locationViewshed->setPitch(pitch);
  }
  else
  {
    if (m_pitch == pitch)
      return;

    m_pitch = pitch;
  }

  emit pitchChanged();
}

QColor ViewshedLocation::visibleColor() const
{
  return LocationViewshed::visibleColor();
}

void ViewshedLocation::setVisibleColor(const QColor& visibleColor)
{
  if (LocationViewshed::visibleColor() == visibleColor)
    return;

  LocationViewshed::setVisibleColor(visibleColor);

  emit visibleColorChanged();
}

QColor ViewshedLocation::obstructedColor() const
{
  return LocationViewshed::obstructedColor();
}

void ViewshedLocation::setObstructedColor(const QColor& obstructedColor)
{
  if (LocationViewshed::obstructedColor() == obstructedColor)
    return;

  LocationViewshed::setObstructedColor(obstructedColor);

  emit obstructedColorChanged();
}

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