Query with CQL filters

View on GitHub

Query data from an OGC API feature service using CQL filters.

Screenshot of Query with CQL filters sample

Use case

CQL (Common Query Language) is an OGC-created query language used to query for subsets of features. Use CQL filters to narrow geometry results from an OGC feature table.

How to use the sample

Configure a CQL query by setting the where clause, max features count, and time extent. Tap the "Apply" button to see the query applied to the OGC API features shown on the map.

How it works

  1. Create an OGCFeatureCollectionTable object using a URL to an OGC API feature service and a collection ID.
  2. Create a QueryParameters object.
  3. Set the parameters' whereClause and maxFeatures properties.
  4. Create a TimeExtent object using Date objects for the start time and end time being queried. Set the timeExtent property on the parameters.
  5. Populate the feature table using populateFromService(using:clearCache:outFields:) with the custom query parameters created in the previous steps.
  6. Set the map view's viewpoint to view the newly queried features

Relevant API

  • OGCFeatureCollectionTable
  • QueryParameters
  • TimeExtent

About the data

The Daraa, Syria test data is OpenStreetMap data converted to the Topographic Data Store schema of NGA.

Additional information

See the OGC API website for more information on the OGC API family of standards. See the CQL documentation to learn more about the common query language.

Tags

browse, catalog, common query language, CQL, feature table, filter, OGC, OGC API, query, service, web

Sample Code

QueryWithCQLFiltersView.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2024 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

struct QueryWithCQLFiltersView: View {
    /// The view model for the sample.
    @StateObject private var model = Model()

    /// The count of feature from the last feature query result.
    @State private var resultFeatureCount = 0

    /// A Boolean value indicating whether the OCG feature collection table is currently being populated.
    @State private var isPopulatingFeatureTable = true

    /// A Boolean value indicating whether the CQL Filters form is presented.
    @State private var isShowingCQLFiltersForm = false

    /// The error shown in the error alert.
    @State private var error: Error?

    var body: some View {
        MapViewReader { mapViewProxy in
            MapView(map: model.map)
                .overlay(alignment: .top) {
                    Text(
                        isPopulatingFeatureTable
                        ? "Populating the feature table..."
                        : "Populated \(resultFeatureCount) features(s)."
                    )
                    .multilineTextAlignment(.center)
                    .frame(maxWidth: .infinity, alignment: .center)
                    .padding(8)
                    .background(.regularMaterial, ignoresSafeAreaEdges: .horizontal)
                }
                .toolbar {
                    ToolbarItem(placement: .bottomBar) {
                        Button("CQL Filters") {
                            isShowingCQLFiltersForm = true
                        }
                        .popover(isPresented: $isShowingCQLFiltersForm) {
                            CQLQueryFiltersForm(model: model) {
                                isPopulatingFeatureTable = true
                            }
                            .presentationDetents([.fraction(0.5)])
                            .frame(idealWidth: 320, idealHeight: 380)
                        }
                    }
                }
                .task(id: isPopulatingFeatureTable) {
                    guard isPopulatingFeatureTable else {
                        return
                    }
                    defer { isPopulatingFeatureTable = false }

                    do {
                        // Queries the feature table using the query parameters.
                        let featureQueryResult = try await model.ogcFeatureCollectionTable
                            .populateFromService(using: model.queryParameters, clearCache: true)

                        let queryResultFeatures = Array(featureQueryResult.features())
                        resultFeatureCount = queryResultFeatures.count

                        // Sets the viewpoint to the extent of the query result.
                        let geometries = queryResultFeatures.compactMap(\.geometry)
                        if let combinedExtent = GeometryEngine.combineExtents(of: geometries) {
                            await mapViewProxy.setViewpointGeometry(combinedExtent, padding: 20)
                        }
                    } catch {
                        self.error = error
                    }
                }
                .errorAlert(presentingError: $error)
        }
    }
}

// MARK: - CQLQueryFiltersForm

private extension QueryWithCQLFiltersView {
    /// A form with filter controls for a CQL query.
    struct CQLQueryFiltersForm: View {
        /// The view model for the sample.
        @ObservedObject var model: Model

        /// The action to perform when the "Apply" button is pressed.
        let onApply: () -> Void

        /// The action to dismiss the view.
        @Environment(\.dismiss) private var dismiss

        /// The attribute expression that defines features to be included in the query.
        @State private var selectedWhereClause = ""

        /// The maximum number of features the query should return.
        @State private var maxFeatures = 1000

        /// A Boolean value indicating whether the query includes a time extent.
        @State private var includesDateFilter = false

        /// The start date of the query's time extent.
        @State private var selectedStartDate: Date = {
            let components = DateComponents(year: 2011, month: 6, day: 13)
            return Calendar.current.date(from: components)!
        }()

        /// The end date of the query's time extent.
        @State private var selectedEndDate: Date = {
            let components = DateComponents(year: 2012, month: 1, day: 7)
            return Calendar.current.date(from: components)!
        }()

