Display feature layers from various data sources.

Use case
Feature layers, like all layers, are visual representations of data and are used on a map or scene. In the case of feature layers, the underlying data is held in a feature table or feature service.
Feature services are useful for sharing vector GIS data with clients so that individual features can be queried, displayed, and edited. There are various online and offline methods to load feature services.
How to use the sample
Use the drop down to display different feature layers on the map. Pan and zoom the map to view the feature layers.
How it works
- Set the basemap with a
BasemapStyleenum - Create a feature layer with a
Geodatabase- Instantiate a
Geodatabaseusing the file name - Get a
GeodatabaseFeatureTableby name from the geodatabase - Create a new
FeatureLayerfrom the feature table
- Instantiate a
- Create a feature layer with a
GeoPackage- Instantiate a
GeoPackageusing the file name and load it - Get the first
GeoPackageFeatureTablefrom the geopackage’s feature tables list - Create a new
FeatureLayerfrom the feature table
- Instantiate a
- Create a feature layer with a
PortalItem- Instantiate a
PortalItemwith aPortaland an item ID - Create a
FeatureLayerwith the portal item and layer ID
- Instantiate a
- Create a feature layer with a
ServiceFeatureTable- Create a
ServiceFeatureTablewith a URL - Create a
FeatureLayerwith the feature table
- Create a
- Create a feature layer with a shapefile
- Create a
ShapefileFeatureTableusing the shapefile name - Create a feature layer from the feature table and load it
- Create a
- Add the created feature layer to the
Map’soperationalLayers
Relevant API
- FeatureLayer
- Geodatabase
- GeoPackage
- PortalItem
- ServiceFeatureTable
- ShapefileFeatureTable
Offline data
Read more about how to set up the sample’s offline data here.
| Link | Local Location |
|---|---|
| Los Angeles Trailheads geodatabase | <userhome>/ArcGIS/Runtime/Data/geodatabase/LA_Trails.geodatabase |
| Aurora, Colorado GeoPackage | <userhome>/ArcGIS/Runtime/Data/gpkg/AuroraCO.gpkg |
| Scottish Wildlife Trust Reserves Shapefile | <userhome>/ArcGIS/Runtime/Data/shp/ScottishWildlifeTrust_ReserveBoundaries_20201102.shp |
About the data
This sample uses the Northern Los Angeles County Geology Service, Trees of Portland portal item, Los Angeles Trailheads geodatabase, Aurora, Colorado GeoPackage, and Scottish Wildlife Trust Reserves Shapefile.
The Scottish Wildlife Trust shapefile data is provided from Scottish Wildlife Trust under CC-BY licence. Data © Scottish Wildlife Trust (2022).
Tags
feature, geodatabase, geopackage, layers, service, shapefile, table
Sample Code
// [WriteFile Name=DisplayFeatureLayers, Category=Layers]// [Legal]// Copyright 2022 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 "DisplayFeatureLayers.h"
// ArcGIS Maps SDK headers#include "Envelope.h"#include "Error.h"#include "FeatureLayer.h"#include "GeoPackage.h"#include "GeoPackageFeatureTable.h"#include "Geodatabase.h"#include "GeodatabaseFeatureTable.h"#include "LayerListModel.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "Point.h"#include "Portal.h"#include "PortalItem.h"#include "ServiceFeatureTable.h"#include "ShapefileFeatureTable.h"#include "SpatialReference.h"#include "Viewpoint.h"
// Qt headers#include <QFuture>#include <QStandardPaths>
using namespace Esri::ArcGISRuntime;
// helper method to get cross platform data pathnamespace{ QString defaultDataPath() { QString dataPath;
#ifdef Q_OS_IOS dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);#else dataPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);#endif
return dataPath + "/ArcGIS/Runtime/Data/"; }} // namespace
DisplayFeatureLayers::DisplayFeatureLayers(QObject* parent /* = nullptr */) : QObject(parent), m_map(new Map(BasemapStyle::ArcGISTopographic, this)){ // Each of these methods creates and adds a feature layer of the specified type to the map's operationalLayers LayerListModel addGeodatabaseLayer(); addGeopackageLayer(); addPortalItemLayer(); addServiceFeatureTableLayer(); addShapefileLayer();}
DisplayFeatureLayers::~DisplayFeatureLayers() = default;
void DisplayFeatureLayers::init(){ // Register the map view for QML qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<DisplayFeatureLayers>("Esri.Samples", 1, 0, "DisplayFeatureLayersSample");}
MapQuickView* DisplayFeatureLayers::mapView() const{ return m_mapView;}
// Set the view (created in QML)void DisplayFeatureLayers::setMapView(MapQuickView* mapView){ if (!mapView || mapView == m_mapView) { return; }
m_mapView = mapView; m_mapView->setMap(m_map);
emit mapViewChanged();}
void DisplayFeatureLayers::setLayerVisibility(FeatureLayerType featureLayerType){ if (!m_map || !m_mapView) { return; }
// Hide all other layers for (Layer* layer : *m_map->operationalLayers()) { layer->setVisible(false); }
// Make the selected feature layer visible and set the viewpoint to show the layer switch (featureLayerType) { case FeatureLayerType::Geodatabase: m_gdbFeatureLayer->setVisible(true); m_mapView->setViewpointGeometryAsync(m_gdbFeatureLayer->fullExtent()); break; case FeatureLayerType::Geopackage: m_gpkgFeatureLayer->setVisible(true); m_mapView->setViewpointGeometryAsync(m_gpkgFeatureLayer->fullExtent()); break; case FeatureLayerType::PortalItem: m_portalItemFeatureLayer->setVisible(true); // We can set padding on the viewpoint geometry. A negative value will zoom in. m_mapView->setViewpointGeometryAsync(m_portalItemFeatureLayer->fullExtent(), -10'000); break; case FeatureLayerType::ServiceFeatureTable: m_serviceFeatureTableFeatureLayer->setVisible(true); // The extent of this layer is very large so we can set the viewpoint to a specific point m_mapView->setViewpointAndWait(Viewpoint(Point(-13176752, 4090404, SpatialReference(102100)), 300'000)); break; case FeatureLayerType::Shapefile: m_shpFeatureLayer->setVisible(true); m_mapView->setViewpointGeometryAsync(m_shpFeatureLayer->fullExtent()); break; default: break; }}
void DisplayFeatureLayers::addGeodatabaseLayer(){ Geodatabase* gdb = new Geodatabase(defaultDataPath() + "geodatabase/LA_Trails.geodatabase", this); connect(gdb, &Geodatabase::doneLoading, this, [gdb, this](const Error& e) { if (!e.isEmpty()) { qDebug() << e.message() << e.additionalMessage(); return; }
GeodatabaseFeatureTable* gdbFeatureTable = gdb->geodatabaseFeatureTable("Trailheads"); m_gdbFeatureLayer = new FeatureLayer(gdbFeatureTable, this); m_map->operationalLayers()->append(m_gdbFeatureLayer);
// Because this feature layer is added asynchronously, we may need to set its visibility even after the initial setLayerVisibility() method is called m_gdbFeatureLayer->setVisible(false); });
gdb->load();}
void DisplayFeatureLayers::addGeopackageLayer(){ GeoPackage* gpkg = new GeoPackage(defaultDataPath() + "gpkg/AuroraCO.gpkg", this);
connect(gpkg, &GeoPackage::doneLoading, this, [this, gpkg](const Error& e) { if (!e.isEmpty()) { qDebug() << e.message() << e.additionalMessage(); return; }
if (!(gpkg->geoPackageFeatureTables().size() > 0)) { return; }
GeoPackageFeatureTable* gpkgFeatureTable = gpkg->geoPackageFeatureTables().at(0); m_gpkgFeatureLayer = new FeatureLayer(gpkgFeatureTable, this); m_map->operationalLayers()->append(m_gpkgFeatureLayer);
// Because this feature layer is added asynchronously, we may need to set its visibility even after the initial setLayerVisibility() method is called m_gpkgFeatureLayer->setVisible(false); });
gpkg->load(); // Note that you must call close() on GeoPackage to allow other processes to access it}
void DisplayFeatureLayers::addPortalItemLayer(){ Portal* portal = new Portal(this); PortalItem* portalItem = new PortalItem(portal, "1759fd3e8a324358a0c58d9a687a8578", this);
// A portal item can be many things from files to web maps. // We can check its type to ensure we're instantiating a feature layer with the // correct portal item type, but the portal item must first be loaded explicitly. // Here, the portal item is automatically loaded when it is used to instantiate a feature layer.
m_portalItemFeatureLayer = new FeatureLayer(portalItem, 0, this); m_map->operationalLayers()->append(m_portalItemFeatureLayer);}
void DisplayFeatureLayers::addServiceFeatureTableLayer(){ ServiceFeatureTable* serviceFeatureTable = new ServiceFeatureTable(QUrl("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Energy/Geology/FeatureServer/9"), this); m_serviceFeatureTableFeatureLayer = new FeatureLayer(serviceFeatureTable, this); m_map->operationalLayers()->append(m_serviceFeatureTableFeatureLayer);}
void DisplayFeatureLayers::addShapefileLayer(){ ShapefileFeatureTable* shpFeatureTable = new ShapefileFeatureTable(defaultDataPath() + "shp/ScottishWildlifeTrust_ReserveBoundaries_20201102.shp", this); m_shpFeatureLayer = new FeatureLayer(shpFeatureTable, this); m_map->operationalLayers()->append(m_shpFeatureLayer);}// [WriteFile Name=DisplayFeatureLayers, Category=Layers]// [Legal]// Copyright 2022 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 DISPLAYFEATURELAYERS_H#define DISPLAYFEATURELAYERS_H
// Qt headers#include <QObject>
namespace Esri::ArcGISRuntime{ class FeatureLayer; class Map; class MapQuickView;} // namespace Esri::ArcGISRuntime
Q_MOC_INCLUDE("MapQuickView.h");
class DisplayFeatureLayers : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)
public: explicit DisplayFeatureLayers(QObject* parent = nullptr); ~DisplayFeatureLayers() override;
enum class FeatureLayerType { Geodatabase, Geopackage, PortalItem, ServiceFeatureTable, Shapefile };
Q_ENUM(FeatureLayerType)
static void init();
Q_INVOKABLE void setLayerVisibility(DisplayFeatureLayers::FeatureLayerType featureLayerType);
signals: void mapViewChanged();
private: [[nodiscard]] Esri::ArcGISRuntime::MapQuickView* mapView() const; void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
void addGeodatabaseLayer(); void addGeopackageLayer(); void addPortalItemLayer(); void addServiceFeatureTableLayer(); void addShapefileLayer();
Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;
Esri::ArcGISRuntime::FeatureLayer* m_gdbFeatureLayer = nullptr; Esri::ArcGISRuntime::FeatureLayer* m_gpkgFeatureLayer = nullptr; Esri::ArcGISRuntime::FeatureLayer* m_portalItemFeatureLayer = nullptr; Esri::ArcGISRuntime::FeatureLayer* m_serviceFeatureTableFeatureLayer = nullptr; Esri::ArcGISRuntime::FeatureLayer* m_shpFeatureLayer = nullptr;};
#endif // DISPLAYFEATURELAYERS_H// [WriteFile Name=DisplayFeatureLayers, Category=Layers]// [Legal]// Copyright 2022 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 QtQuick.Layoutsimport Esri.Samples
Item {
// add a mapView component MapView { id: view anchors.fill: parent
Component.onCompleted: { // Set and keep the focus on MapView to enable keyboard navigation forceActiveFocus(); } }
// Declare the C++ instance which creates the map etc. and supply the view DisplayFeatureLayersSample { id: model mapView: view }
Rectangle { id: layerSelectRectangle anchors { top: parent.top left: parent.left margins: 5 } width: 225 height: column.height + 10 color: palette.base border.color: "black"
MouseArea { anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton onClicked: mouse => mouse.accepted = true onDoubleClicked: mouse => mouse.accepted = true onWheel: wheel => wheel.accepted = true }
ColumnLayout { id: column anchors.centerIn: parent Label { text: qsTr("Feature Layer Mode") } ComboBox { implicitWidth: layerSelectRectangle.width - 10 model: ["Geodatabase", "Geopackage", "Portal Item", "Service Feature Table", "Shapefile"] currentIndex: 3 onCurrentTextChanged: { model.setLayerVisibility(currentIndex); } } } }}// [WriteFile Name=DisplayFeatureLayers, Category=Layers]// [Legal]// Copyright 2022 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 "DisplayFeatureLayers.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QCommandLineParser>#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
int main(int argc, char* argv[]){ QGuiApplication app(argc, argv); app.setApplicationName(QString("DisplayFeatureLayers"));
// 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 DisplayFeatureLayers::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/DisplayFeatureLayers/main.qml"));
return app.exec();}// Copyright 2022 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
DisplayFeatureLayers { anchors.fill: parent }}