Display dimensions

View on GitHub

Display dimension features from a mobile map package.

Image of display dimensions

Use case

Dimensions show specific lengths or distances on a map. A dimension may indicate the length of a land parcel, or the distance between two features, such as a fire hydrant and the corner of a building.

How to use the sample

When the sample loads, it will automatically display the map containing dimension features from the mobile map package. Control the visibility of the dimension layer and apply a definition expression to show dimensions greater than or equal to 450m in length using the toggles.

Note: The minimum scale range of the sample is set to 1:40,000 to maintain readability of the dimension features.

How it works

  1. Create a MobileMapPackage specifying the path to the .mmpk file.
  2. Load the mobile map package (mmpk).
  3. After the mmpk successfully loads, get the map from the mmpk and add it to the map view.
  4. Loop through the map's layers to find the DimensionLayer.
  5. Control the dimension layer's visibility with the isVisible property and set a definition expression using the definitionExpression property.

Relevant API

  • DimensionLayer
  • MobileMapPackage

About the data

This sample shows a subset of the network of pylons, substations, and power lines around Edinburgh, Scotland within a Edinburgh Pylon Dimensions mobile map package. The data was digitized from satellite imagery and is intended to be used for illustrative purposes only.

Additional information

Dimension layers can be taken offline from a feature service hosted on ArcGIS Enterprise 10.9 or later, using the GeodatabaseSyncTask. Dimension layers are also supported in mobile map packages or mobile geodatabases created in ArcGIS Pro 2.9 or later.

Tags

definition expression, dimension, distance, layer, length, mmpk, mobile map package, utility

Sample Code

DisplayDimensionsView.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
// Copyright 2023 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 DisplayDimensionsView: View {
    /// A map with no specified style.
    @State private var map = Map()

    /// The dimensional layer added to the map.
    @State private var dimensionLayer: DimensionLayer?

    /// A Boolean value indicating whether the dimension layer's content is visible.
    @State private var dimensionLayerIsVisible = true

    /// A Boolean value indicating whether the dimension layer's definition expression is set.
    @State private var definitionExpressionIsSet = false

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

    var body: some View {
        MapView(map: map)
            .task {
                do {
                    try await loadDimensionLayer()
                } catch {
                    self.error = error
                }
            }
            .overlay {
                VStack {
                    Spacer()
                    Group {
                        Toggle("Dimension Layer", isOn: $dimensionLayerIsVisible)
                            .onChange(of: dimensionLayerIsVisible) { newValue in
                                dimensionLayer?.isVisible = newValue
                            }

                        Toggle(
                            "Definition Expression:\nDimensions >= 450m",
                            isOn: $definitionExpressionIsSet
                        )
                        .onChange(of: definitionExpressionIsSet) { newValue in
                            dimensionLayer?.definitionExpression = newValue ? "DIMLENGTH >= 450" : ""
                        }
                    }
                    .padding(8)
                    .background(.ultraThinMaterial)
                    .cornerRadius(10)
                    .disabled(dimensionLayer == nil)
                }
                .padding()
                .padding(.bottom)
            }
            .errorAlert(presentingError: $error)
    }
}

private extension DisplayDimensionsView {
    /// Loads a map with a dimension layer from a local mobile map package.
    private func loadDimensionLayer() async throws {
        // Load the local mobile map package using a URL.
        let mapPackage = MobileMapPackage(fileURL: .edinburghPylonDimensions)
        try await mapPackage.load()

        // Set the map to the first map in the mobile map package.
        if let map = mapPackage.maps.first {
            // Set the minScale to maintain dimension readability.
            map.minScale = 4e4
            self.map = map
        } else {
            throw SetupError.noMap
        }

        // Set the dimension layer using the one on the map.
        if let layer = map.operationalLayers.first(where: { $0 is DimensionLayer }) as? DimensionLayer {
            dimensionLayer = layer
        } else {
            throw SetupError.noDimensionLayer
        }
    }

    /// The errors for the sample that can be thrown during setup.
    private enum SetupError: String, LocalizedError {
        case noMap = "The MMPK does not contain a map."
        case noDimensionLayer = "The map does not contain a dimension layer."

        /// The text description of the error.
        var errorDescription: String? {
            NSLocalizedString(
                self.rawValue,
                comment: "Error thrown when the setup for the sample fails."
            )
        }
    }
}

private extension URL {
    /// The URL to the local Edinburgh Pylon Dimensions mobile map package file.
    static var edinburghPylonDimensions: URL {
        Bundle.main.url(forResource: "Edinburgh_Pylon_Dimensions", withExtension: "mmpk")!
    }
}

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