|
| 1 | +// Copyright 2024 Esri |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import ArcGIS |
| 16 | +import SwiftUI |
| 17 | + |
| 18 | +struct ListContentsOfKMLFileView: View { |
| 19 | + /// The view model for the sample. |
| 20 | + @StateObject private var model = Model() |
| 21 | + |
| 22 | + var body: some View { |
| 23 | + VStack(spacing: 0) { |
| 24 | + Text("Tap on a disclosure to reveal a node's children. Tap on a node to open it in a scene.") |
| 25 | + .multilineTextAlignment(.center) |
| 26 | + .frame(maxWidth: .infinity, alignment: .center) |
| 27 | + .padding(8) |
| 28 | + .background(Color(.systemGroupedBackground)) |
| 29 | + |
| 30 | + // Recursively displays the dataset's nodes in a list. |
| 31 | + List(model.kmlDataset?.rootNodes ?? [], id: \.name, children: \.children) { node in |
| 32 | + VStack(alignment: .leading) { |
| 33 | + NavigationLink { |
| 34 | + if let viewpoint = model.nodeViewpoints[node.name] { |
| 35 | + SceneView(scene: model.scene, viewpoint: viewpoint) |
| 36 | + .navigationTitle(node.name) |
| 37 | + } else { |
| 38 | + Text("This node has no extent to view.") |
| 39 | + .navigationTitle(node.name) |
| 40 | + } |
| 41 | + } label: { |
| 42 | + VStack(alignment: .leading) { |
| 43 | + if !node.name.isEmpty { |
| 44 | + Text(node.name) |
| 45 | + } |
| 46 | + Text(node.typeLabel) |
| 47 | + .font(.footnote) |
| 48 | + } |
| 49 | +#if targetEnvironment(macCatalyst) |
| 50 | + .padding(.leading) |
| 51 | +#endif |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + .errorAlert(presentingError: $model.error) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// MARK: Model |
| 61 | + |
| 62 | +private extension ListContentsOfKMLFileView { |
| 63 | + /// The view model for the sample. |
| 64 | + @MainActor |
| 65 | + final class Model: ObservableObject { |
| 66 | + /// A dataset containing the KML data from a local file. |
| 67 | + @Published private(set) var kmlDataset: KMLDataset? |
| 68 | + |
| 69 | + /// The viewpoints for the nodes in the dataset. |
| 70 | + @Published private(set) var nodeViewpoints: [String: Viewpoint] = [:] |
| 71 | + |
| 72 | + /// The error shown in the error alert. |
| 73 | + @Published var error: Error? |
| 74 | + |
| 75 | + /// A scene for displaying the KML data. |
| 76 | + let scene: ArcGIS.Scene = { |
| 77 | + let scene = Scene(basemapStyle: .arcGISImagery) |
| 78 | + let elevationSource = ArcGISTiledElevationSource(url: .worldElevationService) |
| 79 | + scene.baseSurface.addElevationSource(elevationSource) |
| 80 | + return scene |
| 81 | + }() |
| 82 | + |
| 83 | + /// The task used for the asynchronous setup operations. |
| 84 | + private var setupTask: Task<Void, Never>? |
| 85 | + |
| 86 | + init() { |
| 87 | + setupTask = Task { [weak self] in |
| 88 | + guard let self else { return } |
| 89 | + |
| 90 | + do { |
| 91 | + try await setUpKMLDataset() |
| 92 | + } catch { |
| 93 | + self.error = error |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + deinit { |
| 99 | + setupTask?.cancel() |
| 100 | + } |
| 101 | + |
| 102 | + /// Sets up the KML dataset and adds it to the scene as layer. |
| 103 | + private func setUpKMLDataset() async throws { |
| 104 | + // Creates the dataset using a local ".kml" file in the bundle. |
| 105 | + let kmlDataset = KMLDataset(name: "esri_test_data", bundle: .main)! |
| 106 | + try await kmlDataset.load() |
| 107 | + self.kmlDataset = kmlDataset |
| 108 | + |
| 109 | + // Adds the dataset to the scene as a KML layer. |
| 110 | + let kmlLayer = KMLLayer(dataset: kmlDataset) |
| 111 | + scene.addOperationalLayer(kmlLayer) |
| 112 | + try await scene.load() |
| 113 | + |
| 114 | + try await setUpKMLNodes(kmlDataset.rootNodes) |
| 115 | + } |
| 116 | + |
| 117 | + /// Recursively creates viewpoints for KML nodes in a given list. |
| 118 | + /// - Parameter kmlNodes: The list of KML nodes to set up. |
| 119 | + private func setUpKMLNodes(_ kmlNodes: [KMLNode]) async throws { |
| 120 | + // Loads the surface so that the elevation can be queried when the viewpoint is made. |
| 121 | + try await scene.baseSurface.load() |
| 122 | + |
| 123 | + for node in kmlNodes { |
| 124 | + let viewpoint = try await Viewpoint(kmlNode: node, surface: scene.baseSurface) |
| 125 | + nodeViewpoints[node.name] = viewpoint |
| 126 | + |
| 127 | + // Ensures the node is visible since some are hidden by default. |
| 128 | + node.isVisible = true |
| 129 | + |
| 130 | + if let childNodes = node.children { |
| 131 | + try await setUpKMLNodes(childNodes) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// MARK: Helper Extensions |
| 139 | + |
| 140 | +private extension KMLNode { |
| 141 | + /// The child nodes of the node, if any. |
| 142 | + var children: [KMLNode]? { |
| 143 | + switch self { |
| 144 | + case let container as KMLContainer: |
| 145 | + container.childNodes |
| 146 | + case let networkLink as KMLNetworkLink: |
| 147 | + networkLink.childNodes |
| 148 | + default: |
| 149 | + nil |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /// A human-readable label of the type of the node. |
| 154 | + var typeLabel: String { |
| 155 | + switch self { |
| 156 | + case is KMLDocument: "Document" |
| 157 | + case is KMLFolder: "Folder" |
| 158 | + case is KMLContainer: "Container" |
| 159 | + case is KMLGroundOverlay: "Ground Overlay" |
| 160 | + case is KMLNetworkLink: "Network Link" |
| 161 | + case is KMLPhotoOverlay: "Photo Overlay" |
| 162 | + case is KMLPlacemark: "Placemark" |
| 163 | + case is KMLScreenOverlay: "Screen Overlay" |
| 164 | + case is KMLTour: "Tour" |
| 165 | + default: "Unknown" |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +private extension Viewpoint { |
| 171 | + /// Creates a viewpoint from a KML node. |
| 172 | + /// - Parameters: |
| 173 | + /// - kmlNode: The KML node. |
| 174 | + /// - surface: A surface that determines the elevation needed to offset the viewpoint. |
| 175 | + init?(kmlNode: KMLNode, surface: Surface) async throws { |
| 176 | + if let kmlViewpoint = kmlNode.viewpoint { |
| 177 | + try await self.init(kmlViewpoint: kmlViewpoint, surface: surface) |
| 178 | + } else if let extent = kmlNode.extent { |
| 179 | + // Creates a viewpoint with node's extent if it doesn't have a predefined viewpoint. |
| 180 | + try await self.init(kmlNodeExtent: extent, surface: surface) |
| 181 | + } else { |
| 182 | + return nil |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + /// Creates a viewpoint from a KML viewpoint. |
| 187 | + /// - Parameters: |
| 188 | + /// - kmlViewpoint: The KML viewpoint. |
| 189 | + /// - surface: A surface that determines the elevation needed to offset the viewpoint. |
| 190 | + private init(kmlViewpoint: KMLViewpoint, surface: Surface) async throws { |
| 191 | + switch kmlViewpoint.kind { |
| 192 | + case .lookAt: |
| 193 | + var lookAtPoint = kmlViewpoint.location |
| 194 | + if kmlViewpoint.altitudeMode != .absolute { |
| 195 | + // If the elevation is relative, account for the surface's elevation. |
| 196 | + let elevation = try await surface.elevation(at: kmlViewpoint.location) |
| 197 | + lookAtPoint = kmlViewpoint.location.withBuilder { $0.z += elevation } |
| 198 | + } |
| 199 | + |
| 200 | + let camera = Camera( |
| 201 | + lookingAt: lookAtPoint, |
| 202 | + distance: kmlViewpoint.range, |
| 203 | + heading: kmlViewpoint.heading, |
| 204 | + pitch: kmlViewpoint.pitch, |
| 205 | + roll: kmlViewpoint.roll |
| 206 | + ) |
| 207 | + self.init(latitude: .nan, longitude: .nan, scale: .nan, camera: camera) |
| 208 | + case .camera: |
| 209 | + let camera = Camera( |
| 210 | + location: kmlViewpoint.location, |
| 211 | + heading: kmlViewpoint.heading, |
| 212 | + pitch: kmlViewpoint.pitch, |
| 213 | + roll: kmlViewpoint.roll |
| 214 | + ) |
| 215 | + self.init(latitude: .nan, longitude: .nan, scale: .nan, camera: camera) |
| 216 | + @unknown default: |
| 217 | + fatalError("Unexpected KMLViewpoint.Kind: \(kmlViewpoint.kind)") |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + /// Creates a viewpoint from the extent of a KML node. |
| 222 | + /// - Parameters: |
| 223 | + /// - extent: The extent of a KML node. |
| 224 | + /// - surface: A surface that determines the elevation needed to offset the viewpoint. |
| 225 | + private init?(kmlNodeExtent extent: Envelope, surface: Surface) async throws { |
| 226 | + // Ensures the extent isn't empty since some nodes don't include a geometry. |
| 227 | + guard !extent.isEmpty else { return nil } |
| 228 | + |
| 229 | + let extentCenter = extent.center |
| 230 | + let elevation = try await surface.elevation(at: extentCenter) |
| 231 | + |
| 232 | + if extent.extent.width == 0, extent.height == 0 { |
| 233 | + // If the extent is not empty, but the width and height are still zero, |
| 234 | + // default values (based on Google Earth) are used to create a camera. |
| 235 | + let elevatedCenter = extentCenter.withBuilder { $0.z += elevation } |
| 236 | + let camera = Camera( |
| 237 | + lookingAt: elevatedCenter, |
| 238 | + distance: 1000, |
| 239 | + heading: 0, |
| 240 | + pitch: 45, |
| 241 | + roll: 0 |
| 242 | + ) |
| 243 | + self.init(latitude: .nan, longitude: .nan, scale: .nan, camera: camera) |
| 244 | + } else { |
| 245 | + // Adds the elevation and a buffer to the extent. |
| 246 | + let bufferedExtent = extent.withBuilder { builder in |
| 247 | + builder.zMin += elevation |
| 248 | + builder.zMax += elevation |
| 249 | + builder.expand(by: 1.1) |
| 250 | + } |
| 251 | + self.init(boundingGeometry: bufferedExtent) |
| 252 | + } |
| 253 | + } |
| 254 | +} |
| 255 | + |
| 256 | +private extension URL { |
| 257 | + /// A web URL to the Terrain3D image server on ArcGIS REST. |
| 258 | + static var worldElevationService: URL { |
| 259 | + URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")! |
| 260 | + } |
| 261 | +} |
0 commit comments