Display feature layers

View on GitHubSample viewer app

Display feature layers from various data sources.

screenshot

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

  1. Set the basemap with a BasemapStyle enum
  2. Create a feature layer with a Geodatabase
    • Instantiate a Geodatabase using the file name
    • Get a GeodatabaseFeatureTable by name from the geodatabase
    • Create a new FeatureLayer from the feature table
  3. Create a feature layer with a GeoPackage
    • Instantiate a GeoPackage using the file name and load it
    • Get the first GeoPackageFeatureTable from the geopackage's feature tables list
    • Create a new FeatureLayer from the feature table
  4. Create a feature layer with a PortalItem
    • Instantiate a PortalItem with a Portal and an item ID
    • Create a FeatureLayer with the portal item and layer ID
  5. Create a feature layer with a ServiceFeatureTable
    • Create a ServiceFeatureTable with a URL
    • Create a FeatureLayer with the feature table
  6. Create a feature layer with a shapefile
    • Create a ShapefileFeatureTable using the shapefile name
    • Create a feature layer from the feature table and load it
  7. Add the created feature layer to the Map's operationalLayers

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

DisplayFeatureLayers.cppDisplayFeatureLayers.cppDisplayFeatureLayers.hDisplayFeatureLayers.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
// [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

#include "DisplayFeatureLayers.h"

#include "Envelope.h"
#include "Error.h"
#include "FeatureLayer.h"
#include "Geodatabase.h"
#include "GeodatabaseFeatureTable.h"
#include "GeoPackage.h"
#include "GeoPackageFeatureTable.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"

#include <QFuture>
#include <QStandardPaths>

using namespace Esri::ArcGISRuntime;

// helper method to get cross platform data path
namespace
{
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);
}

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