Display feature layers

View on GitHubSample viewer app

Display feature layers from various data sources.

Display feature layers

Use case

Feature layers, like all other 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

Tap the button on the toolbar to add feature layers, from different sources, to the map. Pan and zoom the map to view the feature layers.

How it works

  1. Set the basemap with an AGSBasemapStyle.
  2. Load a feature layer with a URL.
    i. Create an AGSServiceFeatureTable from a URL.
    ii. Create an AGSFeatureLayer with the feature table.
  3. Load a feature layer with a portal item.
    i. Create an AGSPortalItem with the portal and item ID.
    ii. Create an AGSFeatureLayer with the portal item and with or without the layer ID.
  4. Load a feature layer with a geodatabase.
    i. Instantiate and load an AGSGeodatabase using the file name.
    ii. Get the feature table from the geodatabase with the feature table's name.
    iii. Create an AGSFeatureLayer from the feature table.
  5. Load a feature layer with a geopackage.
    i. Instantiate and load a geopackage using its file name.
    ii. Get the first AGSGeoPackageFeatureTable from the geoPackageFeatureTables array.
    iii. Create an AGSFeatureLayer from the feature table.
  6. Load a feature layer with a shapefile.
    i. Create an AGSShapefileFeatureTable using the shapefile name.
    ii. Create an AGSFeatureLayer from the feature table and load it.
  7. Add the feature layer to the map's operationalLayers.

Relevant API

  • AGSFeatureLayer
  • AGSGeodatabase
  • AGSGeoPackageFeatureTable
  • AGSPortalItem
  • AGSServiceFeatureTable
  • AGSShapefileFeatureTable

About the data

This sample uses the Naperville damage assessment 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

DisplayFeatureLayersViewController.swift
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
// 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
//
//   https://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 UIKit
import ArcGIS

class DisplayFeatureLayersViewController: UIViewController {
    // MARK: Storyboard views

    /// The map view managed by the view controller.
    @IBOutlet var mapView: AGSMapView! {
        didSet {
            // Initialize map with basemap.
            mapView.map = AGSMap(basemapStyle: .arcGISTopographic)
        }
    }

    @IBOutlet var changeFeatureLayerBarButtonItem: UIBarButtonItem!

    // MARK: Instance properties
    enum FeatureLayerSource {
        case serviceFeatureTable
        case portalItem
        case geodatabase
        case geopackage
        case shapefile
    }

    var selectedFeatureLayerSource: FeatureLayerSource?
    var geodatabase: AGSGeodatabase!
    var geoPackage: AGSGeoPackage!

    // MARK: Actions

    @IBAction func changeFeatureLayer() {
        let alertController = UIAlertController(title: "Select a feature layer source", message: nil, preferredStyle: .actionSheet)
        // Add an action to load a feature layer from a URL.
        let featureServiceURLAction = UIAlertAction(title: "Service Feature Table", style: .default) { [weak self] (_) in
            guard let self = self else { return }
            self.selectedFeatureLayerSource = .serviceFeatureTable
            self.loadFeatureServiceURL()
        }
        alertController.addAction(featureServiceURLAction)
        featureServiceURLAction.isEnabled = selectedFeatureLayerSource != .serviceFeatureTable
        // Add an action to load a feature layer from a portal item.
        let portalItemAction = UIAlertAction(title: "Portal Item", style: .default) { [weak self] (_) in
            guard let self = self else { return }
            self.selectedFeatureLayerSource = .portalItem
            self.loadPortalItem()
        }
        portalItemAction.isEnabled = selectedFeatureLayerSource != .portalItem
        alertController.addAction(portalItemAction)
        // Add an action to load a feature layer from a geodatabase.
        let geodatabaseAction = UIAlertAction(title: "Geodatabase", style: .default) { [weak self] (_) in
            guard let self = self else { return }
            self.selectedFeatureLayerSource = .geodatabase
            self.loadGeodatabase()
        }
        geodatabaseAction.isEnabled = selectedFeatureLayerSource != .geodatabase
        alertController.addAction(geodatabaseAction)
        // Add an action to load a feature layer from a shapefile.
        let geopackageAction = UIAlertAction(title: "Geopackage", style: .default) { [weak self] (_) in
            guard let self = self else { return }
            self.selectedFeatureLayerSource = .geopackage
            self.loadGeopackage()
        }
        geopackageAction.isEnabled = selectedFeatureLayerSource != .geopackage
        alertController.addAction(geopackageAction)
        // Add an action to load a feature layer from a shapefile.
        let shapefileAction = UIAlertAction(title: "Shapefile", style: .default) { [weak self] (_) in
            guard let self = self else { return }
            self.selectedFeatureLayerSource = .shapefile
            self.loadShapefile()
        }
        shapefileAction.isEnabled = selectedFeatureLayerSource != .shapefile
        alertController.addAction(shapefileAction)

        // Add "cancel" item.
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        alertController.addAction(cancelAction)

        alertController.popoverPresentationController?.barButtonItem = changeFeatureLayerBarButtonItem
        present(alertController, animated: true)
    }

    // MARK: Helper functions

