Line of sight geoelement

View on GitHub
Sample viewer app

Show a line of sight between two moving objects.

Image of line of sight geoelement

Use case

A line of sight between GeoElements (i.e. observer and target) will not remain constant whilst one or both are on the move.

A GeoElementLineOfSight is therefore useful in cases where visibility between two GeoElements requires monitoring over a period of time in a partially obstructed field of view (such as buildings in a city).

How to use the sample

A line of sight will display between a point on the Empire State Building (observer) and a taxi (target). The taxi will drive around a block and the line of sight should automatically update. The taxi will be highlighted when it is visible. You can change the observer height with the slider to see how it affects the target's visibility.

How it works

  1. Instantiate an AnalysisOverlay and add it to the SceneView's analysis overlays collection.
  2. Instantiate a GeoElementLineOfSight, passing in observer and target GeoElements (features or graphics). Add the line of sight to the analysis overlay's analyses collection.
  3. To get the target visibility when it changes, react to the target visibility changing on the GeoElementLineOfSight instance.

Relevant API

  • AnalysisOverlay
  • GeoElementLineOfSight
  • LineOfSight.getTargetVisibility

Tags

3D, line of sight, visibility, visibility analysis

Sample Code

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

import java.io.File;
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.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Paint;
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);

  @Override
  public void start(Stage stage) {

    try {

      // create stack pane and JavaFX app scene
      StackPane stackPane = new StackPane();
      Scene fxScene = new Scene(stackPane);
      fxScene.getStylesheets().add(getClass().getResource("/line_of_sight_geoelement/style.css").toExternalForm());

      // set title, size, and add JavaFX scene to stage
      stage.setTitle("Line of Sight GeoElement Sample");
      stage.setWidth(800);
      stage.setHeight(700);
      stage.setScene(fxScene);
      stage.show();

      // create a scene and add a basemap to it
      ArcGISScene scene = new ArcGISScene();
      scene.setBasemap(Basemap.createTopographic());

      // add the SceneView to the stack pane
      sceneView = new SceneView();
      sceneView.setArcGISScene(scene);
      stackPane.getChildren().add(sceneView);

      // add base surface for elevation data
      Surface surface = new Surface();
      surface.getElevationSources().add(new ArcGISTiledElevationSource("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"));
      scene.setBaseSurface(surface);

      // add buildings from New York City
      String buildingsURL = "https://tiles.arcgis.com/tiles/z2tnIkrLQ2BRzr6P/arcgis/rest/services/New_York_LoD2_3D_Buildings/SceneServer/layers/0";
      ArcGISSceneLayer buildings = new ArcGISSceneLayer(buildingsURL);
      scene.getOperationalLayers().add(buildings);

      // create a graphics overlay for the graphics
      GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
      graphicsOverlay.getSceneProperties().setSurfacePlacement(LayerSceneProperties.SurfacePlacement.RELATIVE);
      sceneView.getGraphicsOverlays().add(graphicsOverlay);

      // set up a heading expression to handle graphic rotation
      SimpleRenderer renderer3D = new SimpleRenderer();
      Renderer.SceneProperties renderProperties = renderer3D.getSceneProperties();
      renderProperties.setHeadingExpression("[HEADING]");
      graphicsOverlay.setRenderer(renderer3D);

      // 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));
      graphicsOverlay.getGraphics().add(observer);

      // create a slider to change the observer's Z value
      Slider heightSlider = new Slider(150, 300, 200);
      heightSlider.setMaxSize(30, 150);
      heightSlider.setShowTickLabels(true);
      heightSlider.setOrientation(Orientation.VERTICAL);
      heightSlider.valueProperty().addListener(e -> {
        PointBuilder pointBuilder = new PointBuilder((Point) observer.getGeometry());
        pointBuilder.setZ(heightSlider.getValue());
        observer.setGeometry(pointBuilder.toGeometry());
      });

      // show a background behind the slider
      VBox controlsVBox = new VBox(6);
      controlsVBox.setBackground(new Background(new BackgroundFill(Paint.valueOf("rgba(0,0,0,0.3)"), CornerRadii.EMPTY,
          Insets.EMPTY)));
      controlsVBox.setPadding(new Insets(10.0));
      controlsVBox.setMaxSize(50, 160);
      controlsVBox.getStyleClass().add("panel-region");
      controlsVBox.getChildren().add(heightSlider);
      stackPane.getChildren().add(controlsVBox);
      StackPane.setAlignment(controlsVBox, Pos.TOP_LEFT);
      StackPane.setMargin(controlsVBox, new Insets(10, 0, 0, 10));

      // 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(System.getProperty("data.dir"), "./samples-data/dolmus_3ds/dolmus.3ds").getAbsolutePath();
      ModelSceneSymbol taxiSymbol = new ModelSceneSymbol(modelURI, 1.0);
      taxiSymbol.setAnchorPosition(SceneSymbol.AnchorPosition.BOTTOM);
      taxiSymbol.loadAsync();
      taxi = new Graphic(waypoints.get(0), taxiSymbol);
      taxi.getAttributes().put("HEADING", 0.0);
      graphicsOverlay.getGraphics().add(taxi);

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

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

      // 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.setCycleCount(-1);
      animation.getKeyFrames().add(new KeyFrame(Duration.millis(100), e -> animate()));
      animation.play();

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

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

  /**
   * 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,
        GeodeticCurveType.GEODESIC);

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

    // 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.
   */
  @Override
  public void stop() {

    // stop the animation
    animation.stop();

    if (sceneView != null) {
      sceneView.dispose();
    }
  }

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

    Application.launch(args);
  }

}

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