Feature layer (shapefile)

View on GitHub
Sample viewer app

Open a shapefile stored on the device and display it as a feature layer with default symbology.

Feature layer (shapefile)

Use case

Shapefiles store location, shape and attributes of geospatial vector data. Shapefiles can be loaded directly into ArcGIS Runtime.

How to use the sample

When launched, the sample displays shapefiles from Aurora, Colorado. Pan and zoom around the map. View the data loaded from the shapefile.

How it works

  1. Create an AGSShapefileFeatureTable using the name of the local file, Public_Art.
  2. Create an AGSFeatureLayer using the shapefile feature table.
  3. Add the layer to the map's operationalLayers.

Relevant API

  • AGSFeatureLayer
  • AGSShapefileFeatureTable

Offline data

This sample uses the Public Art Shapefile. It is downloaded from ArcGIS Online automatically.

Tags

layers, shapefile, shp, vector

Sample Code

FeatureLayerShapefileViewController.swift
                                                                  
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
// Copyright 2018 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 ArcGIS

class FeatureLayerShapefileViewController: UIViewController {
    @IBOutlet weak var mapView: AGSMapView!

    var map: AGSMap
    var featureLayer: AGSFeatureLayer

    required init?(coder aDecoder: NSCoder) {
        // Instantiate a map using a basemap.
        map = AGSMap(basemapStyle: .arcGISStreets)

        // Create a shapefile feature table from a named bundle resource.
        let shapefileTable = AGSShapefileFeatureTable(name: "Public_Art")

        // Create a feature layer for the shapefile feature table.
        featureLayer = AGSFeatureLayer(featureTable: shapefileTable)

        // Add the layer to the map.
        map.operationalLayers.add(featureLayer)

        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Display the map in the map view.
        mapView.map = map

        // Zoom the map to the Shapefile's extent.
        zoom(mapView: mapView, to: featureLayer)

        // Add the source code button item to the right of navigation bar.
        (self.navigationItem.rightBarButtonItem as! SourceCodeBarButtonItem).filenames = ["FeatureLayerShapefileViewController"]
    }

    func zoom(mapView: AGSMapView, to featureLayer: AGSFeatureLayer) {
        // Ensure the feature layer's metadata is loaded.
        featureLayer.load { error in
            guard error == nil else {
                print("Couldn't load the shapefile \(error!.localizedDescription)")
                return
            }

            // Once the layer's metadata has loaded, we can read its full extent.
            if let initialExtent = featureLayer.fullExtent {
                mapView.setViewpointGeometry(initialExtent)
            }
        }
    }
}

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