Get a list of suitable transformations for projecting a geometry between two spatial references with different horizontal datums.
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
- 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. - 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
// 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()
}