|
| 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 BrowseOGCAPIFeatureServiceView: View { |
| 19 | + /// The error shown in the error alert. |
| 20 | + @State private var error: Error? |
| 21 | + |
| 22 | + /// A Boolean value indicating whether the textfield alert should be presented. |
| 23 | + @State private var textfieldAlertIsPresented = true |
| 24 | + |
| 25 | + /// The data model for the sample. |
| 26 | + @StateObject private var model = Model() |
| 27 | + |
| 28 | + /// The user input for the OGC service resource. |
| 29 | + @State private var userInput = URL.daraaService.absoluteString |
| 30 | + |
| 31 | + /// The selected feature collection's title. |
| 32 | + @State private var selectedTitle = "" |
| 33 | + |
| 34 | + var body: some View { |
| 35 | + MapViewReader { mapProxy in |
| 36 | + MapView(map: model.map) |
| 37 | + .toolbar { |
| 38 | + ToolbarItemGroup(placement: .bottomBar) { |
| 39 | + Button("Open Service") { |
| 40 | + textfieldAlertIsPresented = true |
| 41 | + } |
| 42 | + |
| 43 | + Spacer() |
| 44 | + |
| 45 | + if !model.featureCollectionTitles.isEmpty { |
| 46 | + Picker("Layers", selection: $selectedTitle) { |
| 47 | + ForEach(model.featureCollectionTitles, id: \.self) { title in |
| 48 | + Text(title) |
| 49 | + } |
| 50 | + } |
| 51 | + .task(id: selectedTitle) { |
| 52 | + guard !selectedTitle.isEmpty else { return } |
| 53 | + let featureCollectionInfo = model.featureCollectionInfos[selectedTitle]! |
| 54 | + do { |
| 55 | + try await model.displayLayer(with: featureCollectionInfo) |
| 56 | + if let extent = featureCollectionInfo.extent { |
| 57 | + await mapProxy.setViewpointGeometry(extent, padding: 100) |
| 58 | + } |
| 59 | + } catch { |
| 60 | + self.error = error |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + .alert("Load OGC API feature service", isPresented: $textfieldAlertIsPresented) { |
| 67 | + // Textfield has a default OGC API URL. |
| 68 | + TextField("URL", text: $userInput) |
| 69 | + .keyboardType(.URL) |
| 70 | + .textContentType(.URL) |
| 71 | + Button("Load") { |
| 72 | + guard let url = URL(string: userInput) else { return } |
| 73 | + Task { |
| 74 | + do { |
| 75 | + try await model.loadOGCFeatureData(url: url) |
| 76 | + // Set the picker selection to the first title in the title list. |
| 77 | + if let title = model.featureCollectionTitles.first, |
| 78 | + let extent = model.featureCollectionInfos[title]?.extent { |
| 79 | + selectedTitle = title |
| 80 | + await mapProxy.setViewpointGeometry(extent, padding: 100) |
| 81 | + } |
| 82 | + } catch { |
| 83 | + self.error = error |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + .disabled(userInput.isEmpty) |
| 88 | + Button("Cancel", role: .cancel) { |
| 89 | + // Reset the default value of the textfield. |
| 90 | + userInput = URL.daraaService.absoluteString |
| 91 | + } |
| 92 | + } message: { |
| 93 | + Text("Please provide a URL to an OGC API feature service.") |
| 94 | + } |
| 95 | + .errorAlert(presentingError: $error) |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +private extension BrowseOGCAPIFeatureServiceView { |
| 101 | + @MainActor |
| 102 | + class Model: ObservableObject { |
| 103 | + /// A map with a topographic basemap of the Daraa, Syria. |
| 104 | + let map: Map = { |
| 105 | + let map = Map(basemapStyle: .arcGISTopographic) |
| 106 | + map.initialViewpoint = Viewpoint( |
| 107 | + center: Point(latitude: 32.62, longitude: 36.10), |
| 108 | + scale: 200_000 |
| 109 | + ) |
| 110 | + return map |
| 111 | + }() |
| 112 | + |
| 113 | + /// The titles of the feature collection infos in the OGC API. |
| 114 | + @Published private(set) var featureCollectionTitles: [String] = [] |
| 115 | + |
| 116 | + /// The OGC feature collection info from the OCG API. |
| 117 | + private(set) var featureCollectionInfos: [String: OGCFeatureCollectionInfo] = [:] |
| 118 | + |
| 119 | + /// The OGC API feature service. |
| 120 | + private var service: OGCFeatureService! |
| 121 | + |
| 122 | + /// The query parameters to populate features from the OGC API service. |
| 123 | + private let queryParameters: QueryParameters = { |
| 124 | + let queryParameters = QueryParameters() |
| 125 | + // Set a limit of 1000 on the number of returned features per request, |
| 126 | + // because the default on some services could be as low as 10. |
| 127 | + queryParameters.maxFeatures = 1_000 |
| 128 | + return queryParameters |
| 129 | + }() |
| 130 | + |
| 131 | + /// Returns a renderer with the appropriate symbol type for a geometry type. |
| 132 | + /// - Parameter geometryType: The geometry type. |
| 133 | + /// - Returns: A `SimpleRenderer` with the correct symbol for the given geometry. |
| 134 | + private func makeRenderer(withType geometryType: Geometry.Type) -> SimpleRenderer? { |
| 135 | + let symbol: Symbol |
| 136 | + switch geometryType { |
| 137 | + case is Point.Type, is Multipoint.Type: |
| 138 | + symbol = SimpleMarkerSymbol(style: .circle, color: .blue, size: 5) |
| 139 | + case is Polyline.Type: |
| 140 | + symbol = SimpleLineSymbol(style: .solid, color: .blue, width: 1) |
| 141 | + case is Polygon.Type, is Envelope.Type: |
| 142 | + symbol = SimpleFillSymbol(style: .solid, color: .blue) |
| 143 | + default: |
| 144 | + return nil |
| 145 | + } |
| 146 | + return SimpleRenderer(symbol: symbol) |
| 147 | + } |
| 148 | + |
| 149 | + /// Creates and loads the OGC API features service from a URL. |
| 150 | + /// - Parameter url: The URL of the OGC service. |
| 151 | + /// - Returns: Returns a loaded `OCGFeatureService`. |
| 152 | + private func makeService(url: URL) async throws -> OGCFeatureService { |
| 153 | + let service = OGCFeatureService(url: url) |
| 154 | + try await service.load() |
| 155 | + if let serviceInfo = service.serviceInfo { |
| 156 | + let infos = serviceInfo.featureCollectionInfos |
| 157 | + featureCollectionTitles = infos.map(\.title) |
| 158 | + // The sample assumes there is no duplicate titles in the service. |
| 159 | + // Collections with duplicate titles will be discarded. |
| 160 | + featureCollectionInfos = Dictionary( |
| 161 | + infos.map { ($0.title, $0) }, |
| 162 | + uniquingKeysWith: { (title, _) in title } |
| 163 | + ) |
| 164 | + } |
| 165 | + return service |
| 166 | + } |
| 167 | + |
| 168 | + /// Loads OGC service for a URL so that it can be rendered on the map. |
| 169 | + /// - Parameter url: The URL of the OGC service. |
| 170 | + func loadOGCFeatureData(url: URL) async throws { |
| 171 | + service = try await makeService(url: url) |
| 172 | + if let firstFeatureCollectionTitle = featureCollectionTitles.first, |
| 173 | + let info = featureCollectionInfos[firstFeatureCollectionTitle] { |
| 174 | + try await displayLayer(with: info) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /// Populates and displays a feature layer from an OGC feature collection table. |
| 179 | + /// - Parameter info: The `OGCFeatureCollectionInfo` selected by user. |
| 180 | + func displayLayer(with info: OGCFeatureCollectionInfo) async throws { |
| 181 | + map.removeAllOperationalLayers() |
| 182 | + let table = OGCFeatureCollectionTable(featureCollectionInfo: info) |
| 183 | + // Set the feature request mode to manual (only manual is currently |
| 184 | + // supported). In this mode, you must manually populate the table - |
| 185 | + // panning and zooming won't request features automatically. |
| 186 | + table.featureRequestMode = .manualCache |
| 187 | + _ = try await table.populateFromService( |
| 188 | + using: queryParameters, |
| 189 | + clearCache: false |
| 190 | + ) |
| 191 | + let featureLayer = FeatureLayer(featureTable: table) |
| 192 | + if let geometryType = table.geometryType { |
| 193 | + featureLayer.renderer = makeRenderer(withType: geometryType) |
| 194 | + map.addOperationalLayer(featureLayer) |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +private extension URL { |
| 201 | + /// The Daraa, Syria OGC API feature service URL. |
| 202 | + static var daraaService: URL { URL(string: "https://demo.ldproxy.net/daraa")! } |
| 203 | +} |
| 204 | + |
| 205 | +#Preview { |
| 206 | + BrowseOGCAPIFeatureServiceView() |
| 207 | +} |
0 commit comments