Viewshed location

View inJavaKotlin
View on GitHub
Sample viewer app

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. 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 location, direction, range, and visibility properties of the viewshed instance.
  3. Create an AnalysisOverlay and add the viewshed to it with analysisOverlay.getAnalyses().add(viewshed).
  4. Add the analysis overlay to the SceneView with sceneView.getAnalysisOverlays().add(analysisOverlay).

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

MainActivity.java
                                                                                                                                                                                                                                                                                                                                                                                
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
/* 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.
 *
 */

package com.esri.arcgisruntime.sample.viewshedlocation;

import java.util.concurrent.ExecutionException;

import android.graphics.Color;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geoanalysis.LocationViewshed;
import com.esri.arcgisruntime.geoanalysis.Viewshed;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.layers.ArcGISSceneLayer;
import com.esri.arcgisruntime.mapping.ArcGISScene;
import com.esri.arcgisruntime.mapping.ArcGISTiledElevationSource;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Surface;
import com.esri.arcgisruntime.mapping.view.AnalysisOverlay;
import com.esri.arcgisruntime.mapping.view.Camera;
import com.esri.arcgisruntime.mapping.view.DefaultSceneViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.OrbitLocationCameraController;
import com.esri.arcgisruntime.mapping.view.SceneView;

public class MainActivity extends AppCompatActivity {

  private static final String TAG = MainActivity.class.getSimpleName();

  // initial values
  private static final int mInitHeading = 0;
  private static final int mInitPitch = 60;
  private static final int mInitHorizontalAngle = 75;
  private static final int mInitVerticalAngle = 90;
  private static final int mInitMinDistance = 0;
  private static final int mInitMaxDistance = 1500;

  private SceneView mSceneView;
  private LocationViewshed mViewshed;
  private int mMinDistance;
  private int mMaxDistance;

  private SeekBar mHeadingSeekBar;
  private SeekBar mPitchSeekBar;
  private SeekBar mHorizontalAngleSeekBar;
  private SeekBar mVerticalAngleSeekBar;
  private SeekBar mMinDistanceSeekBar;
  private SeekBar mMaxDistanceSeekBar;
  private TextView mCurrHeading;
  private TextView mCurrPitch;
  private TextView mCurrHorizontalAngle;
  private TextView mCurrVerticalAngle;
  private TextView mCurrMinDistance;
  private TextView mCurrMaxDistance;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // set initial values
    mMinDistance = mInitMinDistance;
    mMaxDistance = mInitMaxDistance;

    // create a scene and add an imagery basemap to it
    mSceneView = findViewById(R.id.sceneView);
    ArcGISScene scene = new ArcGISScene();
    scene.setBasemap(Basemap.createImagery());
    mSceneView.setScene(scene);

    // add base surface for elevation data
    Surface surface = new Surface();
    final String localElevationImageService = getString(
        com.esri.arcgisruntime.sample.viewshedlocation.R.string.elevation_service);
    surface.getElevationSources().add(new ArcGISTiledElevationSource(localElevationImageService));
    scene.setBaseSurface(surface);

    // add a scene layer
    final String buildings = getString(com.esri.arcgisruntime.sample.viewshedlocation.R.string.buildings_layer);
    ArcGISSceneLayer sceneLayer = new ArcGISSceneLayer(buildings);
    scene.getOperationalLayers().add(sceneLayer);

    // create viewshed from location
    Point location = new Point(-4.50, 48.4, 100.0);
    Viewshed.setFrustumOutlineColor(Color.BLUE);
    mViewshed = new LocationViewshed(location, mInitHeading, mInitPitch, mInitHorizontalAngle, mInitVerticalAngle,
        mInitMinDistance, mInitMaxDistance);
    mViewshed.setFrustumOutlineVisible(true);

    // add a camera and set it to orbit the location point
    Camera camera = new Camera(location, 20000000, 0, 55, 0);
    OrbitLocationCameraController orbitCamera = new OrbitLocationCameraController(location, 5000);
    mSceneView.setCameraController(orbitCamera);
    mSceneView.setViewpointCamera(camera);

    // create an analysis overlay to add the mViewshed to the scene view
    AnalysisOverlay analysisOverlay = new AnalysisOverlay();
    analysisOverlay.getAnalyses().add(mViewshed);
    mSceneView.getAnalysisOverlays().add(analysisOverlay);

