Get elevation at a point

View on GitHubSample viewer app

Get the elevation for a given point on a surface in a scene.

Image of get elevation at point

Use case

Knowing the elevation at a given point in a landscape can aid in navigation, planning and survey in the field.

How to use the sample

Click anywhere on the surface to get the elevation at that point. Elevation is reported in meters since the scene view is in WGS84.

How it works

  1. Create a SceneView and ArcGISScene with an imagery base map.
  2. Set an ArcGISTiledElevationSource as the elevation source of the scene's base surface.
  3. Use the screenToBaseSurface(screenPoint) method on the scene view to convert the clicked screen point into a point on surface.
  4. Use the getElevationAsync(surfacePoint) method on the base surface to asynchronously get the elevation.

Relevant API

  • ArcGISTiledElevationSource
  • BaseSurface
  • ElevationSourcesList
  • SceneView

Additional information

getElevationAsync(surfacePoint) retrieves the most accurate available elevation value at a given point. To do this, the method must go to the server or local raster file and load the highest level of detail of data for the target location and return the elevation value.

If multiple elevation sources are present in the surface, the top most visible elevation source with a valid elevation in the given location is used to determine the result.

Tags

elevation, surface

Sample Code

GetElevationAtAPointSample.java
Use dark colors for code blocksCopy
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
/*
 * Copyright 2019 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.get_elevation_at_a_point;

import java.util.concurrent.ExecutionException;

import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.StackPane;
import javafx.scene.Scene;
import javafx.stage.Stage;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.PolylineBuilder;
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.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.TextSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;

public class GetElevationAtAPointSample extends Application {

  private SceneView sceneView;
  private GraphicsOverlay graphicsOverlay;

  @Override
  public void start(Stage stage) {

    try {
      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene fxScene = new Scene(stackPane);
      fxScene.getStylesheets().add(getClass().getResource("/get_elevation_at_a_point/style.css").toExternalForm());

      // set title, size, and add JavaFX scene to stage
      stage.setTitle("Get Elevation at a Point 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().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);

      // create a point symbol and graphic to mark where elevation is being measured
      SimpleLineSymbol elevationLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFFFF0000, 3.0f);
      Graphic polylineGraphic = new Graphic();
      polylineGraphic.setSymbol(elevationLineSymbol);

      // create a text symbol and graphic to display the elevation of selected points
      TextSymbol elevationTextSymbol = new TextSymbol(20, null, 0xFFFF0000, TextSymbol.HorizontalAlignment.CENTER, TextSymbol.VerticalAlignment.BOTTOM);
      Graphic elevationTextGraphic = new Graphic();
      elevationTextGraphic.setSymbol(elevationTextSymbol);

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

      // add a camera and initial camera position
      Camera camera = new Camera(28.42, 83.9, 10000.0, 10.0, 80.0, 0.0);
      sceneView.setViewpointCamera(camera);

      // create listener to handle clicks
      sceneView.setOnMouseClicked(event -> {
        if (event.getButton() == MouseButton.PRIMARY && event.isStillSincePress()) {
          // get the clicked screenPoint
          Point2D screenPoint = new Point2D(event.getX(), event.getY());
          // convert the screen point to a point on the surface
          Point relativeSurfacePoint = sceneView.screenToBaseSurface(screenPoint);

          // check that the point is on the surface
          if (relativeSurfacePoint != null) {

            // clear any existing graphics from the graphics overlay
            graphicsOverlay.getGraphics().clear();

            // construct a polyline to use as a marker
            PolylineBuilder polylineBuilder = new PolylineBuilder(relativeSurfacePoint.getSpatialReference());
            Point baseOfPolyline = new Point(relativeSurfacePoint.getX(), relativeSurfacePoint.getY(), 0);
            polylineBuilder.addPoint(baseOfPolyline);
            Point topOfPolyline = new Point(baseOfPolyline.getX(), baseOfPolyline.getY(), 750);
            polylineBuilder.addPoint(topOfPolyline);
            Polyline markerPolyline = polylineBuilder.toGeometry();
            polylineGraphic.setGeometry(markerPolyline);
            graphicsOverlay.getGraphics().add(polylineGraphic);

            // get the surface elevation at the surface point
            ListenableFuture<Double> elevationFuture = scene.getBaseSurface().getElevationAsync(relativeSurfacePoint);
            elevationFuture.addDoneListener(() -> {
              try {
                // get the surface elevation
                Double elevation = elevationFuture.get();

                // update the text in the elevation marker
                elevationTextSymbol.setText(Math.round(elevation) + " m");
                elevationTextGraphic.setGeometry(topOfPolyline);
                graphicsOverlay.getGraphics().add(elevationTextGraphic);

              } catch (InterruptedException | ExecutionException e) {
                new Alert(Alert.AlertType.ERROR, e.getCause().getMessage()).show();
              }
            });
          }
        }
      });
    } catch (Exception e) {
      // on any error, display the stack trace.
      e.printStackTrace();
    }
  }

  /**
   * Stops and releases all resources used in application.
   */
  @Override
  public void 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.