    /// Load a feature layer with a URL.
    func loadFeatureServiceURL() {
        // Initialize the service feature table using a URL.
        let featureTable = AGSServiceFeatureTable(url: URL(string: "https://sampleserver7.arcgisonline.com/server/rest/services/DamageAssessment/FeatureServer/0")!)
        // Create a feature layer with the feature table.
        let featureLayer = AGSFeatureLayer(featureTable: featureTable)
        // Set the viewpoint to the Los Angeles National Forest.
        let viewpoint = AGSViewpoint(latitude: 41.773519, longitude: -88.143104, scale: 4e3)
        setFeatureLayer(featureLayer, viewpoint: viewpoint)
    }

    /// Load a feature layer with a portal item.
    func loadPortalItem() {
        // Set the portal.
        let portal = AGSPortal.arcGISOnline(withLoginRequired: false)
        // Create the portal item with the item ID for the Portland tree service data.
        let item = AGSPortalItem(portal: portal, itemID: "1759fd3e8a324358a0c58d9a687a8578")
        // Create the feature layer with the item and layer ID.
        let featureLayer = AGSFeatureLayer(item: item)
        // Set the viewpoint to Portland, Oregon.
        let viewpoint = AGSViewpoint(latitude: 45.5266, longitude: -122.6219, scale: 6e3)
        setFeatureLayer(featureLayer, viewpoint: viewpoint)
    }

    /// Load a feature layer with a local geodatabase.
    func loadGeodatabase() {
        if let geodatabase = geodatabase {
            setFeatureLayer(from: geodatabase)
        } else {
            // Instantiate the geodatabase with the file name.
            geodatabase = AGSGeodatabase(name: "LA_Trails")

            // Load the geodatabase for feature tables.
            geodatabase.load { [weak self] (error: Error?) in
                guard let self = self else { return }
                if let error = error {
                    self.presentAlert(error: error)
                } else {
                    self.setFeatureLayer(from: self.geodatabase)
                }
            }
        }
    }

    /// Load a feature layer with a local geopackage.
    func loadGeopackage() {
        if let geoPackage = geoPackage {
            setFeatureLayer(from: geoPackage)
        } else {
            // Create a geopackage from a named bundle resource.
            geoPackage = AGSGeoPackage(name: "AuroraCO")

            // Load the geopackage.
            geoPackage.load { [weak self] (error: Error?) in
                guard let self = self else { return }
                if let error = error {
                    self.presentAlert(error: error)
                } else {
                    self.setFeatureLayer(from: self.geoPackage)
                }
            }
        }
    }

    /// Load a feature layer with a local shapefile.
    func loadShapefile() {
        // Create a shapefile feature table from a named bundle resource.
        let shapefileTable = AGSShapefileFeatureTable(name: "ScottishWildlifeTrust_ReserveBoundaries_20201102")

        // Create a feature layer for the shapefile feature table.
        let featureLayer = AGSFeatureLayer(featureTable: shapefileTable)
        // Set the viewpoint to Scotland.
        let viewpoint = AGSViewpoint(latitude: 56.641344, longitude: -3.889066, scale: 6e6)
        setFeatureLayer(featureLayer, viewpoint: viewpoint)
    }

    func setFeatureLayer(from geodatabase: AGSGeodatabase) {
        // Get the feature table with the name.
        let featureTable = geodatabase.geodatabaseFeatureTable(withName: "Trailheads")!
        // Create a feature layer with the feature table.
        let featureLayer = AGSFeatureLayer(featureTable: featureTable)
        // Set the viewpoint to Malibu, California.
        let viewpoint = AGSViewpoint(latitude: 34.0772, longitude: -118.7989, scale: 6e5)
        self.setFeatureLayer(featureLayer, viewpoint: viewpoint)
    }

    func setFeatureLayer(from geoPackage: AGSGeoPackage) {
        // Add the first feature layer from the geopackage to the map.
        if let featureTable = geoPackage.geoPackageFeatureTables.first {
            // Create the feature layer with the feature table.
            let featureLayer = AGSFeatureLayer(featureTable: featureTable)
            // Set the viewpoint to Aurora, Colorado.
            let viewpoint = AGSViewpoint(latitude: 39.7294, longitude: -104.8319, scale: 5e5)
            self.setFeatureLayer(featureLayer, viewpoint: viewpoint)
        }
    }

    /// Add the feature layer to the map and set the viewpoint.
    /// - Parameters:
    ///   - featureLayer: The `AGSFeatureLayer` to display and add to the map.
    ///   - viewpoint: The `AGSViewpoint` to change the map to.
    func setFeatureLayer(_ featureLayer: AGSFeatureLayer, viewpoint: AGSViewpoint) {
        mapView.map?.operationalLayers.setArray([featureLayer])
        mapView.setViewpoint(viewpoint)
    }

    // MARK: UIViewController

    override func viewDidLoad() {
        super.viewDidLoad()
        AGSAuthenticationManager.shared().delegate = self
        // Add the source code button item to the right of navigation bar.
        (navigationItem.rightBarButtonItem as? SourceCodeBarButtonItem)?.filenames = ["DisplayFeatureLayersViewController"]
    }
}

/// Set the credentials to allow access for the Naperville damage assessment feature service.
extension DisplayFeatureLayersViewController: AGSAuthenticationManagerDelegate {
    func authenticationManager(_ authenticationManager: AGSAuthenticationManager, didReceive challenge: AGSAuthenticationChallenge) {
        // NOTE: Never hardcode login information in a production application. This is done solely for the sake of the sample.
        let credential = AGSCredential(user: "viewer01", password: "I68VGU^nMurF")
        challenge.continue(with: credential)
    }
}

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