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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
* Copyright 2022 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.local_server_generate_elevation_profile;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.geometry.Point2D;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.Job;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureCollection;
import com.esri.arcgisruntime.data.FeatureCollectionTable;
import com.esri.arcgisruntime.data.FeatureTable;
import com.esri.arcgisruntime.data.Field;
import com.esri.arcgisruntime.data.ServiceGeodatabase;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PointCollection;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.layers.RasterLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.localserver.LocalGeoprocessingService;
import com.esri.arcgisruntime.localserver.LocalGeoprocessingService.ServiceType;
import com.esri.arcgisruntime.localserver.LocalServer;
import com.esri.arcgisruntime.localserver.LocalServerStatus;
import com.esri.arcgisruntime.mapping.ArcGISScene;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
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.raster.HillshadeRenderer;
import com.esri.arcgisruntime.raster.Raster;
import com.esri.arcgisruntime.raster.RasterFunction;
import com.esri.arcgisruntime.raster.RasterFunctionArguments;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;
import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingFeatures;
import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingJob;
import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingString;
import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingTask;
public class LocalServerGenerateElevationProfileController {
@FXML private Button drawPolylineButton;
@FXML private Button generateProfileButton;
@FXML private Button clearResultsButton;
@FXML private Label instructionsLabel;
@FXML private ProgressBar progressBar;
@FXML private SceneView sceneView;
@FXML private VBox vBox;
private ArcGISScene scene;
private FeatureCollection featureCollection;
private FeatureLayer featureLayer;
private GeoprocessingTask gpTask;
private GraphicsOverlay graphicsOverlay;
private LocalGeoprocessingService localGPService;
private Polyline polyline;
private Raster arranRaster;
private RasterLayer rasterLayer;
private Viewpoint rasterExtentViewPoint;
private static LocalServer server;
/**
* Called after FXML loads. Sets up scene.
*/
public void initialize() {
try {
// 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 topographic basemap style
scene = new ArcGISScene(BasemapStyle.ARCGIS_HILLSHADE_DARK);
// create a new raster from local file and display it on the scene
arranRaster = new Raster(new File
(System.getProperty("data.dir"), "./samples-data/local_server/Arran_10m_raster.tif").getAbsolutePath());
// when the scene has loaded, display the raster
scene.addDoneLoadingListener(() -> {
if (scene.getLoadStatus() == LoadStatus.LOADED) {
displayRaster(arranRaster);
} else if (scene.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
new Alert(AlertType.ERROR, "Scene failed to load").show();
}
});
// set up a new feature collection
featureCollection = new FeatureCollection();
// create a graphics overlay for displaying the sketched polyline and add it to the scene view's list of
// graphics overlays
graphicsOverlay = new GraphicsOverlay();
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, Color.BLACK, 3);
graphicsOverlay.setRenderer(new SimpleRenderer(lineSymbol));
sceneView.getGraphicsOverlays().add(graphicsOverlay);
// start the local server instance
startLocalServer();
// set the scene to the scene view
sceneView.setArcGISScene(scene);
} catch (Exception e) {
// on any exception, print the stack trace
e.printStackTrace();
}
}
/**
* Creates a new raster, applies various raster functions to mask the data to only show data above sea level, and adds
* it to a raster layer. Applies a hillshade renderer to the raster layer, and adds the raster layer to the scene's
* list of operational layers.
*/
private void displayRaster(Raster raster) {
// raster function on raster data
try {
Raster maskedRaster = applyMaskingRasterFunction(raster);
// create a raster layer from the raster
rasterLayer = new RasterLayer(maskedRaster);
// set a hillshade renderer to the raster layer
rasterLayer.setRasterRenderer(new HillshadeRenderer(30, 210, 1));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// once the raster layer has loaded, set the viewpoint of the scene view to the raster layer's full extent
rasterLayer.addDoneLoadingListener(() -> {
if (rasterLayer.getLoadStatus() == LoadStatus.LOADED && rasterLayer.getFullExtent() != null) {
// centered on the raster covering the Isle of Arran, Scotland
rasterExtentViewPoint = new Viewpoint(rasterLayer.getFullExtent());
sceneView.setViewpointAsync(rasterExtentViewPoint);
} else {
new Alert(AlertType.ERROR, "Raster layer failed to load.").show();
}
});
// add the raster layer to the scene's operational layers
scene.getOperationalLayers().add(rasterLayer);
}
/**
* Checks that there is a valid install of Local Server, and if so starts the Local Server, and then starts a local
* geoprocessing service from the create_elevation_profile_model geoprocessing package.
*/
private void startLocalServer() {
// check that there is a valid install on user machine
if (LocalServer.INSTANCE.checkInstallValid()) {
progressBar.setVisible(true);
server = LocalServer.INSTANCE;
// check the local server instance status
server.addStatusChangedListener(serverStatus -> {
if (serverStatus.getNewStatus() == LocalServerStatus.STARTED) {
try {
// get the path to the geoprocessing package (created in ArcGIS Pro) that creates elevation profile from
// raster data
String geoprocessingPackagePath = new File(System.getProperty("data.dir"), "./samples-data/local_server" +
"/create_elevation_profile_model.gpkx").getAbsolutePath();
// create new local geoprocessing service with map server result to display elevation profile on scene
localGPService =
new LocalGeoprocessingService(geoprocessingPackagePath, ServiceType.ASYNCHRONOUS_SUBMIT_WITH_MAP_SERVER_RESULT);
} catch (Exception e) {
e.printStackTrace();
}
localGPService.addStatusChangedListener(serviceStatus -> {
// create geoprocessing task once local geoprocessing service is started
if (serviceStatus.getNewStatus() == LocalServerStatus.STARTED) {
// add the name of the model used to create the gpkx in ArcGIS Pro to the Url of the local geoprocessing task
// e.g. the model name in this sample's gpkx created in ArcGIS Pro is CreateElevationProfileModel
gpTask = new GeoprocessingTask(localGPService.getUrl() + "/CreateElevationProfileModel");
// handle UI behaviour
instructionsLabel.setText("Draw a polyline on the scene with the 'Draw Polyline' button");
drawPolylineButton.setDisable(false);
progressBar.setVisible(false);
} else if (localGPService.getStatus() == LocalServerStatus.FAILED) {
// display an information alert and close the application if the geoprocessing service failed to start
showMessage("Local Server Status Error", "Local Geoprocessing Service failed to start.");
}
});
localGPService.startAsync();
} else if (server.getStatus() == LocalServerStatus.FAILED) {
// display an information alert and close the application if the server failed to start
showMessage("Local Server Status Error", "Local Server failed to start.");
}
});
// start the local server
server.startAsync();
} else {
// display an information alert and close the application if a local server install path couldn't be located
showMessage("Local Server Install Error", "Local Server install path couldn't be located.");
}
}
/**
* Generates an elevation profile when the "Generate Elevation Profile" button is clicked. The elevation profile is
* generated from an input raster (Isle of Arran Lidar data) and a polyline sketched by the user.
*/
@FXML
protected void handleGenerateElevationProfile() {
generateProfileButton.setDisable(true);
// tracking progress of generating elevation profile
progressBar.setVisible(true);
// create the feature collection table from sketched polyline
createFeatureCollectionTableWithPolylineFeature();
// create default parameters and get their inputs
var defaultParamsListener = gpTask.createDefaultParametersAsync();
defaultParamsListener.addDoneListener(() -> {
try {
var params = defaultParamsListener.get();
var inputParams = params.getInputs();
// input polyline path
// name of input parameter, input type (geoprocessing feature, pointing to polyline)
inputParams.put("Input_Polyline", new GeoprocessingFeatures(featureCollection.getTables().get(0)));
// input raster data
// name of input parameter, input type (geoprocessing string pointing to raster file)
inputParams.put("Input_Raster", new GeoprocessingString(arranRaster.getPath()));
// create geoprocessing job from the geoprocessing parameters to show elevation profile on the scene
GeoprocessingJob gpJob = gpTask.createJob(params);
gpJob.addJobDoneListener(() -> {
if (gpJob.getStatus() == Job.Status.SUCCEEDED) {
// convert geoprocesser server url to that of a map server, and get the job id
var serviceUrl = localGPService.getUrl();
var mapServerUrl = serviceUrl.replace("GPServer", "MapServer/jobs/" + gpJob.getServerJobId());
// create a service geodatabase from the map server url
var serviceGeodatabase = new ServiceGeodatabase(mapServerUrl);
serviceGeodatabase.addDoneLoadingListener(() -> {
FeatureTable featureTable = serviceGeodatabase.getTable(0);
featureLayer = new FeatureLayer(featureTable);
featureLayer.addDoneLoadingListener(() -> {
featureLayer.getSceneProperties().setSurfacePlacement(LayerSceneProperties.SurfacePlacement.ABSOLUTE);
featureLayer.setRenderer(new SimpleRenderer(
new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.WHITE, 3)));
sceneView.setViewpointCameraAsync(createCameraFacingElevationProfile(), 2);
instructionsLabel.setText("Elevation Profile drawn");
});
scene.getOperationalLayers().add(featureLayer);
// handle UI
generateProfileButton.setDisable(true);
clearResultsButton.setDisable(false);
});
serviceGeodatabase.loadAsync();
} else {
new Alert(AlertType.ERROR, "Geoprocess Job Fail. Error: " +
gpJob.getError().getMessage()).showAndWait();
}
progressBar.setVisible(false);
});
gpJob.start();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
});
}
/**
* Creates a feature collection table, and adds a feature to it constructed from the sketched polyline. A feature
* collection table is the parameter type required for the GeoprocessingFeatures constructor in this sample.
*/
@FXML
private void createFeatureCollectionTableWithPolylineFeature() {
// create name field for polyline
List<Field> polylineField = new ArrayList<>();
polylineField.add(Field.createString("Name", "Name of feature", 20));
// create a feature collection table
FeatureCollectionTable featureCollectionTable = new FeatureCollectionTable(polylineField, GeometryType.POLYLINE,
SpatialReferences.getWebMercator(), true, false);
// add the feature collection table to the feature collection and load it
featureCollection.addDoneLoadingListener(() -> {
if (featureCollection.getLoadStatus() == LoadStatus.LOADED) {
// add the feature collection table to the feature collection, and create a feature from it, using the polyline
// sketched by the user
featureCollection.getTables().add(featureCollectionTable);
Map<String, Object> attributes = new HashMap<>();
attributes.put(polylineField.get(0).getName(), "ElevationSection");
Feature addedFeature = featureCollectionTable.createFeature(attributes, polyline);
// add feature to collection table
featureCollectionTable.addFeatureAsync(addedFeature);
} else {
new Alert(AlertType.ERROR, "Feature collection failed to load").show();
}
});
featureCollection.loadAsync();
}
/**
* Handles "Draw Polyline" button. Creates a temporary graphics overlay and adds points clicked on the scene
* to a point collection. On right click of the mouse button, the points are used to construct a polyline which is
* returned.
*/
@FXML
private void handleDrawPolyline() {
vBox.setDisable(true);
instructionsLabel.setText("Click on the map to draw polyline path, then right click to save it");
drawPolylineButton.setDisable(true);
// create a temporary graphics overlay to display point collection on map
var tempGraphicsOverlay = new GraphicsOverlay();
sceneView.getGraphicsOverlays().add(tempGraphicsOverlay);
var simpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CROSS, Color.BLACK, 10);
tempGraphicsOverlay.setRenderer(new SimpleRenderer(simpleMarkerSymbol));
// create a point collection with the same spatial reference as the raster layer
var rasterLayerSpatialReference = rasterLayer.getSpatialReference();
PointCollection pointCollection = new PointCollection(rasterLayerSpatialReference);
sceneView.setOnMouseClicked(event -> {
if (event.isStillSincePress() && event.getButton() == MouseButton.PRIMARY && vBox.isDisabled()) {
// get the clicked location
var point2D = new Point2D(event.getX(), event.getY());
ListenableFuture<Point> pointFuture = sceneView.screenToLocationAsync(point2D);
pointFuture.addDoneListener(() -> {
// project the clicked location point, and add it to the point collection and temporarily display the clicked
// location on the map
try {
Point point = pointFuture.get();
Point projectedPoint = (Point) GeometryEngine.project(point, rasterLayerSpatialReference);
// check that the user has clicked within the extent of the raster
if (GeometryEngine.intersects(projectedPoint, rasterLayer.getFullExtent())) {
pointCollection.add(projectedPoint);
tempGraphicsOverlay.getGraphics().add(new Graphic(projectedPoint));
} else {
new Alert(AlertType.ERROR, "Clicked point must be within raster layer extent").show();
drawPolylineButton.setDisable(false);
}
} catch (Exception e) {
e.printStackTrace();
}
});
} else if (event.getButton() == MouseButton.SECONDARY && tempGraphicsOverlay.getGraphics().size() > 1) {
// clear the temporary graphics overlay displaying clicked points
tempGraphicsOverlay.getGraphics().clear();
// create a polyline from the clicked points on the scene and add it as a graphic to the graphics overlay
polyline = new Polyline(pointCollection);
Graphic graphic = new Graphic(polyline);
graphicsOverlay.getGraphics().add(graphic);
new Alert(AlertType.INFORMATION, "Polyline sketched").show();
// handle UI
vBox.setDisable(false);
drawPolylineButton.setDisable(true);
generateProfileButton.setDisable(false);
instructionsLabel.setText("Generate an elevation profile along the polyline using the Generate Elevation " +
"Profile button");
// clear point collection
pointCollection.clear();
} else if (event.getButton() == MouseButton.SECONDARY && tempGraphicsOverlay.getGraphics().size() == 1) {
new Alert(AlertType.WARNING, "More than one point required to draw polyline").show();
}
});
}
/**
* Remove feature layer from list of operational layers (retains the raster layer), and graphics from the scene.
*/
@FXML
protected void handleClearResults() {
// remove all graphics
graphicsOverlay.getGraphics().clear();
// remove last operational layer added to the scene (feature layer), retains the raster layer
scene.getOperationalLayers().remove(1);
// handle UI after checking there is still an operational layer in the scene (raster layer)
generateProfileButton.setDisable(true);
drawPolylineButton.setDisable(false);
clearResultsButton.setDisable(true);
sceneView.setViewpointAsync(rasterExtentViewPoint);
featureCollection.getTables().clear();
instructionsLabel.setVisible(true);
instructionsLabel.setText("Draw a polyline on the scene with the 'Draw Polyline' button");
}
/**
* Performs a sequence of raster functions to the original raster data that finds data with a value above 0m (sea
* level) and masks the data to only show that data above sea level. The data is masked for visual purposes to show
* only the island extent, and also masks out the edge effects from the original data.
*
* @param originalRaster the initial raster to perform raster function on
* @return masked raster (hides data at or below sea level (0m))
* @throws FileNotFoundException if the json raster functions are not found
*/
private Raster applyMaskingRasterFunction(Raster originalRaster) throws FileNotFoundException {
// initiate raster for output
Raster maskedRaster;
// raster function to get pixels above 0m (above sea level)
var aboveSeaLevelJsonFile = new File(System.getProperty("data.dir"), "./samples-data/local_server" +
"/raster_functions/above_sea_level_raster_calculation.json");
String aboveSeaLevelRasterFunctionScanner = new Scanner(aboveSeaLevelJsonFile).useDelimiter("\\A").next();
var aboveSeaLevelRasterFunction = RasterFunction.fromJson(aboveSeaLevelRasterFunctionScanner);
RasterFunctionArguments aboveSeaLevelArguments = aboveSeaLevelRasterFunction.getArguments();
// apply the raster function to the input raster
aboveSeaLevelArguments.setRaster(aboveSeaLevelArguments.getRasterNames().get(0), originalRaster);
Raster aboveSeaLevelRaster = new Raster(aboveSeaLevelRasterFunction); // gets raster composed of 1s and 0s, 1
// represents data above sea level
// raster function to restore elevation profiles post above sea level calculations
var restoreElevationJsonFile = new File(System.getProperty("data.dir"), "./samples-data/local_server" +
"/raster_functions/restore_elevation_raster_calculation.json");
String restoreElevationRasterFunctionScanner = new Scanner(restoreElevationJsonFile).useDelimiter("\\A").next();
var restoreElevationRasterFunction = RasterFunction.fromJson(restoreElevationRasterFunctionScanner);
RasterFunctionArguments restoreElevationArguments = restoreElevationRasterFunction.getArguments();
// set the rasters to the raster function arguments
restoreElevationArguments.setRaster(restoreElevationArguments.getRasterNames().get(0), originalRaster);
restoreElevationArguments.setRaster(restoreElevationArguments.getRasterNames().get(1), aboveSeaLevelRaster);
Raster restoredElevationRaster = new Raster(restoreElevationRasterFunction); // creates new raster with elevation
// values restored above 0
// raster function to mask out values below sea level (pixels with value of 0)
var maskJsonFile = new File(System.getProperty("data.dir"), "./samples-data/local_server/raster_functions/mask.json");
String maskScanner = new Scanner(maskJsonFile).useDelimiter("\\A").next();
var maskRasterFunction = RasterFunction.fromJson(maskScanner);
RasterFunctionArguments maskArguments = maskRasterFunction.getArguments();
// apply the raster function to the restored elevation raster
maskArguments.setRaster(maskArguments.getRasterNames().get(0), restoredElevationRaster);
maskedRaster = new Raster(maskRasterFunction); // creates new raster with values equal to 0 masked out
return maskedRaster;
}
/**
* Calculates a camera position and heading angle that is placed perpendicularly to the polyline sketch.
* @return camera with calculated camera position and heading angle
*/
private Camera createCameraFacingElevationProfile() {
// get the polyline's end point coordinates
var endPoint = polyline.getParts().get(0).getEndPoint();
var endPointX = endPoint.getX();
var endPointY = endPoint.getY();
// get the polyline's center point coordinates
var centerPoint = polyline.getExtent().getCenter();
var centerX = centerPoint.getX();
var centerY = centerPoint.getY();
// calculate the position of a point perpendicular to the centre of the polyline
double lengthX = endPointX - centerX;
double lengthY = endPointY - centerY;
var cameraPositionPoint = new Point(centerX + lengthY * 2, centerY - lengthX * 2, 1200,
SpatialReferences.getWebMercator());
// calculate the heading for the camera position so that it points perpendicularly towards the elevation profile
double theta;
double cameraHeadingPerpToProfile;
// account for switching opposite and adjacent depending on angle direction from drawn line
if (lengthY < 0) { // accounts for a downwards angle
theta = Math.toDegrees(Math.atan((centerX - endPointX) / (centerY - endPointY)));
cameraHeadingPerpToProfile = theta + 90;
} else { // accounts for an upwards angle
theta = Math.toDegrees(Math.atan((centerY - endPointY) / (centerX - endPointX)));
// determine if theta is positive or negative, then account accordingly for calculating the angle back from north
// and then rotate that value by + or - 90 to get the angle perpendicular to the drawn line
double angleFromNorth = (90 - theta);
// if theta is positive, rotate angle anticlockwise by 90 degrees, else, clockwise by 90 degrees
if (theta > 0) {
cameraHeadingPerpToProfile = angleFromNorth - 90;
} else {
cameraHeadingPerpToProfile = angleFromNorth + 90;
}
}
// create a new camera from the calculated camera position point and camera angle perpendicular to profile
return new Camera(cameraPositionPoint, cameraHeadingPerpToProfile, 80, 0);
}
/**
* Displays an information alert and closes the application when dialog is closed.
*
* @param title header text of the dialog
* @param message content text of the dialog
*/
private void showMessage(String title, String message) {
Platform.runLater(() -> {
Alert dialog = new Alert(AlertType.INFORMATION);
dialog.initOwner(sceneView.getScene().getWindow());
dialog.setHeaderText(title);
dialog.setContentText(message);
dialog.showAndWait();
Platform.exit();
});
}
/**
* Stops and releases all resources used in application.
*/
void terminate() throws InterruptedException {
if (sceneView != null) {
sceneView.dispose();
CountDownLatch latch = new CountDownLatch(1);
server.stopAsync().addDoneListener(latch::countDown);
if (!latch.await(2, TimeUnit.SECONDS)) {
System.err.println("Local server failed to shutdown in 2 seconds");
}
}
}
}