List spatial reference transformations

View on GitHub

Get a list of suitable transformations for projecting a geometry between two spatial references with different horizontal datums.

Image of List spatial reference transformations

Use case

Transformations (sometimes known as datum or geographic transformations) are used when projecting data from one spatial reference to another when there is a difference in the underlying datum of the spatial references. Transformations can be mathematically defined by specific equations (equation-based transformations) or may rely on external supporting files (grid-based transformations). Choosing the most appropriate transformation for a situation can ensure the best possible accuracy for this operation. Some users familiar with transformations may wish to control which transformation is used in an operation.

How to use the sample

Select a transformation from the list to see the result of projecting the point from EPSG:27700 to EPSG:3857 using that transformation. The result is shown as a blue cross; you can visually compare the original red point with the projected blue cross.

Select "Suitable for Map Extent" to limit the transformations to those that are appropriate for the current extent.

If the selected transformation is not usable (has missing grid files), then an error is displayed.

To download projection engine data, tap "Download Data" and then the download button next to the latest release of the Projection Engine Data. Unzip the download data in Files, and then tap "Set Data Directory" in the sample. Navigate into the unzipped Projection Engine Data directory and tap "Open".

How it works

  1. Pass the input and output spatial references to TransformationCatalog.transformations(from:to:areaOfInterest:ignoreVertical:) for transformations based on the map's spatial reference OR additionally provide an extent argument to only return transformations suitable to the extent. This returns a list of ranked transformations.
  2. Use one of the DatumTransformation objects returned to project the input geometry to the output spatial reference.

Relevant API

  • DatumTransformation
  • GeographicTransformation
  • GeographicTransformationStep
  • GeometryEngine
  • GeometryEngine.project(_:into:datumTransformation:)
  • TransformationCatalog

About the data

The map starts out zoomed into the grounds of the Royal Observatory, Greenwich. The initial point is in the British National Grid spatial reference, which was created by the United Kingdom Ordnance Survey. The spatial reference after projection is in Web Mercator.

Additional information

Some transformations aren't available until transformation data is provided.

This sample uses GeographicTransformation, a subclass of DatumTransformation. The ArcGIS Maps SDKs also include HorizontalVerticalTransformation, another subclass of DatumTransformation. The HorizontalVerticalTransformation class is used to transform coordinates of z-aware geometries between spatial references that have different geographic and/or vertical coordinate systems.

This sample can be used with or without provisioning projection engine data to your device. If you do not provision data, a limited number of transformations will be available.

Tags

datum, geodesy, projection, spatial reference, transformation

Sample Code

ListSpatialReferenceTransformationsView.swiftListSpatialReferenceTransformationsView.swiftListSpatialReferenceTransformationsView.Model.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
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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 ListSpatialReferenceTransformationsView: View {
    /// The view model for the sample.
    @StateObject private var model = Model()

    /// The visible area of the map view.
    @State private var visibleArea: ArcGIS.Polygon?

    /// A Boolean value indicating whether the transformations list should be filtered using the current map extent.
    @State private var filterByMapExtent = false

    /// A Boolean value indicating whether the file importer interface is presented.
    @State private var fileImporterIsPresented = false

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

    var body: some View {
        VStack(spacing: 0) {
            MapView(map: model.map, graphicsOverlays: [model.graphicsOverlay])
                .onVisibleAreaChanged { visibleArea = $0 }
                .task {
                    // Set the transformations list once the map's spatial reference has loaded.
                    do {
                        try await model.map.load()
                        model.updateTransformationsList()
                    } catch {
                        self.error = error
                    }
                }
                .errorAlert(presentingError: $error)

            ZStack {
                Text("Transformations")
                    .bold()
                HStack {
                    Spacer()
                    transformationsMenu
                        .labelStyle(.iconOnly)
                }
                .padding()
            }
            .background(Color(.systemGroupedBackground))

            TransformationsList(model: model)
        }
    }

    /// A menu containing actions relating to the list of transformations.
    private var transformationsMenu: some View {
        Menu("Transformations", systemImage: "ellipsis") {
            Picker("Filter Transformations", selection: $filterByMapExtent) {
                Label("All Transformations", systemImage: "square.grid.2x2")
                    .tag(false)
                Label("Suitable for Extent", systemImage: "line.3.horizontal.decrease.circle")
                    .tag(true)
            }
            .pickerStyle(.inline)
            .onChange(of: filterByMapExtent) { newValue in
                model.updateTransformationsList(withExtent: newValue ? visibleArea?.extent : nil)
            }

            Link(destination: .projectionEngineDataDownloads) {
                Label("Download Data", systemImage: "arrow.down.circle")
            }

            Button("Set Data Directory", systemImage: "folder") {
                fileImporterIsPresented = true
            }
        }
        .fileImporter(
            isPresented: $fileImporterIsPresented,
            allowedContentTypes: [.folder]
        ) { result in
            do {
                switch result {
                case .success(let fileURL):
                    try model.setProjectionEngineDataURL(fileURL)
                case .failure(let error):
                    throw error
                }
            } catch {
                self.error = error
            }
        }
    }
}

private extension ListSpatialReferenceTransformationsView {
    struct TransformationsList: View {
        /// The view model for the sample.
        @ObservedObject var model: Model

        /// The missing Projection Engine filenames for the tapped transformation.
        @State private var missingFilenames: [String] = []

        var body: some View {
            List(model.transformations, id: \.self) { transformation in
                Button {
                    if transformation.isMissingProjectionEngineFiles {
                        missingFilenames = model.missingProjectionEngineFilenames(
                            for: transformation
                        )
                    } else {
                        model.selectTransformation(transformation)
                    }
                } label: {
                    VStack(alignment: .leading) {
                        HStack {
                            Text(transformation.name.replacingOccurrences(of: "_", with: " "))
                            Spacer()
                            if transformation == model.selectedTransformation {
                                Image(systemName: "checkmark")
                                    .foregroundStyle(Color.accentColor)
                            }
                        }

                        if transformation.isMissingProjectionEngineFiles {
                            Text("Missing Grid Files")
                                .font(.caption)
                                .opacity(0.75)
                        }
                    }
                    .contentShape(Rectangle())
                }
                .buttonStyle(.plain)
            }
            .alert(
                "Missing Grid Files:",
                isPresented: Binding(
                    get: { !missingFilenames.isEmpty },
                    set: { _ in missingFilenames = [] }),
                presenting: missingFilenames,
                actions: { _ in },
                message: { filenames in
                    let message = """
                    \(filenames.joined(separator: ",\n"))

                    See the README file for instructions on adding Projection Engine data to the app.
                    """

                    Text(message)
                }
            )
        }
    }
}

private extension URL {
    /// A URL to download the Projection Engine Data.
    static var projectionEngineDataDownloads: URL {
        URL(string: "https://developers.arcgis.com/swift/downloads/#projection-engine-data")!
    }
}

#Preview {
    ListSpatialReferenceTransformationsView()
}

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