Dictionary renderer with feature layer

View on GitHubSample viewer app

Convert features into graphics to show them with mil2525d symbols.

Dictionary renderer with feature layer

Use case

A dictionary renderer uses a style file along with a rule engine to display advanced symbology. This is useful for displaying features using precise military symbology.

How to use the sample

Pan and zoom around the map. Observe the displayed military symbology on the map.

How it works

  1. Create an AGSGeodatabase.
  2. Create and load an AGSDictionarySymbolStyle using the mil2525d.stylx resource.
  3. Load the geodatabase using AGSGeodatabase.load(completion:).
  4. Once the geodatabase is done loading, create an AGSFeatureLayer from each of the geodatabase's AGSGeodatabaseFeatureTables.
  5. After the last AGSFeatureLayer has loaded, set the AGSMapView's viewpoint to the full extent of all the layers using the method AGSMapViewCommon.setViewpointGeometry(_:completion:).
  6. Add the feature layer to the map's operationalLayers.
  7. Create an AGSDictionaryRenderer and attach it to the feature layer.

Relevant API

  • AGSDictionaryRenderer
  • AGSDictionarySymbolStyle

Offline data

This sample uses the Mil2525d Stylx File and the Military Overlay geodatabase. Both are downloaded from ArcGIS Online automatically.

Tags

military, symbol

Sample Code

FeatureLayerDictionaryRendererViewController.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
// Copyright 2019 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 FeatureLayerDictionaryRendererViewController: UIViewController {
    @IBOutlet var mapView: AGSMapView! {
        didSet {
            // Initialize the map.
            mapView.map = AGSMap(basemapStyle: .arcGISTopographic)

            // Display symbology using a geodatabase.
            displayLayersFromGeodatabase()
        }
    }

    func displayLayersFromGeodatabase() {
        // Load geodatabase from shared resources.
        let geodatabaseURL = Bundle.main.url(forResource: "militaryoverlay", withExtension: ".geodatabase")!
        let generatedGeodatabase = AGSGeodatabase(fileURL: geodatabaseURL)

        // Instantiate and load symbol dictionary from shared resources.
        let styleURL = Bundle.main.url(forResource: "mil2525d", withExtension: "stylx")
        let dictionarySymbol = AGSDictionarySymbolStyle(url: styleURL!)
        dictionarySymbol.load { [weak self] (error: Error?) in
            if let error = error {
                self?.presentAlert(error: error)
            } else {
                return
            }
        }

        // Once geodatabase is done loading, create feature layers from each feature table.
        generatedGeodatabase.load { [weak self] (error: Error?) in
            guard let self = self else { return }
            if let error = error {
                self.presentAlert(error: error)
            } else {
                for featureTable in generatedGeodatabase.geodatabaseFeatureTables {
                    // Create a feature layer from the feature table.
                    let featureLayer = AGSFeatureLayer(featureTable: featureTable)
                    // Set the minimum visibility scale.
                    featureLayer.minScale = 1000000
                    featureLayer.load { [weak self] (error: Error?) in
                        guard let self = self else { return }
                        if let error = error {
                            self.presentAlert(error: error)
                        } else {
                            // Set the viewpoint to the full extent of all layers.
                            self.mapView.setViewpointGeometry(featureLayer.fullExtent!)
                        }
                    }
                    // Add each layer to the map.
                    self.mapView.map?.operationalLayers.add(featureLayer)
                    // Display features from the layer using mil2525d symbols.
                    featureLayer.renderer = AGSDictionaryRenderer(dictionarySymbolStyle: dictionarySymbol)
                }
            }
        }
    }

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

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