|
| 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 AddVectorTiledLayerFromCustomStyleView: View { |
| 19 | + /// The view model for the sample. |
| 20 | + @StateObject private var model = Model() |
| 21 | + |
| 22 | + /// The viewpoint used to update the map view. |
| 23 | + @State private var viewpoint: Viewpoint? |
| 24 | + |
| 25 | + /// The label of the style selected by the picker. |
| 26 | + @State private var selectedStyleLabel = "Default" |
| 27 | + |
| 28 | + /// The error shown in the error alert. |
| 29 | + @State private var error: Error? |
| 30 | + |
| 31 | + var body: some View { |
| 32 | + MapView(map: model.map, viewpoint: viewpoint) |
| 33 | + .toolbar { |
| 34 | + ToolbarItem(placement: .bottomBar) { |
| 35 | + Picker("Style", selection: $selectedStyleLabel) { |
| 36 | + Section("Online Styles") { |
| 37 | + ForEach(model.onlineStyles, id: \.key) { label, _ in |
| 38 | + Text(label) |
| 39 | + } |
| 40 | + } |
| 41 | + Section("Offline Styles") { |
| 42 | + ForEach(model.offlineStyles, id: \.key) { label, _ in |
| 43 | + Text(label) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + .task(id: selectedStyleLabel) { |
| 48 | + // Updates the map's layer when the picker selection changes. |
| 49 | + do { |
| 50 | + viewpoint = try await model.setVectorTiledLayer( |
| 51 | + label: selectedStyleLabel |
| 52 | + ) |
| 53 | + } catch { |
| 54 | + self.error = error |
| 55 | + } |
| 56 | + } |
| 57 | + .errorAlert(presentingError: $error) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// MARK: Model |
| 64 | + |
| 65 | +private extension AddVectorTiledLayerFromCustomStyleView { |
| 66 | + /// The view model for the sample. |
| 67 | + @MainActor |
| 68 | + final class Model: ObservableObject { |
| 69 | + /// A map with no specified style. |
| 70 | + let map = Map() |
| 71 | + |
| 72 | + /// The labels and portal item IDs of the online styles. |
| 73 | + let onlineStyles: KeyValuePairs = [ |
| 74 | + "Default": "1349bfa0ed08485d8a92c442a3850b06", |
| 75 | + "Style 1": "bd8ac41667014d98b933e97713ba8377", |
| 76 | + "Style 2": "02f85ec376084c508b9c8e5a311724fa", |
| 77 | + "Style 3": "1bf0cc4a4380468fbbff107e100f65a5" |
| 78 | + ] |
| 79 | + |
| 80 | + /// The labels and portal item IDs of the offline styles. |
| 81 | + let offlineStyles: KeyValuePairs = [ |
| 82 | + "Light": "e01262ef2a4f4d91897d9bbd3a9b1075", |
| 83 | + "Dark": "ce8a34e5d4ca4fa193a097511daa8855" |
| 84 | + ] |
| 85 | + |
| 86 | + /// The cached vector tiled layers keyed by the label of their associated style. |
| 87 | + private var vectorTiledLayers: [String: ArcGISVectorTiledLayer] = [:] |
| 88 | + |
| 89 | + /// The URL to the temporary directory for the offline style files. |
| 90 | + private let temporaryDirectoryURL = FileManager.createTemporaryDirectory() |
| 91 | + |
| 92 | + /// The vector tile cache for creating the offline vector tiled layers. |
| 93 | + private let vectorTileCache = VectorTileCache(name: "dodge_city", bundle: .main)! |
| 94 | + |
| 95 | + deinit { |
| 96 | + // Removes all of the temporary offline style files used by sample. |
| 97 | + try? FileManager.default.removeItem(at: temporaryDirectoryURL) |
| 98 | + } |
| 99 | + |
| 100 | + /// Sets the vector tiled layer for a given style on the map. |
| 101 | + /// - Parameter label: The label of the style associated with the layer. |
| 102 | + /// - Returns: A viewpoint framing some of the layer's data. |
| 103 | + func setVectorTiledLayer(label: String) async throws -> Viewpoint { |
| 104 | + // Gets or creates a vector tile layer and adds it to the map as a basemap. |
| 105 | + let vectorTiledLayer = if let cachedVectorTiledLayer = vectorTiledLayers[label] { |
| 106 | + cachedVectorTiledLayer |
| 107 | + } else { |
| 108 | + try await cacheVectorTiledLayer(label: label) |
| 109 | + } |
| 110 | + map.basemap = Basemap(baseLayer: vectorTiledLayer) |
| 111 | + |
| 112 | + return if vectorTiledLayer.vectorTileCache != nil { |
| 113 | + // Uses a Dodge City, KS viewpoint if the layer was created using the tile cache. |
| 114 | + Viewpoint(latitude: 37.76528, longitude: -100.01766, scale: 4e4) |
| 115 | + } else { |
| 116 | + // Uses a Europe/Africa viewpoint if the layer was created using an online style. |
| 117 | + Viewpoint(latitude: 28.53345, longitude: 17.56488, scale: 1e8) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// Creates and caches a vector tiled layer for a given style. |
| 122 | + /// - Parameter label: The label of the style associated with the layer. |
| 123 | + /// - Returns: The cached `ArcGISVectorTiledLayer`. |
| 124 | + private func cacheVectorTiledLayer(label: String) async throws -> ArcGISVectorTiledLayer { |
| 125 | + let vectorTiledLayer: ArcGISVectorTiledLayer |
| 126 | + if let onlineStyle = onlineStyles.first(where: { $0.key == label }) { |
| 127 | + vectorTiledLayer = makeOnlineVectorTiledLayer(itemID: onlineStyle.value) |
| 128 | + } else { |
| 129 | + let offlineStyle = offlineStyles.first(where: { $0.key == label })! |
| 130 | + vectorTiledLayer = try await makeOfflineVectorTiledLayer(itemID: offlineStyle.value) |
| 131 | + } |
| 132 | + |
| 133 | + try await vectorTiledLayer.load() |
| 134 | + vectorTiledLayers[label] = vectorTiledLayer |
| 135 | + |
| 136 | + return vectorTiledLayer |
| 137 | + } |
| 138 | + |
| 139 | + /// Creates a vector tiled layer using a portal item. |
| 140 | + /// - Parameter itemID: The ID of the portal item. |
| 141 | + /// - Returns: A new `ArcGISVectorTiledLayer` object. |
| 142 | + private func makeOnlineVectorTiledLayer(itemID: String) -> ArcGISVectorTiledLayer { |
| 143 | + let portalItem = PortalItem( |
| 144 | + portal: .arcGISOnline(connection: .anonymous), |
| 145 | + id: .init(itemID)! |
| 146 | + ) |
| 147 | + return ArcGISVectorTiledLayer(item: portalItem) |
| 148 | + } |
| 149 | + |
| 150 | + /// Creates a vector tiled layer using a local vector tile cache and an item resource cache. |
| 151 | + /// - Parameter itemID: The ID of the portal item used to create the export vector tiles task. |
| 152 | + /// - Returns: A new `ArcGISVectorTiledLayer` object. |
| 153 | + private func makeOfflineVectorTiledLayer( |
| 154 | + itemID: String |
| 155 | + ) async throws -> ArcGISVectorTiledLayer { |
| 156 | + // Creates a export style resource cache job using a portal item. |
| 157 | + let portalItem = PortalItem( |
| 158 | + portal: .arcGISOnline(connection: .anonymous), |
| 159 | + id: .init(itemID)! |
| 160 | + ) |
| 161 | + let exportTask = ExportVectorTilesTask(portalItem: portalItem) |
| 162 | + |
| 163 | + let temporaryURL = temporaryDirectoryURL.appendingPathComponent(itemID) |
| 164 | + let exportStyleResourceCacheJob = exportTask.makeExportStyleResourceCacheJob( |
| 165 | + itemResourceCacheURL: temporaryURL |
| 166 | + ) |
| 167 | + |
| 168 | + // Gets the item resource cache from the job and uses it to create the layer. |
| 169 | + exportStyleResourceCacheJob.start() |
| 170 | + let output = try await exportStyleResourceCacheJob.output |
| 171 | + |
| 172 | + return ArcGISVectorTiledLayer( |
| 173 | + vectorTileCache: vectorTileCache, |
| 174 | + itemResourceCache: output.itemResourceCache |
| 175 | + ) |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +// MARK: Helper Extensions |
| 181 | + |
| 182 | +private extension FileManager { |
| 183 | + /// Creates a temporary directory. |
| 184 | + /// - Returns: The URL of the created directory |
| 185 | + static func createTemporaryDirectory() -> URL { |
| 186 | + // swiftlint:disable:next force_try |
| 187 | + try! FileManager.default.url( |
| 188 | + for: .itemReplacementDirectory, |
| 189 | + in: .userDomainMask, |
| 190 | + appropriateFor: FileManager.default.temporaryDirectory, |
| 191 | + create: true |
| 192 | + ) |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +private extension URL { |
| 197 | + /// The URL to the local vector tile package file with Dodge City, KS data. |
| 198 | + static var dodgeCityVectorTilePackage: URL { |
| 199 | + Bundle.main.url(forResource: "dodge_city", withExtension: "vtpk")! |
| 200 | + } |
| 201 | +} |
0 commit comments