Browse OGC API Feature Service

View inQMLC++
View on GitHub
Sample viewer app

Browse an OGC API feature service for layers and add them to the map.

screenshot

Use case

OGC API standards are used for sharing geospatial data on the web. As an open standard, the OGC API aims to improve access to geospatial or location information and could be a good choice for sharing data internationally or between organizations. That data could be of any type, including, for example, transportation layers shared between government organizations and private businesses.

How to use the sample

Select a layer to display from the drop-down list of layers available from the OGC API service. The Daraa data is used as the default feature service, however, alternative feature services can be used.

How it works

  1. Create an OgcFeatureService object with a URL to an OGC API feature service.
  2. Obtain the OgcFeatureServiceInfo from OgcFeatureService.ServiceInfo.
  3. Create a list of feature collections from the OgcFeatureServiceInfo.FeatureCollectionInfos.
  4. When a feature collection is selected, create an OgcFeatureCollectionTable from the OgcFeatureCollectionInfo.
  5. Populate the OgcFeatureCollectionTable using PopulateFromService with QueryParameters that contain a MaxFeatures property.
  6. Create a feature layer from the feature table.
  7. Add the feature layer to the map.

Relevant API

  • OgcFeatureCollectionInfo
  • OgcFeatureCollectionTable
  • OgcFeatureService
  • OgcFeatureServiceInfo

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.

Tags

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

Sample Code

BrowseOGCAPIFeatureService.cppBrowseOGCAPIFeatureService.cppBrowseOGCAPIFeatureService.hBrowseOGCAPIFeatureService.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
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
// [WriteFile Name=BrowseOGCAPIFeatureService, 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 "BrowseOGCAPIFeatureService.h"

#include "FeatureLayer.h"
#include "Map.h"
#include "MapQuickView.h"
#include "OgcFeatureCollectionInfo.h"
#include "OgcFeatureCollectionTable.h"
#include "OgcFeatureService.h"
#include "OgcFeatureServiceInfo.h"

#include<QQmlEngine>

using namespace Esri::ArcGISRuntime;

BrowseOGCAPIFeatureService::BrowseOGCAPIFeatureService(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISTopographic, this))
{
  // Initialise OGC feature service
  m_featureServiceUrl = "https://demo.ldproxy.net/daraa";
  loadFeatureService(m_featureServiceUrl);
}

BrowseOGCAPIFeatureService::~BrowseOGCAPIFeatureService() = default;

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

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

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

  m_mapView = mapView;
  m_mapView->setMap(m_map);

  emit mapViewChanged();
}

QUrl BrowseOGCAPIFeatureService::featureServiceUrl() const
{
  return m_featureServiceUrl;
}

QStringList BrowseOGCAPIFeatureService::featureCollectionList() const
{
  return m_featureCollectionList;
}

QString BrowseOGCAPIFeatureService::errorMessage() const
{
  return m_errorMessage;
}

void BrowseOGCAPIFeatureService::setErrorMessage(const QString& message)
{
  m_errorMessage = message;
  emit errorMessageChanged();
}

void BrowseOGCAPIFeatureService::handleError(const Esri::ArcGISRuntime::Error& error)
{
  if (error.additionalMessage().isEmpty())
    setErrorMessage(error.message());
  else
    setErrorMessage(error.message() + "\n" + error.additionalMessage());
}

void BrowseOGCAPIFeatureService::loadFeatureService(const QUrl& url)
{
  if (m_featureService && m_featureService->loadStatus() == LoadStatus::Loading)
    return;

  clearExistingFeatureService();

  // Instantiate new OGCFeatureService object using url
  m_featureService = new OgcFeatureService(url, this);

  // Retrieve collection info when feature service has loaded
  connect(m_featureService, &OgcFeatureService::doneLoading, this, &BrowseOGCAPIFeatureService::retrieveCollectionInfos);

  // Connect errorOccurred to handleError()
  connect(m_featureService, &OgcFeatureService::errorOccurred, this, &BrowseOGCAPIFeatureService::handleError);

  // Connect to loadingChanged() to enable and disable UI buttons
  connect(m_featureService, &OgcFeatureService::loadStatusChanged, this, &BrowseOGCAPIFeatureService::serviceOrFeatureLoadingChanged);

  m_featureService->load();
}

