Feature layer selection

View on GitHubSample viewer app

Select features in a feature layer.

Map with features Selected features

Use case

Selecting features, whether by query or identify, can be an important step both in editing data and in visualizing results. One possible use case would be to query a feature layer containing street furniture. A query might look for type "bench" and return a list of bench features contained in the features with an attribute of type bench. These might be selected for further editing or may just be highlighted visually.

How to use the sample

Tap on a feature in the map. All features within a given tolerance (in pixels) of the tap will be selected.

How it works

  1. Create an AGSServiceFeatureTable from a feature service URL.
  2. Create an AGSFeatureLayer from the service feature table.
  3. Identify nearby features at the tapped location using the AGSGeoView.identifyLayer(_:screenPoint:tolerance:returnPopupsOnly:maximumResults:completion:) method on the map view.
  4. Select all identified features in the feature layer with AGSFeatureLayer.select(_:).

Relevant API

  • AGSFeature
  • AGSFeatureLayer
  • AGSServiceFeatureTable

About the data

This sample uses the Gross Domestic Product Per Capita, 1960-2016 feature service. Only the 2016 GDP values are shown.

Tags

features, layers, select, selection, tolerance

Sample Code

FeatureLayerSelectionViewController.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
// Copyright 2016 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 UIKit
import ArcGIS

class FeatureLayerSelectionViewController: UIViewController {
    // MARK: Storyboard views

    /// A label to show the selection status.
    @IBOutlet weak var statusLabel: UILabel!
    /// The map view managed by the view controller.
    @IBOutlet weak var mapView: AGSMapView! {
        didSet {
            mapView.map = makeMap()
            mapView.setViewpoint(AGSViewpoint(targetExtent: AGSEnvelope(xMin: -180, yMin: -90, xMax: 180, yMax: 90, spatialReference: .wgs84())))
            mapView.touchDelegate = self
            mapView.selectionProperties.color = .cyan
        }
    }

    // MARK: Properties and methods

    /// An array of selected features.
    var selectedFeatures = [AGSFeature]()

    /// The feature layer created from a feature service.
    let featureLayer: AGSFeatureLayer = {
        // A feature layer visualizing GDP per capita.
        let featureLayer = AGSFeatureLayer(
            item: AGSPortalItem(
                portal: .arcGISOnline(withLoginRequired: false),
                itemID: "10d76a5b015647279b165f3a64c2524f"
            )
        )
        return featureLayer
    }()

    /// Create a map.
    ///
    /// - Returns: An `AGSMap` object.
    func makeMap() -> AGSMap {
        // Initialize map with topographic basemap.
        let map = AGSMap(basemapStyle: .arcGISStreets)
        // Add feature layer to the map.
        map.operationalLayers.add(featureLayer)
        return map
    }

    // MARK: UIViewController

    override func viewDidLoad() {
        super.viewDidLoad()
        // Add the source code button item to the right of navigation bar.
        (navigationItem.rightBarButtonItem as? SourceCodeBarButtonItem)?.filenames = ["FeatureLayerSelectionViewController"]
        // Load the feature layer.
        featureLayer.load { [weak self] (error) in
            if let error = error {
                self?.presentAlert(error: error)
            } else {
                self?.mapView.setViewpointScale(2e8)
            }
        }
    }
}

// MARK: - AGSGeoViewTouchDelegate

extension FeatureLayerSelectionViewController: AGSGeoViewTouchDelegate {
    func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
        // Identify features at the tapped location.
        mapView.identifyLayer(featureLayer, screenPoint: screenPoint, tolerance: 12.0, returnPopupsOnly: false, maximumResults: 10) { [weak self] result in
            guard let self = self else { return }
            if let error = result.error {
                self.presentAlert(error: error)
                return
            }
            // Un-select previous results.
            self.featureLayer.unselectFeatures(self.selectedFeatures)
            // Select current results.
            self.selectedFeatures = result.geoElements as! [AGSFeature]
            self.featureLayer.select(self.selectedFeatures)
            // Show status.
            self.statusLabel.text = "\(result.geoElements.count) feature(s) selected."
        }
    }
}

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