Viewshed (location)

View inAndroidFormsUWPWPFWinUIiOSView on GitHub

Perform a viewshed analysis from a defined vantage point.

Image of viewshed location

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.

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.cs
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// 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: https://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.

using System;
using System.Drawing;
using CoreGraphics;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using Esri.ArcGISRuntime.UI.Controls;
using Esri.ArcGISRuntime.UI.GeoAnalysis;
using Foundation;
using UIKit;

namespace ArcGISRuntime.Samples.ViewshedLocation
{
    [Register("ViewshedLocation")]
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Viewshed (location)",
        category: "Analysis",
        description: "Perform a viewshed analysis from a defined vantage point.",
        instructions: "Use the sliders to change the properties (heading, pitch, etc.), of the viewshed and see them updated in real time.",
        tags: new[] { "3D", "frustum", "scene", "viewshed", "visibility analysis" })]
    public class ViewshedLocation : UIViewController
    {
        // Hold references to UI controls.
        private SceneView _mySceneView;
        private ViewshedLocationSettingsController _settingsVC;
        private UIBarButtonItem _settingsButton;

        // Hold the URL to the elevation source.
        private readonly Uri _localElevationImageService = new Uri("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer");

        // Hold the URL to the buildings scene layer.
        private readonly Uri _buildingsUrl = new Uri("https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Brest/SceneServer/layers/0");

        // Hold a reference to the viewshed analysis.
        private LocationViewshed _viewshed;

        // Hold a reference to the analysis overlay that will hold the viewshed analysis.
        private AnalysisOverlay _analysisOverlay;

        // Graphics overlay for viewpoint symbol.
        private GraphicsOverlay _viewpointOverlay;

        // Symbol for viewpoint.
        private SimpleMarkerSceneSymbol _viewpointSymbol;

        public ViewshedLocation()
        {
            Title = "Viewshed (location)";
        }

        private void Initialize()
        {
            // Create the scene with the imagery basemap.
            _mySceneView.Scene = new Scene(BasemapStyle.ArcGISImageryStandard);

            // Add the surface elevation.
            Surface mySurface = new Surface();
            mySurface.ElevationSources.Add(new ArcGISTiledElevationSource(_localElevationImageService));
            _mySceneView.Scene.BaseSurface = mySurface;

            // Add the scene layer.
            ArcGISSceneLayer sceneLayer = new ArcGISSceneLayer(_buildingsUrl);
            _mySceneView.Scene.OperationalLayers.Add(sceneLayer);

            // Create the MapPoint representing the initial location.
            MapPoint initialLocation = new MapPoint(-4.5, 48.4, 56.0);

            // Create the location viewshed analysis.
            _viewshed = new LocationViewshed(
                initialLocation,
                0,
                60,
                75,
                90,
                11,
                1500);

            _settingsVC = new ViewshedLocationSettingsController(_viewshed);
            _settingsButton.Enabled = true;

            // Create a camera based on the initial location.
            Camera camera = new Camera(initialLocation, 200.0, 20.0, 70.0, 0.0);

            // Apply the camera to the scene view.
            _mySceneView.SetViewpointCamera(camera);

            // Create an analysis overlay for showing the viewshed analysis.
            _analysisOverlay = new AnalysisOverlay();

            // Add the viewshed analysis to the overlay.
            _analysisOverlay.Analyses.Add(_viewshed);

            // Create a symbol for the viewpoint.
            _viewpointSymbol = SimpleMarkerSceneSymbol.CreateSphere(Color.Blue, 10, SceneSymbolAnchorPosition.Center);

            // Add the symbol to the viewpoint overlay.
            _viewpointOverlay = new GraphicsOverlay
            {
                SceneProperties = new LayerSceneProperties(SurfacePlacement.Absolute)
            };
            _viewpointOverlay.Graphics.Add(new Graphic(initialLocation, _viewpointSymbol));

            // Add the analysis overlay to the SceneView.
            _mySceneView.AnalysisOverlays.Add(_analysisOverlay);

            // Add the graphics overlay
            _mySceneView.GraphicsOverlays.Add(_viewpointOverlay);

            // Update the frustum outline color.
            // The frustum outline shows the volume in which the viewshed analysis is performed.
            Viewshed.FrustumOutlineColor = Color.Blue;
        }

        private void MySceneView_GeoViewTapped(object sender, GeoViewInputEventArgs viewInputEventArgs)
        {
            // Sample isn't ready yet, return.
            if (_viewshed == null) return;

            if (viewInputEventArgs.Location == null)
            {
                // User clicked on the sky - don't update the location with invalid value.
                return;
            }

            // Update the viewshed location.
            _viewshed.Location = viewInputEventArgs.Location;

            // Move the location off of the ground.
            _viewshed.Location = new MapPoint(_viewshed.Location.X, _viewshed.Location.Y, _viewshed.Location.Z + 10.0);

            // Update the viewpoint symbol.
            _viewpointOverlay.Graphics.Clear();
            _viewpointOverlay.Graphics.Add(new Graphic(_viewshed.Location, _viewpointSymbol));
        }

        private void HandleSettings_Clicked(object sender, EventArgs e)
        {
            UINavigationController controller = new UINavigationController(_settingsVC);
            controller.ModalPresentationStyle = UIModalPresentationStyle.Popover;
            controller.PreferredContentSize = new CGSize(300, 320);
            UIPopoverPresentationController pc = controller.PopoverPresentationController;
            if (pc != null)
            {
                pc.BarButtonItem = (UIBarButtonItem) sender;
                pc.PermittedArrowDirections = UIPopoverArrowDirection.Down;
                pc.Delegate = new PpDelegate();
            }

            PresentViewController(controller, true, null);
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            Initialize();
        }

        public override void LoadView()
        {
            // Create the views.
            View = new UIView {BackgroundColor = ApplicationTheme.BackgroundColor};

            _mySceneView = new SceneView();
            _mySceneView.TranslatesAutoresizingMaskIntoConstraints = false;

            _settingsButton = new UIBarButtonItem();
            _settingsButton.Title = "Edit settings";

            UIToolbar toolbar = new UIToolbar();
            toolbar.TranslatesAutoresizingMaskIntoConstraints = false;
            toolbar.Items = new[]
            {
                new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace),
                _settingsButton
            };

            // Add the views.
            View.AddSubviews(_mySceneView, toolbar);

            // Lay out the views.
            NSLayoutConstraint.ActivateConstraints(new[]
            {
                _mySceneView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
                _mySceneView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
                _mySceneView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor),
                _mySceneView.BottomAnchor.ConstraintEqualTo(toolbar.TopAnchor),

                toolbar.BottomAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.BottomAnchor),
                toolbar.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
                toolbar.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor)
            });
        }

        // Force popover to display on iPhone.
        private class PpDelegate : UIPopoverPresentationControllerDelegate
        {
            public override UIModalPresentationStyle GetAdaptivePresentationStyle(
                UIPresentationController forPresentationController) => UIModalPresentationStyle.None;

            public override UIModalPresentationStyle GetAdaptivePresentationStyle(UIPresentationController controller,
                UITraitCollection traitCollection) => UIModalPresentationStyle.None;
        }

        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            // Subscribe to events.
            _mySceneView.GeoViewTapped += MySceneView_GeoViewTapped;
            _settingsButton.Clicked += HandleSettings_Clicked;
        }

        public override void ViewDidDisappear(bool animated)
        {
            base.ViewDidDisappear(animated);

            // Unsubscribe from events, per best practice.
            _mySceneView.GeoViewTapped -= MySceneView_GeoViewTapped;
            _settingsButton.Clicked -= HandleSettings_Clicked;
        }
    }

    public class ViewshedLocationSettingsController : UIViewController
    {
        private readonly LocationViewshed _viewshed;
        private UISlider _headingSlider;
        private UISlider _pitchSlider;
        private UISlider _horizontalAngleSlider;
        private UISlider _verticalAngleSlider;
        private UISlider _minimumDistanceSlider;
        private UISlider _maximumDistanceSlider;
        private UISwitch _analysisVisibilitySwitch;
        private UISwitch _frustumVisibilitySwitch;

        ~ViewshedLocationSettingsController()
        {
            System.Diagnostics.Debug.WriteLine($"Finalized {nameof(ViewshedLocationSettingsController)}");
        }

        public ViewshedLocationSettingsController(LocationViewshed viewshed)
        {
            _viewshed = viewshed;
            Title = "Viewshed settings";
        }

        private void HandleSettingsChange(object sender, EventArgs e)
        {
            // Update the viewshed settings.
            _viewshed.Heading = _headingSlider.Value;
            _viewshed.Pitch = _pitchSlider.Value;
            _viewshed.HorizontalAngle = _horizontalAngleSlider.Value;
            _viewshed.VerticalAngle = _verticalAngleSlider.Value;
            _viewshed.MinDistance = _minimumDistanceSlider.Value;
            _viewshed.MaxDistance = _maximumDistanceSlider.Value;

            // Update visibility of the viewshed analysis.
            _viewshed.IsVisible = _analysisVisibilitySwitch.On;

            // Update visibility of the frustum. Note that the frustum will be invisible
            //     regardless of this setting if the viewshed analysis is not visible.
            _viewshed.IsFrustumOutlineVisible = _frustumVisibilitySwitch.On;
        }

        public override void LoadView()
        {
            // Create and add the container views.
            View = new UIView() { BackgroundColor = ApplicationTheme.BackgroundColor };

            UIScrollView scrollView = new UIScrollView();
            scrollView.TranslatesAutoresizingMaskIntoConstraints = false;

            View.AddSubviews(scrollView);

            scrollView.TopAnchor.ConstraintEqualTo(View.TopAnchor).Active = true;
            scrollView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor).Active = true;
            scrollView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor).Active = true;
            scrollView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor).Active = true;

            UIStackView formContainer = new UIStackView();
            formContainer.TranslatesAutoresizingMaskIntoConstraints = false;
            formContainer.Spacing = 8;
            formContainer.LayoutMarginsRelativeArrangement = true;
            formContainer.Alignment = UIStackViewAlignment.Fill;
            formContainer.LayoutMargins = new UIEdgeInsets(8, 8, 8, 8);
            formContainer.Axis = UILayoutConstraintAxis.Vertical;
            formContainer.WidthAnchor.ConstraintEqualTo(300).Active = true;

            // Create and add each row.
            UILabel analysisLabel = new UILabel();
            analysisLabel.TranslatesAutoresizingMaskIntoConstraints = false;
            analysisLabel.Text = "Analysis overlay";
            _analysisVisibilitySwitch = new UISwitch();
            _analysisVisibilitySwitch.On = _viewshed.IsVisible;
            _analysisVisibilitySwitch.TranslatesAutoresizingMaskIntoConstraints = false;
            formContainer.AddArrangedSubview(getRowStackView(new UIView[] {analysisLabel, _analysisVisibilitySwitch}));

            UILabel frustumLabel = new UILabel();
            frustumLabel.TranslatesAutoresizingMaskIntoConstraints = false;
            frustumLabel.Text = "Frustum";
            _frustumVisibilitySwitch = new UISwitch();
            _frustumVisibilitySwitch.On = _viewshed.IsFrustumOutlineVisible;
            _frustumVisibilitySwitch.TranslatesAutoresizingMaskIntoConstraints = false;
            formContainer.AddArrangedSubview(getRowStackView(new UIView[] {frustumLabel, _frustumVisibilitySwitch}));

            UILabel headingLabel = new UILabel();
            headingLabel.TranslatesAutoresizingMaskIntoConstraints = false;
            headingLabel.Text = "Heading";
            _headingSlider = new UISlider {MinValue = 0, MaxValue = 360, Value = (float) _viewshed.Heading};
            _headingSlider.TranslatesAutoresizingMaskIntoConstraints = false;
            formContainer.AddArrangedSubview(getRowStackView(new UIView[] {headingLabel, _headingSlider}));

            UILabel pitchLabel = new UILabel();
            pitchLabel.TranslatesAutoresizingMaskIntoConstraints = false;
            pitchLabel.Text = "Pitch";
            _pitchSlider = new UISlider {MinValue = 0, MaxValue = 180, Value = (float) _viewshed.Pitch};
            _pitchSlider.TranslatesAutoresizingMaskIntoConstraints = false;
            formContainer.AddArrangedSubview(getRowStackView(new UIView[] {pitchLabel, _pitchSlider}));

            UILabel horizontalLabel = new UILabel();
            horizontalLabel.TranslatesAutoresizingMaskIntoConstraints = false;
            horizontalLabel.Text = "Horizontal";
            _horizontalAngleSlider = new UISlider {MinValue = 1, MaxValue = 120, Value = (float) _viewshed.HorizontalAngle};
            _horizontalAngleSlider.TranslatesAutoresizingMaskIntoConstraints = false;
            formContainer.AddArrangedSubview(getRowStackView(new UIView[] {horizontalLabel, _horizontalAngleSlider}));

            UILabel verticalLabel = new UILabel();
            verticalLabel.TranslatesAutoresizingMaskIntoConstraints = false;
            verticalLabel.Text = "Vertical";
            _verticalAngleSlider = new UISlider {MinValue = 1, MaxValue = 120, Value = (float) _viewshed.VerticalAngle};
            _verticalAngleSlider.TranslatesAutoresizingMaskIntoConstraints = false;
            formContainer.AddArrangedSubview(getRowStackView(new UIView[] {verticalLabel, _verticalAngleSlider}));

            UILabel minLabel = new UILabel();
            minLabel.TranslatesAutoresizingMaskIntoConstraints = false;
            minLabel.Text = "Min";
            _minimumDistanceSlider = new UISlider {MinValue = 11, MaxValue = 8999, Value = (float) _viewshed.MinDistance};
            _minimumDistanceSlider.TranslatesAutoresizingMaskIntoConstraints = false;
            formContainer.AddArrangedSubview(getRowStackView(new UIView[] {minLabel, _minimumDistanceSlider}));

            UILabel maxLabel = new UILabel();
            maxLabel.TranslatesAutoresizingMaskIntoConstraints = false;
            maxLabel.Text = "Max";
            _maximumDistanceSlider = new UISlider {MinValue = 0, MaxValue = 9999, Value = (float) _viewshed.MaxDistance};
            _maximumDistanceSlider.TranslatesAutoresizingMaskIntoConstraints = false;
            formContainer.AddArrangedSubview(getRowStackView(new UIView[] {maxLabel, _maximumDistanceSlider}));

            // Lay out container and scroll view.
            scrollView.AddSubview(formContainer);

            formContainer.TopAnchor.ConstraintEqualTo(scrollView.TopAnchor).Active = true;
            formContainer.LeadingAnchor.ConstraintEqualTo(scrollView.LeadingAnchor).Active = true;
            formContainer.TrailingAnchor.ConstraintEqualTo(scrollView.TrailingAnchor).Active = true;
            formContainer.BottomAnchor.ConstraintEqualTo(scrollView.BottomAnchor).Active = true;
        }

        private UIStackView getRowStackView(UIView[] views)
        {
            UIStackView row = new UIStackView(views);
            row.TranslatesAutoresizingMaskIntoConstraints = false;
            row.Spacing = 8;
            row.Axis = UILayoutConstraintAxis.Horizontal;
            row.Distribution = UIStackViewDistribution.FillEqually;
            return row;
        }

        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);

            // Subscribe to events.
            _headingSlider.ValueChanged += HandleSettingsChange;
            _pitchSlider.ValueChanged += HandleSettingsChange;
            _horizontalAngleSlider.ValueChanged += HandleSettingsChange;
            _verticalAngleSlider.ValueChanged += HandleSettingsChange;
            _minimumDistanceSlider.ValueChanged += HandleSettingsChange;
            _maximumDistanceSlider.ValueChanged += HandleSettingsChange;
            _analysisVisibilitySwitch.ValueChanged += HandleSettingsChange;
            _frustumVisibilitySwitch.ValueChanged += HandleSettingsChange;
        }

        public override void ViewDidDisappear(bool animated)
        {
            base.ViewDidDisappear(animated);

            // Unsubscribe from events, per best practice.
            _headingSlider.ValueChanged -= HandleSettingsChange;
            _pitchSlider.ValueChanged -= HandleSettingsChange;
            _horizontalAngleSlider.ValueChanged -= HandleSettingsChange;
            _verticalAngleSlider.ValueChanged -= HandleSettingsChange;
            _minimumDistanceSlider.ValueChanged -= HandleSettingsChange;
            _maximumDistanceSlider.ValueChanged -= HandleSettingsChange;
            _analysisVisibilitySwitch.ValueChanged -= HandleSettingsChange;
            _frustumVisibilitySwitch.ValueChanged -= HandleSettingsChange;
        }
    }
}

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