        var body: some View {
            NavigationStack {
                Form {
                    Picker("Where Clause", selection: $selectedWhereClause) {
                        ForEach(model.sampleWhereClauses, id: \.self) { whereClause in
                            Text(whereClause)
                        }
                    }
                    .pickerStyle(.navigationLink)

                    LabeledContent("Max Features") {
                        TextField("1000", value: $maxFeatures, format: .number)
                            .multilineTextAlignment(.trailing)
                            .onChange(of: maxFeatures) { newValue in
                                maxFeatures = newValue == 0 ? 1 : abs(newValue)
                            }
                    }

                    Section {
                        Toggle("Date Filter", isOn: $includesDateFilter)
                        DatePicker(
                            "Start Date",
                            selection: $selectedStartDate,
                            in: ...selectedEndDate,
                            displayedComponents: [.date]
                        )
                        .disabled(!includesDateFilter)
                        DatePicker(
                            "End Date",
                            selection: $selectedEndDate,
                            in: selectedStartDate...,
                            displayedComponents: [.date]
                        )
                        .disabled(!includesDateFilter)
                    }
                }
                .navigationTitle("CQL Filters")
                .navigationBarTitleDisplayMode(.inline)
                .toolbar {
                    ToolbarItem(placement: .cancellationAction) {
                        Button("Cancel", role: .cancel) {
                            dismiss()
                        }
                    }
                    ToolbarItem(placement: .confirmationAction) {
                        Button("Apply") {
                            updateQueryParameters()
                            onApply()
                            dismiss()
                        }
                    }
                }
            }
            .onAppear {
                // Sets the control's initial state using the values from the last query.
                selectedWhereClause = model.queryParameters.whereClause
                maxFeatures = model.queryParameters.maxFeatures

                if let timeExtent = model.queryParameters.timeExtent {
                    includesDateFilter = true
                    selectedStartDate = timeExtent.startDate!
                    selectedEndDate = timeExtent.endDate!
                }
            }
        }

        /// Updates the model's query parameters using the values from the form.
        private func updateQueryParameters() {
            model.queryParameters.whereClause = selectedWhereClause
            model.queryParameters.maxFeatures = maxFeatures

            model.queryParameters.timeExtent = includesDateFilter
            ? TimeExtent(startDate: selectedStartDate, endDate: selectedEndDate)
            : nil
        }
    }
}

// MARK: - Model

private extension QueryWithCQLFiltersView {
    /// The view model for the sample.
    final class Model: ObservableObject {
        /// A map with a topographic basemap.
        let map: Map = {
            let map = Map(basemapStyle: .arcGISTopographic)
            map.initialViewpoint = Viewpoint(latitude: 32.62, longitude: 36.10, scale: 20_000)
            return map
        }()

        /// An OGC API - Features feature collection table for the "Daraa" test dataset.
        let ogcFeatureCollectionTable: OGCFeatureCollectionTable = {
            let table = OGCFeatureCollectionTable(
                url: URL(string: "https://demo.ldproxy.net/daraa")!,
                collectionID: "TransportationGroundCrv"
            )
            // Sets the feature request mode to manual. In this mode, the table must be populated
            // manually. Panning and zooming won't request features automatically.
            table.featureRequestMode = .manualCache
            return table
        }()

        /// The sample where clause expressions to use with the query parameters.
        let sampleWhereClauses: [String] = [
            // An empty query.
            "",
            // A CQL2 TEXT query for features with an F_CODE property of "AP010".
            "F_CODE = 'AP010'",
            // A CQL2 JSON query for features with an F_CODE property of "AP010".
            #"{ "op": "=", "args": [ { "property": "F_CODE" }, "AP010" ] }"#,
            // A CQL2 TEXT query for features with an F_CODE attribute property similar to "AQ".
            "F_CODE LIKE 'AQ%'",
            // A CQL2 JSON query that combines the "before" and "eq" operators
            // with the logical "and" operator.
            #"{"op": "and", "args":[{ "op": "=", "args":[{ "property" : "F_CODE" }, "AP010"]}, { "op": "t_before", "args":[{ "property" : "ZI001_SDV"},"2013-01-01"]}]}"#
        ]

        /// The parameters for filtering the features returned form a query.
        let queryParameters: QueryParameters = {
            let queryParameters = QueryParameters()
            queryParameters.maxFeatures = 1000
            return queryParameters
        }()

        init() {
            // Creates a feature layer to visualize the OGC API features and adds it to the map.
            let ogcFeatureLayer = FeatureLayer(featureTable: ogcFeatureCollectionTable)
            let redLineSymbol = SimpleLineSymbol(style: .solid, color: .red, width: 3)
            ogcFeatureLayer.renderer = SimpleRenderer(symbol: redLineSymbol)

            map.addOperationalLayer(ogcFeatureLayer)
        }
    }
}

#Preview {
    QueryWithCQLFiltersView()
}

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