Apply dictionary renderer to feature layer

View on GitHub

Convert features into graphics to show them with mil2525d symbols.

Image of Apply dictionary renderer to feature layer sample

Use case

A dictionary renderer uses a style file and 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 and load a Geodatabase using a local ".geodatabase" file.
  2. Create and load a DictionarySymbolStyle using a style file.
  3. Create a FeatureLayer for each of the geodatabase's featureTables.
  4. Create a DictionaryRenderer using the dictionary symbol style and assign it to the feature layer's renderer.
  5. Add the feature layers to the map using addOperationalLayers(_:).
  6. Load the feature layers and create a new Envelope from a union of the extents of all layers.
  7. Use the envelope to create a viewpoint and pass it to the map view.

Relevant API

  • DictionaryRenderer
  • DictionarySymbolStyle

Offline data

This sample uses the Joint Military Symbology MIL-STD-2525D style file and the Military Overlay geodatabase. Both are downloaded from ArcGIS Online automatically.

Tags

military, symbol

Sample Code

ApplyDictionaryRendererToFeatureLayerView.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
// Copyright 2024 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 ArcGIS
import SwiftUI

struct ApplyDictionaryRendererToFeatureLayerView: View {
    /// A map with a topographic basemap.
    @State private var map = Map(basemapStyle: .arcGISTopographic)

    /// The viewpoint for zooming the map view to the feature layers.
    @State private var viewpoint: Viewpoint?

    /// The error shown in the error alert.
    @State private var error: Error?

    var body: some View {
        MapView(map: map, viewpoint: viewpoint)
            .task {
                do {
                    // Creates the feature layers and adds them to the map when the sample opens.
                    let featureLayers = try await makeMIL2525DFeatureLayers()
                    map.addOperationalLayers(featureLayers)

                    // Zooms the viewpoint to the feature layers using their extents.
                    let layerExtents = featureLayers.compactMap(\.fullExtent)
                    if let combinedExtents = GeometryEngine.combineExtents(of: layerExtents) {
                        viewpoint = Viewpoint(boundingGeometry: combinedExtents)
                    }
                } catch {
                    self.error = error
                }
            }
            .errorAlert(presentingError: $error)
    }

    /// Creates a list of feature layers with mil2525d symbols.
    /// - Returns: A list of new `FeatureLayer` objects.
    private func makeMIL2525DFeatureLayers() async throws -> [FeatureLayer] {
        // Creates and loads a geodatabase from a local file.
        let geodatabase = Geodatabase(fileURL: .militaryOverlayGeodatabase)
        try await geodatabase.load()

        // Creates and loads a mil2525d dictionary symbol style from a local file.
        let mil2525dDictionarySymbolStyle = DictionarySymbolStyle(url: .mil2525dStyleFile)
        try await mil2525dDictionarySymbolStyle.load()

        // Creates feature layers from the geodatabase's feature tables.
        let featureLayers = geodatabase.featureTables.map { featureTable in
            let featureLayer = FeatureLayer(featureTable: featureTable)

            // Sets the layer's renderer to display the features using mil2525d symbols.
            featureLayer.renderer = DictionaryRenderer(
                dictionarySymbolStyle: mil2525dDictionarySymbolStyle
            )
            featureLayer.minScale = 1000000
            return featureLayer
        }
        await featureLayers.load()

        return featureLayers
    }
}

private extension URL {
    /// The URL to the local "Joint Military Symbology MIL-STD-2525D" mobile style file.
    static var mil2525dStyleFile: URL {
        Bundle.main.url(forResource: "mil2525d", withExtension: "stylx")!
    }

    /// The URL to the local "Military Overlay" geodatabase file.
    static var militaryOverlayGeodatabase: URL {
        Bundle.main.url(
            forResource: "militaryoverlay",
            withExtension: "geodatabase",
            subdirectory: "militaryoverlay.geodatabase"
        )!
    }
}

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