Identify features in a scene to select.

Use case
You can select features to visually distinguish them with a selection color or highlighting. This can be useful to demonstrate the physical extent or associated attributes of a feature, or to initiate another action such as centering that feature in the scene view.
How to use the sample
Click a building in the scene layer to select it. Deselect buildings by clicking away from the buildings.
How it works
- Create an
ArcGISSceneLayerpassing in the URL to a scene layer service. - Use
sceneView.setOnMouseClickedto get the screen click locationPoint2D. - Call
sceneView.identifyLayersAsync(sceneLayer, point2D, tolerance, false, 1)to identify features in the scene. - From the resulting
IdentifyLayerResult, get the list of identifiedGeoElements withresult.getElements(). - Get the first element in the list, checking that it is a feature, and call
sceneLayer.selectFeature(feature)to select it.
About the data
The scene shows a buildings layer in Brest, France hosted on ArcGIS Online.
Relevant API
- ArcGISSceneLayer
- Scene
- SceneView
Tags
3D, Berlin, buildings, identify, model, query, search, select
Sample Code
/* * 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. */
module com.esri.samples.scene_layer_selection { // require ArcGIS Maps SDK for Java module requires com.esri.arcgisruntime;
// handle SLF4J http://www.slf4j.org/codes.html#StaticLoggerBinder requires org.slf4j.nop;
// require JavaFX modules that the application uses requires javafx.graphics;
exports com.esri.samples.scene_layer_selection;}/* * 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.scene_layer_selection;
import java.util.List;import java.util.concurrent.ExecutionException;
import javafx.application.Application;import javafx.geometry.Point2D;import javafx.scene.Scene;import javafx.scene.control.Alert;import javafx.scene.input.MouseButton;import javafx.scene.layout.StackPane;import javafx.stage.Stage;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;import com.esri.arcgisruntime.concurrent.ListenableFuture;import com.esri.arcgisruntime.data.Feature;import com.esri.arcgisruntime.layers.ArcGISSceneLayer;import com.esri.arcgisruntime.loadable.LoadStatus;import com.esri.arcgisruntime.mapping.ArcGISScene;import com.esri.arcgisruntime.mapping.ArcGISTiledElevationSource;import com.esri.arcgisruntime.mapping.BasemapStyle;import com.esri.arcgisruntime.mapping.GeoElement;import com.esri.arcgisruntime.mapping.Surface;import com.esri.arcgisruntime.mapping.view.Camera;import com.esri.arcgisruntime.mapping.view.IdentifyLayerResult;import com.esri.arcgisruntime.mapping.view.SceneView;
public class SceneLayerSelectionSample extends Application {
private SceneView sceneView;
@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("Scene Layer Selection 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_TOPOGRAPHIC);
// set the scene to the scene view sceneView = new SceneView(); sceneView.setArcGISScene(scene);
// set the initial viewpoint Camera camera = new Camera(48.378, -4.494, 200, 345, 65, 0); sceneView.setViewpointCamera(camera);
// add the scene view to the stack pane stackPane.getChildren().add(sceneView);
// add base surface with elevation data Surface surface = new Surface(); final String elevationService = "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"; surface.getElevationSources().add(new ArcGISTiledElevationSource(elevationService)); scene.setBaseSurface(surface);
// add a scene layer of buildings in Brest, France final String buildings = "http://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Brest/SceneServer/layers/0"; ArcGISSceneLayer sceneLayer = new ArcGISSceneLayer(buildings); scene.getOperationalLayers().add(sceneLayer);
// zoom to the layer's extent when loaded sceneLayer.addDoneLoadingListener(() -> { if (sceneLayer.getLoadStatus() == LoadStatus.LOADED) {
// when the scene is clicked, identify the clicked feature and select it sceneView.setOnMouseClicked(e -> { if (e.isStillSincePress() && e.getButton() == MouseButton.PRIMARY) { // clear any previous selection sceneLayer.clearSelection(); // identify clicked feature Point2D point2D = new Point2D(e.getX(), e.getY()); ListenableFuture<IdentifyLayerResult> identify = sceneView.identifyLayerAsync(sceneLayer, point2D, 10, false, 1); identify.addDoneListener(() -> { try { // get the identified result and check that it is a feature IdentifyLayerResult result = identify.get(); List<GeoElement> geoElements = result.getElements(); if (geoElements.size() > 0) { GeoElement geoElement = geoElements.get(0); if (geoElement instanceof Feature) { // select the feature sceneLayer.selectFeature((Feature) geoElement); } } } catch (InterruptedException | ExecutionException ex) { new Alert(Alert.AlertType.ERROR, "Error identifying features").show(); } }); } }); } else { new Alert(Alert.AlertType.ERROR, "Error loading scene layer").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); }
}