-
Notifications
You must be signed in to change notification settings - Fork 123
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
group layers sample #331
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file added
BIN
+428 KB
src/main/java/com/esri/samples/grouplayers/group_layers/GroupLayers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
170 changes: 170 additions & 0 deletions
170
src/main/java/com/esri/samples/grouplayers/group_layers/GroupLayersSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/esri/samples/grouplayers/group_layers/LayerTreeCell.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
src/main/java/com/esri/samples/grouplayers/group_layers/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
<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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.