Add ENC exchange set

View on GitHub

Display nautical charts per the ENC specification.

Image showing the add ENC exchange set app

Use case

The ENC specification describes how hydrographic data should be displayed digitally.

An ENC exchange set is a catalog of data files which can be loaded as cells. The cells contain information on how symbols should be displayed in relation to one another, so as to represent information such as depth and obstacles accurately.

How to use the sample

Run the sample and view the ENC data. Pan and zoom around the map. Take note of the high level of detail in the data and the smooth rendering of the layer.

How it works

  1. Specify the path to a local CATALOG.031 file to create an ENCExchangeSet.
  2. After loading the exchange set, get the ENCDataset objects asynchronously.
  3. Create an ENCCell for each dataset. Then create an ENCLayer for each cell.
  4. Add the ENC layer to a map's operational layers collection to display it.

Relevant API

  • ENCCell
  • ENCDataset
  • ENCExchangeSet
  • ENCLayer

Offline data

This sample downloads the ENC Exchange Set without updates item from ArcGIS Online.

The latest hydrography package can be downloaded from Esri Developer website. The S57DataDictionary.xml file is contained in it along with many others but a user does not need to know that in order to render ENC data.

Tags

data, ENC, hydrographic, layers, maritime, nautical chart

Sample Code

AddENCExchangeSetView.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 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 AddENCExchangeSetView: View {
    /// The tracking status for the loading operation.
    @State private var isLoading = false

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

    /// The data model for the sample.
    @StateObject private var model = Model()

    var body: some View {
        MapViewReader { mapProxy in
            MapView(map: model.map)
                .overlay(alignment: .center) {
                    if isLoading {
                        ProgressView("Loading…")
                            .padding()
                            .background(.ultraThickMaterial)
                            .cornerRadius(10)
                            .shadow(radius: 50)
                    }
                }
                .task {
                    do {
                        isLoading = true
                        try await model.addENCExchangeSet()
                        if let extent = model.completeExtent {
                            await mapProxy.setViewpoint(
                                Viewpoint(center: extent.center, scale: 67000)
                            )
                        }
                        isLoading = false
                    } catch {
                        self.error = error
                    }
                }
        }
        .errorAlert(presentingError: $error)
    }
}

private extension AddENCExchangeSetView {
    @MainActor
    class Model: ObservableObject {
        /// A map with Oceans style.
        let map: Map = {
            let map = Map(basemapStyle: .arcGISOceans)
            map.initialViewpoint = Viewpoint(
                latitude: -32.5,
                longitude: 60.95,
                scale: 67000
            )
            return map
        }()

        /// The geometry that represents a rectangular shape that encompasses the area on the
        /// map where the ENC data will be rendering.
        private(set) var completeExtent: Envelope?

        /// A URL to the temporary SENC data directory.
        private let temporaryURL: URL = {
            let directoryURL = FileManager.default.temporaryDirectory.appendingPathComponent(
                ProcessInfo().globallyUniqueString
            )
            // Create and return the full, unique URL to the temporary folder.
            try? FileManager.default.createDirectory(
                at: directoryURL,
                withIntermediateDirectories: true
            )
            return directoryURL
        }()

        deinit {
            // Recursively remove all files in the sample-specific
            // temporary folder and the folder itself.
            try? FileManager.default.removeItem(at: temporaryURL)
            // Reset ENC environment display settings.
            let displaySettings = ENCEnvironmentSettings.shared.displaySettings
            displaySettings.textGroupVisibilitySettings.resetToDefaults()
            displaySettings.viewingGroupSettings.resetToDefaults()
        }

        /// Gets the ENC exchange set data and loads it and sets the display settings.
        func addENCExchangeSet() async throws {
            let exchangeSet = ENCExchangeSet(fileURLs: [.exchangeSet])
            // URL to the "hydrography" data folder that contains the "S57DataDictionary.xml" file.
            let resourceURL = URL.hydrographyDirectory.deletingLastPathComponent()
            // Set environment settings for loading the dataset.
            let environmentSettings = ENCEnvironmentSettings.shared
            environmentSettings.resourceURL = resourceURL
            // The SENC data directory is for temporarily storing generated files.
            environmentSettings.sencDataURL = temporaryURL
            updateDisplaySettings()
            try await exchangeSet.load()
            try await renderENCData(datasets: exchangeSet.datasets)
        }

        /// Maps the exchange set data to ENC layer and ENC cells and loads the layers.
        /// - Parameter datasets: The ENC datasets previously loaded.
        private func renderENCData(datasets: [ENCDataset]) async throws {
            let encLayers = datasets.map {
                ENCLayer(
                    cell: ENCCell(
                        dataset: $0
                    )
                )
            }
            map.addOperationalLayers(encLayers)
            await encLayers.load()
            let extents = map.operationalLayers.compactMap(\.fullExtent)
            completeExtent = GeometryEngine.combineExtents(of: extents)
        }

        /// Updates the display settings to make the chart less cluttered.
        private func updateDisplaySettings() {
            let displaySettings = ENCEnvironmentSettings.shared.displaySettings
            let textGroupVisibilitySettings = displaySettings.textGroupVisibilitySettings
            textGroupVisibilitySettings.includesGeographicNames = false
            textGroupVisibilitySettings.includesNatureOfSeabed = false
            let viewingGroupSettings = displaySettings.viewingGroupSettings
            viewingGroupSettings.includesBuoysBeaconsAidsToNavigation = false
            viewingGroupSettings.includesDepthContours = false
            viewingGroupSettings.includesSpotSoundings = false
        }
    }
}

private extension URL {
    static let exchangeSet = Bundle.main.url(
        forResource: "CATALOG",
        withExtension: "031",
        subdirectory: "ExchangeSetwithoutUpdates/ExchangeSetwithoutUpdates/ENC_ROOT"
    )!

    static let hydrographyDirectory = Bundle.main.url(
        forResource: "S57DataDictionary",
        withExtension: "xml",
        subdirectory: "hydrography"
    )!
}

#Preview {
    AddENCExchangeSetView()
}

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