Query OGC API with CQL Filters

View inC++QMLView on GitHubSample viewer app

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

screenshot

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

The sample loads displaying a maximum of 1000 features within the OGC API feature service. Select a sample CQL query from the drop down menu. Optionally, adjust the max features value in the text box, and enter a date range such as June 13, 2011 (06/13/2011) to January 7, 2012 (01/07/2012) to narrow down the results based on time extent. Press the "Query" 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 QueryParameters and set a CQL filter string to it using queryParameters.setWhereClause().
  3. Set the maximum amount of features to be returned with queryParamters.setMaxFeatures().
  4. Create a new TimeExtent from start and end date values, and set it to the queryParameters.setTimeExtent() method.
  5. Populate the OGC feature collection table using populateFromService() with the custom QueryParameters created in the previous steps.
  6. Use mapView.setViewpointGeometryAsync() with the OGC feature collection table extent 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

QueryOGCAPICQLFilters.cppQueryOGCAPICQLFilters.cppQueryOGCAPICQLFilters.hQueryOGCAPICQLFilters.qml
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
// [WriteFile Name=Query_OGC, Category=Layers]
// [Legal]
// Copyright 2021 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
// http://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.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "QueryOGCAPICQLFilters.h"

#include "FeatureLayer.h"
#include "Map.h"
#include "MapQuickView.h"
#include "OgcFeatureCollectionTable.h"
#include "SimpleLineSymbol.h"
#include "SimpleRenderer.h"

using namespace Esri::ArcGISRuntime;

QueryOGCAPICQLFilters::QueryOGCAPICQLFilters(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISTopographic, this))
{
  const QUrl serviceUrl = QUrl("https://demo.ldproxy.net/daraa");
  const QString collectionId = "TransportationGroundCrv";
  m_ogcFeatureCollectionTable = new OgcFeatureCollectionTable(serviceUrl, collectionId, this);

  // FeatureRequestMode::ManualCache specifies that features from the server will be stored locally for display and querying
  // In this mode, ServiceFeatureTable::populateFromService() must be called to populate the local cache
  m_ogcFeatureCollectionTable->setFeatureRequestMode(FeatureRequestMode::ManualCache);

  // m_ogcFeatureCollectionTable->load() will be automatically called when added to a FeatureLayer
  m_featureLayer = new FeatureLayer(m_ogcFeatureCollectionTable, this);

  m_map->operationalLayers()->append(m_featureLayer);

  // Set a renderer to it to visualize the OGC API features and zoom to features
  m_featureLayer->setRenderer(new SimpleRenderer(new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, Qt::darkMagenta, 3, this), this));
}

QueryOGCAPICQLFilters::~QueryOGCAPICQLFilters() = default;

void QueryOGCAPICQLFilters::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<QueryOGCAPICQLFilters>("Esri.Samples", 1, 0, "QueryOGCAPICQLFiltersSample");
}

MapQuickView* QueryOGCAPICQLFilters::mapView() const
{
  return m_mapView;
}

// Set the view (created in QML)
void QueryOGCAPICQLFilters::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

  m_mapView = mapView;
  m_mapView->setMap(m_map);
  m_mapView->setViewpoint(Viewpoint(32.62, 36.10, 200'000));

  QueryParameters queryParams;

  //Get the extent of the dataset
  m_dataSetExtent = m_ogcFeatureCollectionTable->extent();
  queryParams.setGeometry(m_dataSetExtent);
  queryParams.setWhereClause("F_CODE = 'AP010'");
  queryParams.setMaxFeatures(1000);

  // Populate the feature collection table with features that match the parameters, clear the cache, and store all table fields
  m_ogcFeatureCollectionTable->populateFromService(queryParams, true, {});

  emit mapViewChanged();
}

void QueryOGCAPICQLFilters::query(const QString& whereClause, const QString& maxFeature, const QString& fromDateString, const QString& toDateString)
{
  if (!m_ogcFeatureCollectionTable || !m_featureLayer || !m_mapView)
    return;

  // create the parameters
  QueryParameters queryParams;

  queryParams.setGeometry(m_mapView->currentViewpoint(ViewpointType::BoundingGeometry).targetGeometry().extent());
  queryParams.setWhereClause(whereClause);
  queryParams.setMaxFeatures(maxFeature.toUInt());

  if (!fromDateString.isEmpty() && !toDateString.isEmpty())
  {
    const QDateTime fromDate = QDateTime::fromString(fromDateString,"MM/dd/yyyy");
    const QDateTime toDate = QDateTime::fromString(toDateString,"MM/dd/yyyy");
    const TimeExtent timeExtent(fromDate.toUTC(), toDate.toUTC());
    queryParams.setTimeExtent(timeExtent);
  }

  // Populate the feature collection table with features that match the parameters,
  // clear the cache to prepare for the new query results, and store all table fields
  m_ogcFeatureCollectionTable->populateFromService(queryParams, true, {});
}

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