ArcGIS Runtime SDK for Java

Line of Sight GeoElement

Shows how to display a line of sight between two (possibly moving) graphics.

To determine if an observer can see a target, you can show a line of sight between them. The line will be green until it is obstructed, in which case it will turn red. By using the GeoElement variant of the line of sight, the line will automatically update when either GeoElement moves.

How it works

To show a line of sight between two graphics:

  1. Create an AnalysisOverlay and add it to the SceneView's analysis overlays collection.
  2. Create a GeoElementLineOfSight, passing in observer and target GeoElements (feautures or graphics). Add the line of sight to the analysis overlay's analyses collection.
  3. To get the target visibility when it changes, add a TargetVisibilityChangedListener to the line of sight. The changed event will give the TargetVisibility.


 * 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
 * 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.samples.analysis.line_of_sight_geoelement;

import java.util.Arrays;
import java.util.List;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

import com.esri.arcgisruntime.geoanalysis.GeoElementLineOfSight;
import com.esri.arcgisruntime.geoanalysis.LineOfSight;
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.PointBuilder;
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.Camera;
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.SceneView;
import com.esri.arcgisruntime.symbology.ModelSceneSymbol;
import com.esri.arcgisruntime.symbology.Renderer;
import com.esri.arcgisruntime.symbology.SceneSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;

public class LineOfSightGeoElementSample extends Application {

  private SceneView sceneView;
  private List<Point> waypoints;
  private int waypointIndex = 0;
  private Timeline animation;
  private Graphic taxi;

  private static final LinearUnit METERS = new LinearUnit(LinearUnitId.METERS);
  private static final AngularUnit DEGREES = new AngularUnit(AngularUnitId.DEGREES);

  public void start(Stage stage) {

    try {

      // create stack pane and JavaFX app scene
      StackPane stackPane = new StackPane();
      Scene fxScene = new Scene(stackPane);

      // set title, size, and add JavaFX scene to stage
      stage.setTitle("Line of Sight GeoElement Sample");

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

      // add the SceneView to the stack pane
      sceneView = new SceneView();

      // add base surface for elevation data
      Surface surface = new Surface();
      surface.getElevationSources().add(new ArcGISTiledElevationSource("http://elevation3d.arcgis" +

      // add buildings from New York City
      String buildingsURL = "";
      ArcGISSceneLayer buildings = new ArcGISSceneLayer(buildingsURL);

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

      // set up a heading expression to handle graphic rotation
      SimpleRenderer renderer3D = new SimpleRenderer();
      Renderer.SceneProperties renderProperties = renderer3D.getSceneProperties();

      // create a point graph near the Empire State Building to be the observer
      Point observationPoint = new Point(-73.9853, 40.7484, 200, SpatialReferences.getWgs84());
      Graphic observer = new Graphic(observationPoint, new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xFFFF0000, 5));

      // create a slider to change the observer's Z value
      Slider heightSlider = new Slider(150, 300, 200);
      heightSlider.setMaxSize(30, 150);
      StackPane.setAlignment(heightSlider, Pos.TOP_LEFT);
      StackPane.setMargin(heightSlider, new Insets(10, 0, 0, 10));
      heightSlider.valueProperty().addListener(e -> {
        PointBuilder pointBuilder = new PointBuilder((Point) observer.getGeometry());

      // create waypoints around a block for the taxi to drive to
      waypoints = Arrays.asList(
          new Point(-73.984513, 40.748469, SpatialReferences.getWgs84()),
          new Point(-73.985068, 40.747786, SpatialReferences.getWgs84()),
          new Point(-73.983452, 40.747091, SpatialReferences.getWgs84()),
          new Point(-73.982961, 40.747762, SpatialReferences.getWgs84())

      // create a graphic of a taxi to be the target
      String modelURI = new File("./samples-data/dolmus_3ds/dolmus.3ds").getAbsolutePath();
      ModelSceneSymbol taxiSymbol = new ModelSceneSymbol(modelURI, 1.0);
      taxi = new Graphic(waypoints.get(0), taxiSymbol);
      taxi.getAttributes().put("HEADING", 0.0);

      // create an analysis overlay to hold the line of sight
      AnalysisOverlay analysisOverlay = new AnalysisOverlay();

      // create a line of sight between the two graphics and add it to the analysis overlay
      GeoElementLineOfSight lineOfSight = new GeoElementLineOfSight(observer, taxi);

      // select (highlight) the taxi when the line of sight target visibility changes to visible
      lineOfSight.addTargetVisibilityChangedListener(targetVisibilityChangedEvent ->
        taxi.setSelected(targetVisibilityChangedEvent.getTargetVisibility() == LineOfSight.TargetVisibility.VISIBLE)

      // create a timeline to animate the taxi driving around the block
      animation = new Timeline();
      animation.getKeyFrames().add(new KeyFrame(Duration.millis(100), e -> animate()));;

      // zoom to show the observer
      Camera camera = new Camera((Point) observer.getGeometry(), 700, -30, 45, 0);

    } catch (Exception e) {
      // on any error, display the stack trace.

   * Moves the taxi toward the current waypoint a short distance.
  private void animate() {
    Point waypoint = waypoints.get(waypointIndex);
    // get current location and distance from waypoint
    Point location = (Point) taxi.getGeometry();
    GeodeticDistanceResult distance = GeometryEngine.distanceGeodetic(location, waypoint, METERS, DEGREES,

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

    // rotate to the waypoint
    taxi.getAttributes().put("HEADING", distance.getAzimuth1());

    // reached waypoint, move to next waypoint
    if (distance.getDistance() <= 2) {
      waypointIndex = (waypointIndex + 1) % waypoints.size();

   * Stops and releases all resources used in application.
  public void stop() {

    // stop the animation

    if (sceneView != null) {

   * Opens and runs application.
   * @param args arguments passed to this application
  public static void main(String[] args) {



In this topic
  1. How it works
  2. Code