Skip to content

Browse WFS layers

View on GitHub

Browse a WFS service for layers and add them to the map.

Image of browse WFS layers sample Image of browse WFS layers service view Image of browse WFS layers layer view

Use case

Services often have multiple layers available for display. For example, a feature service for a city might have layers representing roads, landmasses, building footprints, parks, and facilities. A user can choose to only show the road network and parks for a park accessibility analysis.

How to use the sample

A list of layers in the WFS service will be shown. Select a layer to display.

Some WFS services return coordinates in X,Y order, while others return coordinates in lat/long (Y,X) order. If you don't see features rendered or you see features in the wrong location, use the checkbox to change the coordinate order and reload.

How it works

  1. Create a WFSService object with a URL to a WFS feature service.
  2. Obtain an array of WFSLayerInfo objects using WFSServiceInfo.layerInfos.
  3. When a layer is selected, create a WFSFeatureTable instance with the selected WFSLayerInfo object.
    • Set the axis order if necessary.
  4. Create a feature layer from the feature table.
  5. Add the feature layer to the map.

Relevant API

  • FeatureLayer
  • WFSFeatureTable
  • WFSFeatureTable.OGCAxisOrder
  • WFSLayerInfo
  • WFSService
  • WFSServiceInfo

About the data

The sample is configured with a sample WFS service, but you can load other WFS services if desired. The default service shows Seattle downtown features hosted on ArcGIS Online.

Tags

browse, catalog, feature, layers, OGC, service, web, WFS

Sample Code

BrowseWFSLayersView.swift
Use dark colors for code blocksCopy
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
184
185
186
187
188
189
190
191
192
// Copyright 2025 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
//
//   https://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.

import ArcGIS
import SwiftUI

/// The initial view of the Browse WFS Layers sample.
struct BrowseWFSLayersView: View {
    /// The URL of the service to load.
    @State private var serviceURL = URL(string: "https://dservices2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/services/Seattle_Downtown_Features/WFSServer?service=wfs&request=getcapabilities")!
    /// A Boolean value indicating whether the service view is pushed on the
    /// navigation stack.
    @State private var isServiceViewPresented = false

    var body: some View {
        Form {
            Section {
                TextField(
                    "WFS Service",
                    value: $serviceURL,
                    format: .url,
                    prompt: Text("WFS Service URL")
                )
                Button("Load") {
                    isServiceViewPresented = true
                }
                .containerRelativeFrame(.horizontal)
                .multilineTextAlignment(.center)
            }
        }
        .navigationDestination(isPresented: $isServiceViewPresented) {
            WFSServiceView(serviceURL: serviceURL)
        }
    }
}

/// A view that loads a WFS service and lists its layers.
private struct WFSServiceView: View {
    /// The URL of the WFS service to load.
    let serviceURL: URL

    @Environment(\.dismiss) private var dismiss

    /// The loaded WFS service.
    @State private var service: WFSService?
    /// The error if the service failed to load, otherwise `nil`.
    @State private var serviceLoadError: Error?
    /// A Boolean value indicating whether the service failed to load.
    @State private var serviceLoadDidFail = false

    var body: some View {
        if let serviceInfo = service?.serviceInfo {
            Form {
                Section("Layers") {
                    ForEach(serviceInfo.layerInfos, id: \.name) { layerInfo in
                        NavigationLink(layerInfo.title) {
                            WFSServiceLayerView(layerInfo: layerInfo)
                        }
                    }
                }
            }
            .navigationTitle(serviceInfo.title)
        } else {
            ProgressView()
                .navigationTitle("Loading Service")
                .task {
                    let service = WFSService(url: serviceURL)
                    do {
                        try await withTaskCancellationHandler {
                            try await service.load()
                        } onCancel: {
                            service.cancelLoad()
                        }
                        self.service = service
                    } catch {
                        serviceLoadError = error
                        serviceLoadDidFail = true
                    }
                }
                .alert("Error", isPresented: $serviceLoadDidFail, presenting: serviceLoadError) { _ in
                    Button("OK") {
                        dismiss()
                    }
                } message: { error in
                    Text(String(reflecting: error))
                }
        }
    }
}

/// A view that displays a layer of a WFS service.
private struct WFSServiceLayerView: View {
    /// The metadata of the layer to display.
    let layerInfo: WFSLayerInfo

    /// The map that displays the layer.
    @State private var map = Map(basemapStyle: .arcGISImageryStandard)
    /// The area of the map to display.
    @State private var viewpoint: Viewpoint?
    /// The error if the populate operation failed, otherwise `nil`.
    @State private var populateError: Error?
    /// A Boolean value indicating whether a query operation is in progress.
    @State private var isQuerying = false
    /// A Boolean value indicating whether the axis order should be swapped.
    @State private var swapAxisOrder = false

    var body: some View {
        MapView(map: map, viewpoint: viewpoint)
            .onViewpointChanged(kind: .boundingGeometry) { newViewpoint in
                viewpoint = newViewpoint
            }
            .onAppear {
                if let extent = layerInfo.extent {
                    viewpoint = Viewpoint(boundingGeometry: extent)
                }
            }
            .task(id: swapAxisOrder) {
                map.removeAllOperationalLayers()
                let featureTable = WFSFeatureTable(layerInfo: layerInfo)
                // Sets the feature request mode to 'manualCache'. In this mode,
                // you must manually populate the table - panning and zooming
                // won't request features automatically.
                featureTable.featureRequestMode = .manualCache
                featureTable.axisOrder = if swapAxisOrder {
                    .swap
                } else {
                    .noSwap
                }
                do {
                    isQuerying = true
                    _ = try await featureTable.populateFromService(using: nil, clearCache: false, outFields: [])
                    let featureLayer = FeatureLayer(featureTable: featureTable)
                    if let geometryType = featureTable.geometryType,
                       let renderer = renderer(for: geometryType) {
                        featureLayer.renderer = renderer
                    }
                    map.addOperationalLayer(featureLayer)
                    isQuerying = false
                } catch is CancellationError {
                    // Do nothing.
                } catch {
                    self.populateError = error
                    isQuerying = false
                }
            }
            .navigationTitle(layerInfo.title)
            .toolbar {
                if isQuerying {
                    ToolbarItem(placement: .topBarTrailing) {
                        ProgressView()
                            .progressViewStyle(.circular)
                    }
                }
                ToolbarItem(placement: .bottomBar) {
                    Toggle(isOn: $swapAxisOrder) {
                        Text("Swap Axis Order")
                        Text("Try if nothing appears after load.")
                    }
                    .toggleStyle(.switch)
                }
            }
            .errorAlert(presentingError: $populateError)
    }

    /// Returns a renderer for the given geometry type.
    /// - Parameter geometryType: The type of geometry to be rendered.
    /// - Returns: A renderer.
    func renderer(for geometryType: Geometry.Type) -> Renderer? {
        let symbol: Symbol? = switch geometryType {
        case is Point.Type, is Multipoint.Type:
            SimpleMarkerSymbol(style: .circle, color: .blue, size: 4)
        case is Envelope.Type, is ArcGIS.Polygon.Type:
            SimpleFillSymbol(style: .solid, color: .blue)
        case is Polyline.Type:
            SimpleLineSymbol(style: .solid, color: .blue, width: 1)
        default:
            nil
        }
        return symbol.map(SimpleRenderer.init(symbol:))
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.