Symbolize shapefile

View on GitHubSample viewer app

Display a shapefile with custom symbology.

Symbolize Shapefile sample

Use case

Feature layers created from shapefiles do not possess any rendering information, and will be assigned with a default symbology. You can apply custom styling to ensure that the content is visible and usable in the context of a specific map. For example, you could use this to visually differentiate between features originating from two different shapefiles, by applying a blue color to one, and a red color to the other.

How to use the sample

Tap the button to apply a new symbology renderer to the feature layer created from the shapefile.

How it works

  1. Create an AGSShapefileFeatureTable, providing the name of the local shapefile.
  2. Create an AGSFeatureLayer from the AGSShapefileFeatureTable and add it to the map's operationalLayers array.
  3. Create an AGSSimpleRenderer to override the default symbology. The simple renderer takes a symbol and applies that to all features in a layer.
  4. Apply the renderer to the AGSFeatureLayer.

Relevant API

  • AGSFeatureLayer
  • AGSShapefileFeatureTable
  • AGSSimpleFillSymbol
  • AGSSimpleLineSymbol
  • AGSSimpleRenderer

Offline data

This sample uses the Aurora Subdivisions Shapefile. It is downloaded from ArcGIS Online automatically.

About the data

This sample displays a shapefile containing subdivisions in Aurora, CO.

Additional information

While shapefiles contain no rendering information, other data sources such as Service Feature Tables or Geodatabase Feature Tables can contain such information. As a result, the rendering properties of the other data sources can be pre-defined by the author.

Tags

package, shape file, shapefile, symbology, visualization

Sample Code

SymbolizeShapefileViewController.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
// Copyright 2017 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 SymbolizeShapefileViewController: UIViewController {
    @IBOutlet weak var mapView: AGSMapView!

    var featureLayer: AGSFeatureLayer?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Instantiate a map using a basemap.
        let map = AGSMap(basemapStyle: .arcGISStreets)

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

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

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

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

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

        // Hold on to the layer to set its symbology later.
        featureLayer = shapefileLayer

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

    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)
            }
        }
    }

    @IBAction func setShapefileSymbol(_ sender: Any) {
        if let layer = featureLayer {
            // Create a new yellow fill symbol with a red outline.
            let outlineSymbol = AGSSimpleLineSymbol(style: .solid, color: .red, width: 1)
            let fillSymbol = AGSSimpleFillSymbol(style: .solid, color: .yellow, outline: outlineSymbol)

            // Create a new renderer using this symbol and set it on the layer.
            layer.renderer = AGSSimpleRenderer(symbol: fillSymbol)
        }
    }
}

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