    handleUiElements();
  }

  /**
   * Handles double touch drag for movement of viewshed location point, inflation of UI elements, and listeners for
   * changes in seek bar progress.
   */
  private void handleUiElements() {
    mSceneView.setOnTouchListener(new DefaultSceneViewOnTouchListener(mSceneView) {
      @Override public boolean onDoubleTouchDrag(MotionEvent motionEvent) {
        // convert from screen point to location point
        android.graphics.Point screenPoint = new android.graphics.Point(Math.round(motionEvent.getX()),
            Math.round(motionEvent.getY()));
        ListenableFuture<Point> locationPointFuture = mSceneView.screenToLocationAsync(screenPoint);
        locationPointFuture.addDoneListener(() -> {
          try {
            Point locationPoint = locationPointFuture.get();

            // add 50 meters to location point and set to viewshed
            mViewshed.setLocation(new Point(locationPoint.getX(), locationPoint.getY(), locationPoint.getZ() + 50));
          } catch (InterruptedException | ExecutionException e) {
            String error = "Error converting screen point to location point: " + e.getMessage();
            Log.e(TAG, error);
            Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
          }
        });

        // ignore default double touch drag gesture
        return true;
      }
    });

    // get views from layout
    mCurrHeading = findViewById(R.id.curr_heading);
    mCurrPitch = findViewById(R.id.curr_pitch);
    mCurrHorizontalAngle = findViewById(R.id.curr_horizontal_angle);
    mCurrVerticalAngle = findViewById(R.id.curr_vertical_angle);
    mCurrMinDistance = findViewById(R.id.curr_minimum_distance);
    mCurrMaxDistance = findViewById(R.id.curr_maximum_distance);

    // heading range 0 - 360
    mHeadingSeekBar = findViewById(R.id.heading_seek_bar);
    mHeadingSeekBar.setMax(360);
    setHeading(mInitHeading);
    mHeadingSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        setHeading(seekBar.getProgress());
      }

      @Override public void onStartTrackingTouch(SeekBar seekBar) {
      }

      @Override public void onStopTrackingTouch(SeekBar seekBar) {
      }
    });

    // set arbitrary max to 180 to avoid nonsensical pitch values
    mPitchSeekBar = findViewById(R.id.pitch_seek_bar);
    mPitchSeekBar.setMax(180);
    setPitch(mInitPitch);
    mPitchSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        setPitch(seekBar.getProgress());
      }

      @Override public void onStartTrackingTouch(SeekBar seekBar) {
      }

      @Override public void onStopTrackingTouch(SeekBar seekBar) {
      }
    });

    // horizontal angle range 1 - 120
    mHorizontalAngleSeekBar = findViewById(R.id.horizontal_angle_seekbar);
    mHorizontalAngleSeekBar.setMax(120);
    setHorizontalAngle(mInitHorizontalAngle);
    mHorizontalAngleSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        int horizontalAngle = mHorizontalAngleSeekBar.getProgress();
        if (horizontalAngle > 0) { // horizontal angle must be > 0
          setHorizontalAngle(horizontalAngle);
        }
      }

      @Override public void onStartTrackingTouch(SeekBar seekBar) {
      }

      @Override public void onStopTrackingTouch(SeekBar seekBar) {
      }
    });

    // vertical angle range 1 - 120
    mVerticalAngleSeekBar = findViewById(R.id.vertical_angle_seekbar);
    mVerticalAngleSeekBar.setMax(120);
    setVerticalAngle(mInitVerticalAngle);
    mVerticalAngleSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        int verticalAngle = mVerticalAngleSeekBar.getProgress();
        if (verticalAngle > 0) { // vertical angle must be > 0
          setVerticalAngle(verticalAngle);
        }
      }

      @Override public void onStartTrackingTouch(SeekBar seekBar) {
      }

      @Override public void onStopTrackingTouch(SeekBar seekBar) {
      }
    });

    // set to 1000 below the arbitrary max
    mMinDistanceSeekBar = findViewById(R.id.min_distance_seekbar);
    mMinDistanceSeekBar.setMax(8999);
    setMinDistance(mInitMinDistance);
    mMinDistanceSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        mMinDistance = seekBar.getProgress();
        if (mMaxDistance - mMinDistance < 1000) {
          mMaxDistance = mMinDistance + 1000;
          setMaxDistance(mMaxDistance);
        }
        setMinDistance(mMinDistance);
      }

      @Override public void onStartTrackingTouch(SeekBar seekBar) {
      }

      @Override public void onStopTrackingTouch(SeekBar seekBar) {
      }
    });

    // set arbitrary max to 9999 to allow a maximum of 4 digits
    mMaxDistanceSeekBar = findViewById(R.id.max_distance_seekbar);
    mMaxDistanceSeekBar.setMax(9999);
    setMaxDistance(mInitMaxDistance);
    mMaxDistanceSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        mMaxDistance = seekBar.getProgress();
        if (mMaxDistance - mMinDistance < 1000) {
          if (mMaxDistance > 1000) {
            mMinDistance = mMaxDistance - 1000;
          } else {
            mMinDistance = 0;
          }
          setMinDistance(mMinDistance);
        }
        setMaxDistance(mMaxDistance);
      }

      @Override public void onStartTrackingTouch(SeekBar seekBar) {
      }

      @Override public void onStopTrackingTouch(SeekBar seekBar) {
      }
    });
  }

  /**
   * Set viewshed heading, seek bar progress, and current heading text view.
   *
   * @param heading in degrees
   */
  private void setHeading(int heading) {
    mViewshed.setHeading(heading);
    mHeadingSeekBar.setProgress(heading);
    mCurrHeading.setText(Integer.toString(heading));
  }

  /**
   * Set viewshed pitch, seek bar progress, and current pitch text view.
   *
   * @param pitch in degrees
   */
  private void setPitch(int pitch) {
    mViewshed.setPitch(pitch);
    mPitchSeekBar.setProgress(pitch);
    mCurrPitch.setText(Integer.toString(pitch));
  }

  /**
   * Set viewshed horizontal angle, seek bar progress, and current horizontal angle text view.
   *
   * @param horizontalAngle in degrees, > 0 and <= 120
   */
  private void setHorizontalAngle(int horizontalAngle) {
    if (horizontalAngle > 0 && horizontalAngle <= 120) {
      mViewshed.setHorizontalAngle(horizontalAngle);
      mHorizontalAngleSeekBar.setProgress(horizontalAngle);
      mCurrHorizontalAngle.setText(Integer.toString(horizontalAngle));
    } else {
      Log.e(TAG, "Horizontal angle must be greater than 0 and less than or equal to 120.");
    }
  }

  /**
   * Set viewshed vertical angle, seek bar progress, and current vertical angle text view.
   *
   * @param verticalAngle in degrees, > 0 and <= 120
   */
  private void setVerticalAngle(int verticalAngle) {
    if (verticalAngle > 0 && verticalAngle <= 120) {
      mViewshed.setVerticalAngle(verticalAngle);
      mVerticalAngleSeekBar.setProgress(verticalAngle);
      mCurrVerticalAngle.setText(Integer.toString(verticalAngle));
    } else {
      Log.e(TAG, "Vertical angle must be greater than 0 and less than or equal to 120.");
    }
  }

  /**
   * Set viewshed minimum distance, seek bar progress, and current minimum distance text view.
   *
   * @param minDistance in meters
   */
  private void setMinDistance(int minDistance) {
    mViewshed.setMinDistance(minDistance);
    mMinDistanceSeekBar.setProgress(minDistance);
    mCurrMinDistance.setText(Integer.toString(minDistance));
  }

  /**
   * Set viewshed maximum distance, seek bar progress, and current maximum distance text view.
   *
   * @param maxDistance in meters
   */
  private void setMaxDistance(int maxDistance) {
    mViewshed.setMaxDistance(maxDistance);
    mMaxDistanceSeekBar.setProgress(maxDistance);
    mCurrMaxDistance.setText(Integer.toString(maxDistance));
  }

  @Override
  protected void onPause() {
    mSceneView.pause();
    super.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mSceneView.resume();
  }

  @Override
  protected void onDestroy() {
    mSceneView.dispose();
    super.onDestroy();
  }
}

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