List KML contents

View on GitHub
Sample viewer app

List the contents of a KML file.

Image of list KML contents

Use case

KML files can contain a hierarchy of features, including network links to other KML content. A user may wish to traverse through the contents of KML nodes to know what data is contained within each node and, recursively, their children.

How to use the sample

The contents of the KML file are shown in a tree. Select a node to zoom to that node. Not all nodes can be zoomed to (e.g. screen overlays).

How it works

  1. Add the KML file to the scene as a layer.
  2. Explore the root nodes of the KmlDataset recursively explored to create a view model.
  3. Each node is enabled for display at this step. KML files may include nodes that are turned off by default.
  4. When a node is selected, get its extent with selectedNode.getExtent(). Use that to create a Viewpoint and set the SceneView object's viewpoint to it.

Relevant API

  • KmlContainer
  • KmlDataset
  • KmlDocument
  • KmlFolder
  • KmlGroundOverlay
  • KmlLayer
  • KmlNetworkLink
  • KmlNode
  • KmlPlacemark
  • KmlScreenOverlay

Tags

Keyhole, KML, KMZ, layers, OGC

Sample Code

ListKMLContentsSample.java
                                                                                                                                                                                       
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
174
175
176
177
178
179
180
181
182
183
/*
 * 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.list_kml_contents;

import java.io.File;
import java.util.ArrayList;
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.control.cell.TextFieldTreeCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.layers.KmlLayer;
import com.esri.arcgisruntime.mapping.ArcGISScene;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.SceneView;
import com.esri.arcgisruntime.ogc.kml.KmlContainer;
import com.esri.arcgisruntime.ogc.kml.KmlDataset;
import com.esri.arcgisruntime.ogc.kml.KmlDocument;
import com.esri.arcgisruntime.ogc.kml.KmlFolder;
import com.esri.arcgisruntime.ogc.kml.KmlGroundOverlay;
import com.esri.arcgisruntime.ogc.kml.KmlNetworkLink;
import com.esri.arcgisruntime.ogc.kml.KmlNode;
import com.esri.arcgisruntime.ogc.kml.KmlPlacemark;
import com.esri.arcgisruntime.ogc.kml.KmlScreenOverlay;

public class ListKMLContentsSample extends Application {

  private SceneView sceneView;

  @Override
  public void start(Stage stage) {

    try {
      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene fxScene = new Scene(stackPane);

      // set title, size, and add scene to stage
      stage.setTitle("List KML Contents Sample");
      stage.setWidth(800);
      stage.setHeight(700);
      stage.setScene(fxScene);
      stage.show();

      // create a map and add it to the map view
      ArcGISScene scene = new ArcGISScene(Basemap.createImageryWithLabels());
      sceneView = new SceneView();
      sceneView.setArcGISScene(scene);

      // load a KML dataset from a local KMZ file and show it as an operational layer
      File kmzFile = new File(System.getProperty("data.dir"), "./samples-data/kml/esri_test_data.kmz");
      KmlDataset kmlDataset = new KmlDataset(kmzFile.getAbsolutePath());
      KmlLayer kmlLayer = new KmlLayer(kmlDataset);
      scene.getOperationalLayers().add(kmlLayer);

      // create a tree view to list the contents of the KML dataset
      TreeView<KmlNode> kmlTree = new TreeView<>();
      kmlTree.setMaxSize(300, 400);
      TreeItem<KmlNode> root = new TreeItem<>(null);
      kmlTree.setRoot(root);
      kmlTree.setShowRoot(false);

      // when the dataset is loaded, recursively build the tree view with KML nodes starting with the root node(s)
      kmlDataset.addDoneLoadingListener(() -> kmlDataset.getRootNodes().forEach(kmlNode -> {
        TreeItem<KmlNode> kmlNodeTreeItem = buildTree(new TreeItem<>(kmlNode));
        root.getChildren().add(kmlNodeTreeItem);
      }));

      // show the KML node in the tree view with its name and type
      kmlTree.setCellFactory(param -> new TextFieldTreeCell<>(new StringConverter<KmlNode>() {

        @Override
        public String toString(KmlNode node) {
          String type = null;
          if (node instanceof KmlDocument) {
            type = "KmlDocument";
          } else if (node instanceof KmlFolder) {
            type = "KmlFolder";
          } else if (node instanceof KmlGroundOverlay) {
            type = "KmlGroundOverlay";
          } else if (node instanceof KmlScreenOverlay) {
            type = "KmlScreenOverlay";
          } else if (node instanceof KmlPlacemark) {
              type = "KmlPlacemark";
          }
          return node.getName() + " - " + type;
        }

        @Override
        public KmlNode fromString(String string) {
          return null; //not needed
        }
      }));

      // when a tree item is selected, zoom to its node's extent (if it has one)
      kmlTree.getSelectionModel().selectedItemProperty().addListener(o -> {
        TreeItem<KmlNode> selectedTreeItem = kmlTree.getSelectionModel().getSelectedItem();
        KmlNode selectedNode = selectedTreeItem.getValue();
        Envelope nodeExtent = selectedNode.getExtent();
        if (nodeExtent != null && !nodeExtent.isEmpty()) {
          sceneView.setViewpointAsync(new Viewpoint(nodeExtent));
        }
      });

      // add the map view to stack pane
      stackPane.getChildren().addAll(sceneView, kmlTree);
      StackPane.setAlignment(kmlTree, Pos.TOP_LEFT);
      StackPane.setMargin(kmlTree, new Insets(10));
    } catch (Exception e) {
      // on any error, display the stack trace.
      e.printStackTrace();
    }
  }

  /**
   * Recursively adds child tree items to the given parent tree item based on its node's child nodes.
   *
   * @param parent a parent KML node tree item
   * @return the original parent, now with the child tree attached to it
   */
  private TreeItem<KmlNode> buildTree(TreeItem<KmlNode> parent) {
    KmlNode node = parent.getValue();
    node.setVisible(true);
    List<KmlNode> children = new ArrayList<>();
    if (parent.getValue() instanceof KmlContainer) {
      children.addAll(((KmlContainer) node).getChildNodes());
    } else if (parent.getValue() instanceof KmlNetworkLink) {
      children.addAll(((KmlNetworkLink) node).getChildNodes());
    }
    children.forEach(childNode -> {
      TreeItem<KmlNode> childTreeItem = buildTree(new TreeItem<>(childNode));
      parent.getChildren().add(childTreeItem);
    });
    parent.setExpanded(true); // expand all nodes in the tree view
    return parent;
  }

  /**
   * 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.