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

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
- Create an
OgcFeatureServiceobject with a URL to an OGC API feature service. - Obtain the
OgcFeatureServiceInfofromOgcFeatureService.ServiceInfo. - Create a list of feature collections from the
OgcFeatureServiceInfo.FeatureCollectionInfos. - When a feature collection is selected, create an
OgcFeatureCollectionTablefrom theOgcFeatureCollectionInfo. - Populate the
OgcFeatureCollectionTableusingPopulateFromServiceAsyncwithQueryParametersthat contain aMaxFeaturesproperty. - Create a feature layer from the feature table.
- 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
// [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
// sample headers#include "BrowseOGCAPIFeatureService.h"
// ArcGIS Maps SDK headers#include "Envelope.h"#include "Error.h"#include "FeatureLayer.h"#include "GeodatabaseTypes.h"#include "LayerListModel.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "OgcFeatureCollectionInfo.h"#include "OgcFeatureCollectionTable.h"#include "OgcFeatureService.h"#include "OgcFeatureServiceInfo.h"#include "QueryParameters.h"
// Qt headers#include <QFuture>#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 : std::as_const(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); auto future = m_featureCollectionTable->populateFromServiceAsync(queryParameters, false, QStringList{ /* empty */ }); Q_UNUSED(future)
// 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->setViewpointGeometryAsync(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;}// [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]
#ifndef BROWSEOGCAPIFEATURESERVICE_H#define BROWSEOGCAPIFEATURESERVICE_H
// Qt headers#include <QObject>#include <QUrl>
namespace Esri::ArcGISRuntime{class Error;class FeatureLayer;class Map;class MapQuickView;class OgcFeatureCollectionTable;class OgcFeatureCollectionInfo;class OgcFeatureService;class OgcFeatureServiceInfo;}
Q_MOC_INCLUDE("MapQuickView.h")
class BrowseOGCAPIFeatureService : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged) Q_PROPERTY(QString errorMessage READ errorMessage WRITE setErrorMessage NOTIFY errorMessageChanged) Q_PROPERTY(QUrl featureServiceUrl READ featureServiceUrl NOTIFY urlChanged) Q_PROPERTY(QStringList featureCollectionList READ featureCollectionList NOTIFY featureCollectionListChanged) Q_PROPERTY(bool serviceOrFeatureLoading READ serviceOrFeatureLoading NOTIFY serviceOrFeatureLoadingChanged)
public: explicit BrowseOGCAPIFeatureService(QObject* parent = nullptr); ~BrowseOGCAPIFeatureService();
static void init();
Q_INVOKABLE void loadService(const QUrl& urlFromInterface); Q_INVOKABLE void loadFeatureCollection(int selectedFeature);
signals: void mapViewChanged(); void errorMessageChanged(); void urlChanged(); void featureCollectionListChanged(); void serviceOrFeatureLoadingChanged();
private: Esri::ArcGISRuntime::MapQuickView* mapView() const; void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView); QUrl featureServiceUrl() const; QStringList featureCollectionList() const; QString errorMessage() const; void setErrorMessage(const QString& message); void handleError(const Esri::ArcGISRuntime::Error& error); void loadFeatureService(const QUrl& url); void clearExistingFeatureService(); void retrieveCollectionInfos(); void createFeatureCollectionList(); void clearExistingFeatureLayer(); void addFeatureLayerToMap(); bool serviceOrFeatureLoading() const;
Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; QString m_errorMessage = ""; QUrl m_featureServiceUrl; Esri::ArcGISRuntime::OgcFeatureService* m_featureService = nullptr; Esri::ArcGISRuntime::OgcFeatureServiceInfo* m_serviceInfo = nullptr; QList<Esri::ArcGISRuntime::OgcFeatureCollectionInfo*> m_collectionInfo; QStringList m_featureCollectionList; Esri::ArcGISRuntime::OgcFeatureCollectionTable* m_featureCollectionTable = nullptr; Esri::ArcGISRuntime::FeatureLayer* m_featureLayer = nullptr;};
#endif // BROWSEOGCAPIFEATURESERVICE_H// [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]
import QtQuickimport QtQuick.Controlsimport Esri.Samplesimport QtQuick.Layouts
Item { // Declare the C++ instance which creates the map etc. and supply the view BrowseOGCAPIFeatureServiceSample { id: model mapView: view }
// add a mapView component MapView { id: view anchors.fill: parent
Component.onCompleted: { // Set the focus on MapView to initially enable keyboard navigation forceActiveFocus(); }
// Add the OGC feature service UI element Control { id: uiControl anchors { right: view.right top: view.top margins: 10 } padding: 10 background: Rectangle { color: "white" border.color: "black" } contentItem: GridLayout { columns: 2 Label { id: instructionLabel text: "Load the service, then select a layer for display" font.bold: true verticalAlignment: "AlignVCenter" horizontalAlignment: "AlignHCenter" Layout.columnSpan: 2 Layout.fillWidth: true } TextField { id: serviceURLBox text: model.featureServiceUrl Layout.fillWidth: true selectByMouse: true } Button { id: connectButton text: "Load service" enabled: !model.serviceOrFeatureLoading onClicked: model.loadService(serviceURLBox.text);
} ComboBox { id: featureList model: model.featureCollectionList Layout.columnSpan: 2 Layout.fillWidth: true Layout.fillHeight: true } Button { id: loadLayerButton text: "Load selected layer" enabled: !model.serviceOrFeatureLoading && featureList.currentIndex >= 0 onClicked: model.loadFeatureCollection(featureList.currentIndex); Layout.columnSpan: 2 Layout.fillWidth: true } } }
// Pop-up error message box Dialog { id: errorMessageBox visible: model.errorMessage === ""? false : true title: model.errorMessage standardButtons: Dialog.Ok x: (parent.width - width) / 2 y: (parent.height - height) / 2 } }}// [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]
// sample headers#include "BrowseOGCAPIFeatureService.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
int main(int argc, char *argv[]){ Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setUseLegacyAuthentication(false); QGuiApplication app(argc, argv); app.setApplicationName(QString("BrowseOGCAPIFeatureService"));
// Use of ArcGIS location services, such as basemap styles, geocoding, and routing services, // requires an access token. For more information see // https://links.esri.com/arcgis-runtime-security-auth.
// The following methods grant an access token:
// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account. // To generate a token, a user logs in to the app with an ArcGIS account that is part of an // organization in ArcGIS Online or ArcGIS Enterprise.
// 2. API key authentication: Get a long-lived access token that gives your application access to // ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key. // Copy the API Key access token.
const QString accessToken = QString("");
if (accessToken.isEmpty()) { qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" << "you to authenticate with an ArcGIS account or set the API Key property."; } else { Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setApiKey(accessToken); }
// Initialize the sample BrowseOGCAPIFeatureService::init();
// Initialize application view QQmlApplicationEngine engine; // Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
#ifdef ARCGIS_RUNTIME_IMPORT_PATH_2 engine.addImportPath(ARCGIS_RUNTIME_IMPORT_PATH_2);#endif
// Set the source engine.load(QUrl("qrc:/Samples/Layers/BrowseOGCAPIFeatureService/main.qml"));
return app.exec();}// 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.
import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
BrowseOGCAPIFeatureService { anchors.fill: parent }}