Viewshed GeoElement

View on GitHub
Sample viewer app

Analyze the viewshed for an object (GeoElement) in a scene.

Image of viewshed geoelement

Use case

A viewshed analysis is a type of visual analysis you can perform on a scene. The viewshed aims to answer the question 'What can I see 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).

How to use the sample

Tap to set a destination for the vehicle (a GeoElement). The vehicle will 'drive' towards the tapped location. The viewshed analysis will update as the vehicle moves.

How it works

  1. Create and show the scene, with an elevation source and a buildings layer.
  2. Add a model (the GeoElement) to represent the observer (in this case, a tank).

    • Use a SimpleRenderer which has a heading expression set in the GraphicsOverlay. This way you can relate the viewshed's heading to the GeoElement object's heading.
  3. Create a GeoElementViewshed with configuration for the viewshed analysis.
  4. Add the viewshed to an AnalysisOverlay and add the overlay to the scene.
  5. Configure the SceneView CameraController to orbit the vehicle.

About the data

This sample shows a Johannesburg, South Africa Scene from ArcGIS Online. The sample uses a Tank model scene symbol hosted as an item on ArcGIS Online.

Relevant API

  • AnalysisOverlay
  • GeodeticDistanceResult
  • GeoElementViewshed
  • GeometryEngine.distanceGeodetic (used to animate the vehicle)
  • ModelSceneSymbol
  • OrbitGeoElementCameraController


3D, analysis, buildings, model, scene, viewshed, visibility analysis