void BrowseOGCAPIFeatureService::clearExistingFeatureService()
{
  m_collectionInfo.clear();
  if (m_serviceInfo != nullptr)
  {
    delete m_serviceInfo;
    m_serviceInfo = nullptr;
  }
  if (m_featureService != nullptr)
  {
    delete m_featureService;
    m_featureService = nullptr;
  }

  m_featureCollectionList.clear();
  emit featureCollectionListChanged();
}

void BrowseOGCAPIFeatureService::retrieveCollectionInfos()
{
  // Assign OGC service metadata to m_serviceInfo property
  m_serviceInfo = m_featureService->serviceInfo();

  // Assign information from each feature collection to the m_collectionInfo property
  m_collectionInfo = m_serviceInfo->featureCollectionInfos();

  createFeatureCollectionList();
}

void BrowseOGCAPIFeatureService::createFeatureCollectionList()
{
  m_featureCollectionList.clear();

  // Populate list with names of each feature collection
  for (OgcFeatureCollectionInfo* info : qAsConst(m_collectionInfo))
  {
    m_featureCollectionList.push_back(info->title());
  }

  emit featureCollectionListChanged();
}

void BrowseOGCAPIFeatureService::loadService(const QUrl& urlFromInterface)
{
  m_featureServiceUrl = urlFromInterface;
  emit urlChanged();
  loadFeatureService(m_featureServiceUrl);
}

void BrowseOGCAPIFeatureService::loadFeatureCollection(int selectedFeature)
{
  clearExistingFeatureLayer();

  // Create a CollectionInfo object from the selected feature collection
  OgcFeatureCollectionInfo* info = m_collectionInfo[selectedFeature];

  // Assign the CollectedInfo to the m_featureCollectionTable property
  m_featureCollectionTable = new OgcFeatureCollectionTable(info, this);

  // Set the feature request mode to manual (only manual is currently supported)
  // In this mode you must manually populate the table - panning and zooming won't request features automatically
  m_featureCollectionTable->setFeatureRequestMode(FeatureRequestMode::ManualCache);

  // Populate the OGC feature collection table
  QueryParameters queryParameters;
  queryParameters.setMaxFeatures(1000);
  m_featureCollectionTable->populateFromService(queryParameters, false, QStringList{ /* empty */ });

  // Create new Feature Layer from selected collection
  m_featureLayer = new FeatureLayer(m_featureCollectionTable, this);

  // Add feature layer to operational layers when it has finished loading
  connect(m_featureLayer, &FeatureLayer::doneLoading, this, &BrowseOGCAPIFeatureService::addFeatureLayerToMap);

  // Connect errorOccurred to handleError()
  connect(m_featureLayer, &FeatureLayer::errorOccurred, this, &BrowseOGCAPIFeatureService::handleError);

  // Connect to loadingChanged() to enable and disable UI buttons
  connect(m_featureLayer, &FeatureLayer::loadStatusChanged, this, &BrowseOGCAPIFeatureService::serviceOrFeatureLoadingChanged);

  m_featureLayer->load();
}

void BrowseOGCAPIFeatureService::clearExistingFeatureLayer()
{
  if (m_featureCollectionTable != nullptr)
  {
    delete m_featureCollectionTable;
    m_featureCollectionTable = nullptr;
  }

  if (m_featureLayer != nullptr)
  {
    delete m_featureLayer;
    m_featureLayer = nullptr;
  }
}

void BrowseOGCAPIFeatureService::addFeatureLayerToMap()
{
  // Adjust the viewpoint to match the extent of the layer
  m_mapView->setViewpointGeometry(m_featureLayer->fullExtent());

  // Clear any existing layers and add current layer to map
  m_map->operationalLayers()->clear();
  m_map->operationalLayers()->append(m_featureLayer);
}

bool BrowseOGCAPIFeatureService::serviceOrFeatureLoading() const
{
  if (m_featureService && m_featureService->loadStatus() == LoadStatus::Loading)
    return true;

  else if (m_featureLayer && m_featureLayer->loadStatus() == LoadStatus::Loading)
    return true;

  return false;
}

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