Skip to content

group layers sample #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* 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.grouplayers.group_layers;

import java.util.Arrays;
import java.util.List;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.layers.ArcGISSceneLayer;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.layers.GroupLayer;
import com.esri.arcgisruntime.layers.Layer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISScene;
import com.esri.arcgisruntime.mapping.ArcGISTiledElevationSource;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Surface;
import com.esri.arcgisruntime.mapping.view.Camera;
import com.esri.arcgisruntime.mapping.view.SceneView;

public class GroupLayersSample extends Application {

private SceneView sceneView;

@Override
public void start(Stage stage) {

try {

// set the title and size of the stage and show it
StackPane stackPane = new StackPane();
Scene fxScene = new Scene(stackPane);
stage.setTitle("Group Layers Sample");
stage.setWidth(800);
stage.setHeight(700);

// create a JavaFX scene with a stackpane and set it to the stage
stage.setScene(fxScene);
stage.show();

// create a scene view and add it to the stack pane
sceneView = new SceneView();
stackPane.getChildren().add(sceneView);

// create a scene with a basemap and add it to the scene view
ArcGISScene scene = new ArcGISScene();
scene.setBasemap(Basemap.createImagery());
sceneView.setArcGISScene(scene);

// set the base surface with world elevation
Surface surface = new Surface();
surface.getElevationSources().add(new ArcGISTiledElevationSource("http://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"));
scene.setBaseSurface(surface);

// create different types of layers
ArcGISSceneLayer devOne = new ArcGISSceneLayer("https://scenesampleserverdev.arcgis.com/arcgis/rest/services/Hosted/DevA_Trees/SceneServer/layers/0");
ArcGISSceneLayer devTwo = new ArcGISSceneLayer("https://scenesampleserverdev.arcgis.com/arcgis/rest/services/Hosted/DevA_Pathways/SceneServer/layers/0");
ArcGISSceneLayer devThree = new ArcGISSceneLayer("https://scenesampleserverdev.arcgis.com/arcgis/rest/services/Hosted/DevA_BuildingShell_Textured/SceneServer/layers/0");
ArcGISSceneLayer nonDevOne = new ArcGISSceneLayer("https://scenesampleserverdev.arcgis.com/arcgis/rest/services/Hosted/PlannedDemo_BuildingShell/SceneServer/layers/0");
FeatureLayer nonDevTwo = new FeatureLayer(new ServiceFeatureTable("https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/DevelopmentProjectArea/FeatureServer/0"));

// create a group layer from scratch by adding the layers as children
GroupLayer groupLayer = new GroupLayer();
groupLayer.setName("Group: Dev A");
groupLayer.getLayers().addAll(Arrays.asList(devOne, devTwo, devThree));

// add the group layer and other layers to the scene as operational layers
scene.getOperationalLayers().addAll(Arrays.asList(groupLayer, nonDevOne, nonDevTwo));

// zoom to the extent of the group layer when the child layers are loaded
groupLayer.getLayers().forEach(childLayer ->
childLayer.addDoneLoadingListener(() -> {
if (childLayer.getLoadStatus() == LoadStatus.LOADED) {
sceneView.setViewpointCamera(new Camera(groupLayer.getFullExtent().getCenter(), 700, 0, 60, 0));
}
})
);

// create a JavaFX tree view to show the layers in the scene
TreeView<Layer> layerTreeView = new TreeView<>();
layerTreeView.setMaxSize(250, 200);
TreeItem<Layer> rootTreeItem = new TreeItem<>();
layerTreeView.setRoot(rootTreeItem);
layerTreeView.setShowRoot(false);
StackPane.setAlignment(layerTreeView, Pos.TOP_RIGHT);
StackPane.setMargin(layerTreeView, new Insets(10));
stackPane.getChildren().add(layerTreeView);

// display each layer with a custom tree cell
layerTreeView.setCellFactory(p -> new LayerTreeCell());

// recursively build the table of contents from the scene's operational layers
buildLayersView(rootTreeItem, scene.getOperationalLayers());

} catch (Exception e) {
// on any error, display the stack trace.
e.printStackTrace();
}
}

/**
* Recursively builds a tree from a parent tree item using a list of operational layers (including group layers).
*
* @param parentItem tree item to build tree from
* @param operationalLayers a list of operational layers
*/
private void buildLayersView(TreeItem<Layer> parentItem, List<Layer> operationalLayers) {
for (Layer layer : operationalLayers) {
// load each layer before adding to ensure all metadata is ready for display
layer.loadAsync();
layer.addDoneLoadingListener(() -> {
// add a tree item for the layer to the parent tree item
if (layer.canShowInLegend()) {
TreeItem<Layer> layerItem = new TreeItem<>(layer);
layerItem.setExpanded(true);
parentItem.getChildren().add(layerItem);
// if the layer is a group layer, continue building with its children
if (layer instanceof GroupLayer && ((GroupLayer) layer).isShowChildrenInLegend()) {
buildLayersView(layerItem, ((GroupLayer) layer).getLayers());
}
}
});
}
}

/**
* 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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.esri.samples.grouplayers.group_layers;

import javafx.scene.control.CheckBox;
import javafx.scene.control.TreeCell;

import com.esri.arcgisruntime.layers.Layer;

/**
* A custom tree cell for displaying a layer in a tree view. Includes a check box for toggling the layer's visibility.
*/
public class LayerTreeCell extends TreeCell<Layer> {

@Override
public void updateItem(Layer layer, boolean empty) {
super.updateItem(layer, empty);
if (!empty) {
// use the layer's name for the text
setText(layer.getName());

// add a check box to allow the user to change the visibility of the layer
CheckBox checkBox = new CheckBox();
setGraphic(checkBox);

// toggle the layer's visibility when the check box is toggled
checkBox.setSelected(layer.isVisible());
checkBox.selectedProperty().addListener(e -> layer.setVisible(checkBox.isSelected()));
} else {
setText(null);
setGraphic(null);
}
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/esri/samples/grouplayers/group_layers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<h1>Group layers</h1>

<p>Group a collection of layers together and toggle their visibility as a group.</p>

<p><img src="GroupLayers.png"/></p>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after the update from Lucas on accessibility, I think we should be adding alt tags after all? Depends what route we go down. Either basic with alt="Group Layer" or more detailed like alt="Scene view with 3D buildings and a table of contents for selecting group layers". But that's a discussion point for a later date, stick with basic for now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll hold off on this until after the release. It might have impacts on how the sample viewer renders the image.


<h2>Use case</h2>

<p>Group layers communicate to the user that layers are related and can be managed together.</p>

<p>In a land development project, you might group layers according to the phase of development.</p>

<h2>How to use the sample</h2>

<p>The layers in the map will be displayed in a table of contents. Toggle the checkbox next to a layer's name to change its visibility.</p>

<h2>How it works</h2>

<ol>
<li>Create an empty <code>GroupLayer</code>.</li>

<li>Add a child layer to the group layer's layers collection.</li>

<li>To toggle the visibility of the group, simply change the group layer's visibility property.</li>
</ol>

<h2>Relevant API</h2>

<ul>
<li>GroupLayer</li>
</ul>

<h2>Additional information</h2>

<p>The full extent of a group layer may change when child layers are added/removed. Group layers do not have a spatial reference, but the full extent will have the spatial reference of the first child layer.</p>

<p>Group layers can be saved to web scenes. In web maps, group layers will be flattened in the web map's operational layers.</p>

<p>The implementation shown here makes use of a custom tree cell for displaying layers in a tree view. In the custom
tree cell, toggling the checkbox of the group layer cell does not also toggle the child cell's checkbox. If you
desire this behavior, use or extend JavaFX's <code>CheckBoxTreeCell</code> and <code>CheckBoxTreeItem</code>.

<h2 id="tags">Tags</h2>

<p>Layers, group layer</p>