View on GitHub Sample viewer app
Analyze the viewshed for an object (GeoElement) in a scene.
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
Click to set a destination for the vehicle (a GeoElement). The vehicle will 'drive' towards the clicked location. The viewshed analysis will update as the vehicle moves.
How it works
Create and show the scene, with an elevation source and a buildings layer.
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.
Create a GeoElementViewshed
with configuration for the viewshed analysis.
Add the viewshed to an AnalysisOverlay
and add the overlay to the scene.
Configure the SceneView OrbitGeoElementCameraController
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 CodeViewshedGeoElementSample.java
Use dark colors for code blocks Copy
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
/*
* Copyright 2017 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.viewshed_geoelement;
import java.io.File;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
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.BasemapStyle;
import com.esri.arcgisruntime.mapping.Surface;
import com.esri.arcgisruntime.mapping.view.AnalysisOverlay;
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 ViewshedGeoElementSample extends Application {
private SceneView sceneView;
private Graphic tank;
private Timeline animation;
private Point waypoint;
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);
// set title, size, and add JavaFX scene to stage
stage.setTitle( "Viewshed GeoElement Sample" );
stage.setWidth( 800 );
stage.setHeight( 700 );
stage.setScene(fxScene);
stage.show();
// authentication with an API key or named user is required to access basemaps and other location services
String yourAPIKey = System.getProperty( "apiKey" );
ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);
// create a scene with a basemap style
ArcGISScene scene = new ArcGISScene(BasemapStyle.ARCGIS_IMAGERY);
// add the SceneView to the stack pane
sceneView = new SceneView();
sceneView.setArcGISScene(scene);
stackPane.getChildren().addAll(sceneView);
// add base surface for elevation data
Surface surface = new Surface();
final String elevationImageService = "https://elevation3d.arcgis.com" +
"/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer" ;
surface.getElevationSources().add( new ArcGISTiledElevationSource(elevationImageService));
scene.setBaseSurface(surface);
// add a scene layer
final String buildings = "https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Brest/SceneServer/layers/0" ;
ArcGISSceneLayer sceneLayer = new ArcGISSceneLayer(buildings);
scene.getOperationalLayers().add(sceneLayer);
// create a graphics overlay for the tank
GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
graphicsOverlay.getSceneProperties().setSurfacePlacement(LayerSceneProperties.SurfacePlacement.RELATIVE);
sceneView.getGraphicsOverlays().add(graphicsOverlay);
// set up heading expression for tank
SimpleRenderer renderer3D = new SimpleRenderer();
Renderer.SceneProperties renderProperties = renderer3D.getSceneProperties();
renderProperties.setHeadingExpression( "[HEADING]" );
graphicsOverlay.setRenderer(renderer3D);
// create a graphic of a tank
String modelURI = new File(System.getProperty( "data.dir" ), "./samples-data/bradley_low_3ds/bradle.3ds" ).getAbsolutePath();
ModelSceneSymbol tankSymbol = new ModelSceneSymbol(modelURI.replace( "\\" , "/" ), 10.0 );
tankSymbol.setHeading( 90 );
tankSymbol.setAnchorPosition(SceneSymbol.AnchorPosition.BOTTOM);
tankSymbol.loadAsync();
tank = new Graphic( new Point(- 4.506390 , 48.385624 , SpatialReferences.getWgs84()), tankSymbol);
tank.getAttributes().put( "HEADING" , 0.0 );
graphicsOverlay.getGraphics().add(tank);
// create a viewshed to attach to the tank
GeoElementViewshed geoElementViewshed = new GeoElementViewshed(tank, 90.0 , 40.0 , 0.1 , 250.0 , 0.0 , 0.0 );
// offset viewshed observer location to top of tank
geoElementViewshed.setOffsetZ( 3.0 );
// create an analysis overlay to add the viewshed to the scene view
AnalysisOverlay analysisOverlay = new AnalysisOverlay();
analysisOverlay.getAnalyses().add(geoElementViewshed);
sceneView.getAnalysisOverlays().add(analysisOverlay);
// set the waypoint where the user clicks
sceneView.setOnMouseClicked(e -> {
if (e.isStillSincePress() && e.getButton() == MouseButton.PRIMARY) {
// create a point from where the user clicked
Point2D point = new Point2D(e.getX(), e.getY());
// set the new waypoint
waypoint = sceneView.screenToBaseSurface(point);
}
});
// set camera controller to follow tank
OrbitGeoElementCameraController cameraController = new OrbitGeoElementCameraController(tank, 200.0 );
cameraController.setCameraPitchOffset( 45.0 );
sceneView.setCameraController(cameraController);
// create a timeline to animate the tank
animation = new Timeline();
animation.setCycleCount(- 1 );
animation.getKeyFrames().add( new KeyFrame(Duration.millis( 100 ), e -> animate()));
animation.play();
} catch (Exception e) {
// on any error, display the stack trace.
e.printStackTrace();
}
}
/**
* Moves the tank toward the current waypoint a short distance.
*/
private void animate () {
if (waypoint != null ) {
// get current location and distance from waypoint
Point location = (Point) tank.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);
tank.setGeometry(location);
// rotate toward waypoint
double heading = ( double ) tank.getAttributes().get( "HEADING" );
tank.getAttributes().put( "HEADING" , heading + ((distance.getAzimuth1() - heading) / 10 ));
// reached waypoint, stop moving
if (distance.getDistance() <= 5 ) {
waypoint = null ;
}
}
}
/**
* 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);
}
}