Sample Code
/* Copyright 2018 ESRI
 * All rights reserved under the copyright laws of the United States
 * and applicable international laws, treaties, and conventions.
 * You may freely redistribute and use this sample code, with or
 * without modification, provided you include the original copyright
 * notice and use restrictions.
 * See the Sample code usage restrictions document for further information.

package com.esri.arcgisruntime.sample.viewshedgeoelement;

import java.util.Timer;
import java.util.TimerTask;

import android.Manifest;
import android.content.res.AssetManager;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import com.esri.arcgisruntime.geoanalysis.GeoElementViewshed;
import com.esri.arcgisruntime.geometry.AngularUnit;
import com.esri.arcgisruntime.geometry.AngularUnitId;
import com.esri.arcgisruntime.geometry.GeodeticCurveType;
import com.esri.arcgisruntime.geometry.GeodeticDistanceResult;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.LinearUnit;
import com.esri.arcgisruntime.geometry.LinearUnitId;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReferences;
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.DefaultSceneViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.LayerSceneProperties;
import com.esri.arcgisruntime.mapping.view.OrbitGeoElementCameraController;
import com.esri.arcgisruntime.mapping.view.SceneView;
import com.esri.arcgisruntime.symbology.ModelSceneSymbol;
import com.esri.arcgisruntime.symbology.Renderer;
import com.esri.arcgisruntime.symbology.SceneSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;

public class MainActivity extends AppCompatActivity {

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

  private static final LinearUnit METERS = new LinearUnit(LinearUnitId.METERS);
  private static final AngularUnit DEGREES = new AngularUnit(AngularUnitId.DEGREES);
  private SceneView mSceneView;
  private Point mWaypoint;
  private Graphic mTankGraphic;
  private Timer mTimer;

  protected void onCreate(Bundle savedInstanceState) {

    // create a scene and add a basemap to it
    ArcGISScene scene = new ArcGISScene();

    // add the SceneView to the stack pane
    mSceneView = findViewById(;

    // add base surface for elevation data
    Surface surface = new Surface();
    surface.getElevationSources().add(new ArcGISTiledElevationSource(getString(R.string.elevation_service)));

    // add a scene layer
    ArcGISSceneLayer sceneLayer = new ArcGISSceneLayer(getString(R.string.buildings_layer));

    // request read permission

   * Creates a GeoElement Viewshed fixed to a graphic of a tank. Includes a touch listener which uses a single tap as a
   * waypoint for navigation of the tank and associated viewshed.
  private void viewshedGeoElement() {

    // load tank model from assets into cache directory

    // create a graphics overlay for the tank
    GraphicsOverlay graphicsOverlay = new GraphicsOverlay();

    // set up heading expression for tank
    SimpleRenderer renderer3D = new SimpleRenderer();
    Renderer.SceneProperties renderProperties = renderer3D.getSceneProperties();

    String pathToModel = getCacheDir() + File.separator + getString(R.string.bradley_model);

    ModelSceneSymbol tankSymbol = new ModelSceneSymbol(pathToModel, 10.0);
    mTankGraphic = new Graphic(new Point(-4.506390, 48.385624, SpatialReferences.getWgs84()), tankSymbol);
    mTankGraphic.getAttributes().put("HEADING", 0.0);

    // create a viewshed to attach to the tank
    GeoElementViewshed geoElementViewshed = new GeoElementViewshed(mTankGraphic, 90.0, 40.0, 0.1, 250.0, 0.0, 0.0);
    // offset viewshed observer location to top of tank

    // create an analysis overlay to add the viewshed to the scene view
    AnalysisOverlay analysisOverlay = new AnalysisOverlay();

    // set the waypoint where the user taps
    mSceneView.setOnTouchListener(new DefaultSceneViewOnTouchListener(mSceneView) {
      @Override public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
        // get a screen point from the motion event screenPoint = new,

        // convert the screen point to a scene point
        mWaypoint = mSceneView.screenToBaseSurface(screenPoint);

        // create a timer to animate the tank
        mTimer = new Timer();
        mTimer.scheduleAtFixedRate(new TimerTask() {
          @Override public void run() {
        }, 0, 50);

        return true;

    // set camera controller to follow tank
    OrbitGeoElementCameraController cameraController = new OrbitGeoElementCameraController(mTankGraphic, 200.0);

   * Moves the tank toward the current waypoint a short distance.
  private void animate() {
    if (mWaypoint != null) {
      // get current location and distance from waypoint
      Point location = (Point) mTankGraphic.getGeometry();
      GeodeticDistanceResult distance = GeometryEngine
          .distanceGeodetic(location, mWaypoint, METERS, DEGREES, GeodeticCurveType.GEODESIC);

      // move toward waypoint a short distance
      location = GeometryEngine
          .moveGeodetic(location, 1.0, METERS, distance.getAzimuth1(), DEGREES, GeodeticCurveType.GEODESIC);

      // rotate toward waypoint
      double heading = (double) mTankGraphic.getAttributes().get("HEADING");
      mTankGraphic.getAttributes().put("HEADING", heading + ((distance.getAzimuth1() - heading) / 10));

      // reached waypoint, stop moving and set waypoint to null
      if (distance.getDistance() <= 5) {
        mWaypoint = null;

   * Request write permission on the device.
  private void requestWritePermission() {
    // define permission to request
    String[] reqPermission = new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE };
    int requestCode = 2;
    // For API level 23+ request permission at runtime
    if (ContextCompat.checkSelfPermission(MainActivity.this,
        reqPermission[0]) == PackageManager.PERMISSION_GRANTED) {
    } else {
      // request permission
      ActivityCompat.requestPermissions(MainActivity.this, reqPermission, requestCode);

   * Handle permission request response.
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    } else {
      // report to user that permission was denied
      Toast.makeText(MainActivity.this, getResources().getString(R.string.write_permission_denied),

   * Copy the given file from the app's assets folder to the app's cache directory.
   * @param fileName as String
  private void copyFileFromAssetsToCache(String fileName) {
    AssetManager assetManager = getApplicationContext().getAssets();

    File file = new File(getCacheDir() + File.separator + fileName);

    if (!file.exists()) {
      try {
        InputStream in =;
        OutputStream out = new FileOutputStream(getCacheDir() + File.separator + fileName);
        byte[] buffer = new byte[1024];
        int read =;
        while (read != -1) {
          out.write(buffer, 0, read);
          read =;
        Log.i(TAG, fileName + " copied to cache.");
      } catch (Exception e) {
        Log.e(TAG, "Error writing " + fileName + " to cache. " + e.getMessage());
    } else {
      Log.i(TAG, fileName + " already in cache.");

  protected void onPause() {
    // pause SceneView

  protected void onResume() {
    // resume SceneView

  protected void onDestroy() {
    // dispose SceneView