|
| 1 | +/* |
| 2 | + * Copyright 2018 Esri. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.esri.samples.scene.scene_layer_select; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.concurrent.ExecutionException; |
| 21 | + |
| 22 | +import javafx.application.Application; |
| 23 | +import javafx.geometry.Point2D; |
| 24 | +import javafx.scene.Scene; |
| 25 | +import javafx.scene.control.Alert; |
| 26 | +import javafx.scene.input.MouseButton; |
| 27 | +import javafx.scene.layout.StackPane; |
| 28 | +import javafx.stage.Stage; |
| 29 | + |
| 30 | +import com.esri.arcgisruntime.concurrent.ListenableFuture; |
| 31 | +import com.esri.arcgisruntime.data.Feature; |
| 32 | +import com.esri.arcgisruntime.layers.ArcGISSceneLayer; |
| 33 | +import com.esri.arcgisruntime.loadable.LoadStatus; |
| 34 | +import com.esri.arcgisruntime.mapping.ArcGISScene; |
| 35 | +import com.esri.arcgisruntime.mapping.ArcGISTiledElevationSource; |
| 36 | +import com.esri.arcgisruntime.mapping.Basemap; |
| 37 | +import com.esri.arcgisruntime.mapping.GeoElement; |
| 38 | +import com.esri.arcgisruntime.mapping.Surface; |
| 39 | +import com.esri.arcgisruntime.mapping.Viewpoint; |
| 40 | +import com.esri.arcgisruntime.mapping.view.IdentifyLayerResult; |
| 41 | +import com.esri.arcgisruntime.mapping.view.SceneView; |
| 42 | + |
| 43 | +public class SceneLayerSelectSample extends Application { |
| 44 | + |
| 45 | + private SceneView sceneView; |
| 46 | + |
| 47 | + @Override |
| 48 | + public void start(Stage stage) throws Exception { |
| 49 | + |
| 50 | + try { |
| 51 | + |
| 52 | + // create stack pane and JavaFX app scene |
| 53 | + StackPane stackPane = new StackPane(); |
| 54 | + Scene fxScene = new Scene(stackPane); |
| 55 | + |
| 56 | + // set title, size, and add JavaFX scene to stage |
| 57 | + stage.setTitle("Scene Layer Select Sample"); |
| 58 | + stage.setWidth(800); |
| 59 | + stage.setHeight(700); |
| 60 | + stage.setScene(fxScene); |
| 61 | + stage.show(); |
| 62 | + |
| 63 | + // create a scene and add a basemap to it |
| 64 | + ArcGISScene scene = new ArcGISScene(); |
| 65 | + scene.setBasemap(Basemap.createImagery()); |
| 66 | + |
| 67 | + // set the scene to the scene view |
| 68 | + sceneView = new SceneView(); |
| 69 | + sceneView.setArcGISScene(scene); |
| 70 | + |
| 71 | + // add the scene view to the stack pane |
| 72 | + stackPane.getChildren().add(sceneView); |
| 73 | + |
| 74 | + // add base surface with elevation data |
| 75 | + Surface surface = new Surface(); |
| 76 | + final String elevationService = "http://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"; |
| 77 | + surface.getElevationSources().add(new ArcGISTiledElevationSource(elevationService)); |
| 78 | + scene.setBaseSurface(surface); |
| 79 | + |
| 80 | + // add a scene layer of Harvard buildings to the scene |
| 81 | + final String buildings = "https://tiles.arcgis.com/tiles/N82JbI5EYtAkuUKU/arcgis/rest/services/Buildings_Harvard/SceneServer"; |
| 82 | + ArcGISSceneLayer sceneLayer = new ArcGISSceneLayer(buildings); |
| 83 | + scene.getOperationalLayers().add(sceneLayer); |
| 84 | + |
| 85 | + // zoom to the layer's extent when loaded |
| 86 | + sceneLayer.addDoneLoadingListener(() -> { |
| 87 | + if (sceneLayer.getLoadStatus() == LoadStatus.LOADED) { |
| 88 | + sceneView.setViewpoint(new Viewpoint(sceneLayer.getFullExtent())); |
| 89 | + |
| 90 | + // when the scene is clicked, identify the clicked feature and select it |
| 91 | + sceneView.setOnMouseClicked(e -> { |
| 92 | + if (e.isStillSincePress() && e.getButton() == MouseButton.PRIMARY) { |
| 93 | + // clear any previous selection |
| 94 | + sceneLayer.clearSelection(); |
| 95 | + // identify clicked feature |
| 96 | + Point2D point2D = new Point2D(e.getX(), e.getY()); |
| 97 | + ListenableFuture<IdentifyLayerResult> identify = sceneView.identifyLayerAsync(sceneLayer, point2D, 10, false, 1); |
| 98 | + identify.addDoneListener(() -> { |
| 99 | + try { |
| 100 | + // get the identified result and check that it is a feature |
| 101 | + IdentifyLayerResult result = identify.get(); |
| 102 | + List<GeoElement> geoElements = result.getElements(); |
| 103 | + if (geoElements.size() > 0) { |
| 104 | + GeoElement geoElement = geoElements.get(0); |
| 105 | + if (geoElement instanceof Feature) { |
| 106 | + // select the feature |
| 107 | + sceneLayer.selectFeature((Feature) geoElement); |
| 108 | + } |
| 109 | + } |
| 110 | + } catch (InterruptedException | ExecutionException ex) { |
| 111 | + new Alert(Alert.AlertType.ERROR, "Error identifying features").show(); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + }); |
| 116 | + } else { |
| 117 | + new Alert(Alert.AlertType.ERROR, "Error loading scene layer").show(); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + } catch (Exception e) { |
| 122 | + // on any error, display the stack trace. |
| 123 | + e.printStackTrace(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Stops and releases all resources used in application. |
| 129 | + */ |
| 130 | + @Override |
| 131 | + public void stop() { |
| 132 | + |
| 133 | + if (sceneView != null) { |
| 134 | + sceneView.dispose(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Opens and runs application. |
| 140 | + * |
| 141 | + * @param args arguments passed to this application |
| 142 | + */ |
| 143 | + public static void main(String[] args) { |
| 144 | + |
| 145 | + Application.launch(args); |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments