List transformations by suitability

View inMAUIUWPWPFWinUIView on GitHub

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

Image of list transformations by suitability

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 red cross; you can visually compare the original blue point with the projected red cross.

Select 'Consider current extent' to limit the transformations that are appropriate for the current extent.

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

How it works

  1. Pass the input and output spatial references to TransformationCatalog.GetTransformationsBySuitability 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
  • 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 a GeographicTransformation, which extends the DatumTransformation class. The ArcGIS Maps SDK for .NET also includes a HorizontalVerticalTransformation, which also extends 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.

To download projection engine data to your device:

  1. Log in to the ArcGIS for Developers site using your Developer account.
  2. In the Dashboard page, tap 'Download APIs and SDKs'.
  3. Tap the download button next to Projection Engine Data to download projection engine data to your computer.
  4. Unzip the downloaded data on your computer.
  5. Create an ~/ArcGIS/Runtime/Data/PEDataRuntime directory on your device and copy the files to this directory.

Tags

datum, geodesy, projection, spatial reference, transformation

Sample Code

ListTransformations.xaml.csListTransformations.xaml.csListTransformations.xaml
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2022 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: http://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.

using Esri.ArcGISRuntime;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using System.Collections.ObjectModel;

namespace ArcGIS.Samples.ListTransformations
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "List transformations by suitability",
        category: "Geometry",
        description: "Get a list of suitable transformations for projecting a geometry between two spatial references with different horizontal datums.",
        instructions: "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 red cross; you can visually compare the original blue point with the projected red cross.",
        tags: new[] { "datum", "geodesy", "projection", "spatial reference", "transformation" })]
    public partial class ListTransformations : ContentPage
    {
        // Point whose coordinates will be projected using a selected transform.
        private MapPoint _originalPoint;

        // Graphic representing the projected point.
        private Graphic _projectedPointGraphic;

        // GraphicsOverlay to hold the point graphics.
        private GraphicsOverlay _pointsOverlay;

        public ObservableCollection<DatumTransformationListBoxItem> SuitableTransformationsList { get; } = new ObservableCollection<DatumTransformationListBoxItem>();

        public ListTransformations()
        {
            InitializeComponent();

            _ = Initialize();
        }

        private async Task Initialize()
        {
            // Create the map.
            Map myMap = new Map(BasemapStyle.ArcGISImagery);

            // Create a point in the Greenwich observatory courtyard in London, UK, the location of the prime meridian.
            _originalPoint = new MapPoint(538985.355, 177329.516, SpatialReference.Create(27700));

            // Set the initial extent to an extent centered on the point.
            Viewpoint initialViewpoint = new Viewpoint(_originalPoint, 5000);
            myMap.InitialViewpoint = initialViewpoint;

            // Add the map to the map view.
            MyMapView.Map = myMap;

            // Create a graphics overlay to hold the original and projected points.
            _pointsOverlay = new GraphicsOverlay();
            MyMapView.GraphicsOverlays.Add(_pointsOverlay);

            // Add the point as a graphic with a blue square.
            SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Square, System.Drawing.Color.Blue, 15);
            Graphic originalGraphic = new Graphic(_originalPoint, markerSymbol);
            _pointsOverlay.Graphics.Add(originalGraphic);

            // Get the path to the projection engine data (if it exists).
            string peFolderPath = GetProjectionDataPath();
            if (!String.IsNullOrEmpty(peFolderPath))
            {
                TransformationCatalog.ProjectionEngineDirectory = peFolderPath;
                MessagesTextBox.Text = "Using projection data found at '" + peFolderPath + "'";
            }
            else
            {
                MessagesTextBox.Text = "Projection engine data not found.";
            }

            try
            {
                // Wait for the map to load so that it has a spatial reference.
                await myMap.LoadAsync();

                // Show the input and output spatial reference.
                InSpatialRefTextBox.Text = "In WKID = " + _originalPoint.SpatialReference.Wkid;
                OutSpatialRefTextBox.Text = "Out WKID = " + myMap.SpatialReference.Wkid;

                // Set up the UI.
                TransformationsListBox.ItemsSource = SuitableTransformationsList;

                // Create a list of transformations to fill the UI list box.
                GetSuitableTransformations(_originalPoint.SpatialReference, myMap.SpatialReference, UseExtentSwitch.IsToggled);
            }
            catch (Exception e)
            {
                await Application.Current.MainPage.DisplayAlert("Error", e.ToString(), "OK");
            }
        }

        // Function to get suitable datum transformations for the specified input and output spatial references.
        private void GetSuitableTransformations(SpatialReference inSpatialRef, SpatialReference outSpatialRef, bool considerExtent)
        {
            // Get suitable transformations. Use the current extent to evaluate suitability, if requested.
            IReadOnlyList<DatumTransformation> transformations;
            if (considerExtent)
            {
                Envelope currentExtent = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry).TargetGeometry as Envelope;
                transformations = TransformationCatalog.GetTransformationsBySuitability(inSpatialRef, outSpatialRef, currentExtent);
            }
            else
            {
                transformations = TransformationCatalog.GetTransformationsBySuitability(inSpatialRef, outSpatialRef);
            }

            // Get the default transformation for the specified input and output spatial reference.
            DatumTransformation defaultTransform = TransformationCatalog.GetTransformation(inSpatialRef, outSpatialRef);

            // Reset list.
            SuitableTransformationsList.Clear();

            // Wrap the transformations in a class that includes a boolean to indicate if it's the default transformation.
            foreach (DatumTransformation transform in transformations)
            {
                DatumTransformationListBoxItem item = new DatumTransformationListBoxItem(transform)
                {
                    IsDefault = (transform.Name == defaultTransform.Name)
                };
                SuitableTransformationsList.Add(item);
            }
        }

        void TransformationsListBox_SelectionChanged(System.Object sender, Microsoft.Maui.Controls.SelectionChangedEventArgs e)
        {
            // Get the selected transform from the list box. Return if there isn't a selected item.
            DatumTransformationListBoxItem selectedListBoxItem = TransformationsListBox.SelectedItem as DatumTransformationListBoxItem;
            if (selectedListBoxItem == null) { return; }

            // Get the datum transformation object from the list box item.
            DatumTransformation selectedTransform = selectedListBoxItem.TransformationObject;

            try
            {
                // Project the original point using the selected transform.
                MapPoint projectedPoint = (MapPoint)_originalPoint.Project(MyMapView.SpatialReference, selectedTransform);

                // Update the projected graphic (if it already exists), create it otherwise.
                if (_projectedPointGraphic != null)
                {
                    _projectedPointGraphic.Geometry = projectedPoint;
                }
                else
                {
                    // Create a symbol to represent the projected point (a cross to ensure both markers are visible).
                    SimpleMarkerSymbol projectedPointMarker = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Cross, System.Drawing.Color.Red, 15);

                    // Create the point graphic and add it to the overlay.
                    _projectedPointGraphic = new Graphic(projectedPoint, projectedPointMarker);
                    _pointsOverlay.Graphics.Add(_projectedPointGraphic);
                }

                MessagesTextBox.Text = "Projected point using transform: " + selectedTransform.Name;
            }
            catch (ArcGISRuntimeException ex)
            {
                // Exception if a transformation is missing grid files.
                MessagesTextBox.Text = "Error using selected transformation: " + ex.Message;

                // Remove the projected point graphic (if it exists).
                if (_projectedPointGraphic != null && _pointsOverlay.Graphics.Contains(_projectedPointGraphic))
                {
                    _pointsOverlay.Graphics.Remove(_projectedPointGraphic);
                    _projectedPointGraphic = null;
                }
            }
        }

        private void UseExtentSwitch_Toggled(object sender, ToggledEventArgs e)
        {
            // Recreate the contents of the datum transformations list box.
            GetSuitableTransformations(_originalPoint.SpatialReference, MyMapView.Map.SpatialReference, UseExtentSwitch.IsToggled);
        }

        private string GetProjectionDataPath()
        {
            // Return the projection data path; note that this is not valid by default.
            //You must manually download the projection engine data and update the path returned here.
            return "";
        }
    }

    // A class that wraps a DatumTransformation object and adds a property that indicates if it's the default transformation.
    public class DatumTransformationListBoxItem
    {
        // Datum transformation object.
        public DatumTransformation TransformationObject { get; set; }

        // Whether or not this transformation is the default (for the specified in/out spatial reference).
        public bool IsDefault { get; set; }

        // Constructor that takes the DatumTransformation object to wrap.
        public DatumTransformationListBoxItem(DatumTransformation transformation)
        {
            TransformationObject = transformation;
        }
    }

    // Class to select the appropriate data template for datum transformation list items.
    public class TransformRowTemplateSelector : DataTemplateSelector
    {
        // Data templates for three types of datum transformations.
        // - Those without supporting projection engine data (making the transformation unavailable).
        private readonly DataTemplate _unavailableTransformTemplate;

        // - Available transformations (data required is either available by default, or has been stored on the device).
        private readonly DataTemplate _availableTransformTemplate;

        // - The default datum transformation for the context (input/output spatial reference, and possibly the extent).
        private readonly DataTemplate _defaultTransformTemplate;

        public TransformRowTemplateSelector()
        {
            // Create the data template for unavailable transformations.
            _unavailableTransformTemplate = new DataTemplate(() =>
            {
                Label transformNameLabel = new Label { Padding = new Thickness(4,4,4,0) };
                transformNameLabel.SetAppThemeColor(Label.TextColorProperty, Color.FromArgb("#6A6A6A"), Color.FromArgb("#9F9F9F"));
                transformNameLabel.SetBinding(Label.TextProperty, "TransformationObject.Name");

                return transformNameLabel;
            });

            // Create the data template for available (but non-default) transformations.
            _availableTransformTemplate = new DataTemplate(() =>
            {
                Label transformNameLabel = new Label { Padding = new Thickness(4,4,4,0) };
                transformNameLabel.SetAppThemeColor(Label.TextColorProperty, Color.FromArgb("#151515"), Colors.White);
                transformNameLabel.SetBinding(Label.TextProperty, "TransformationObject.Name");

                return transformNameLabel;
            });

            // Create the data template for the default transformation.
            _defaultTransformTemplate = new DataTemplate(() =>
            {
                Label transformNameLabel = new Label
                {
                    // Show these with bold blue text.
                    FontAttributes = FontAttributes.Bold,
                    Padding = new Thickness(4,4,4,0)
                };
                transformNameLabel.SetAppThemeColor(Label.TextColorProperty, Color.FromArgb("#00619B"), Color.FromArgb("#00A0FF"));
                transformNameLabel.SetBinding(Label.TextProperty, "TransformationObject.Name");

                return transformNameLabel;
            });
        }

        // Logic that is called when a template is needed for a list view item.
        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            DataTemplate selectedTemplate = null;

            // Get the current list item being created. Return if the item is null.
            DatumTransformationListBoxItem transformationItem = item as DatumTransformationListBoxItem;
            if (transformationItem == null)
            {
                return null;
            }

            // Read the IsMissingProjectionEngineFiles property to select the available or unavailable data template.
            if (transformationItem.TransformationObject.IsMissingProjectionEngineFiles)
            {
                selectedTemplate = _unavailableTransformTemplate;
            }
            else if (!transformationItem.TransformationObject.IsMissingProjectionEngineFiles)
            {
                selectedTemplate = _availableTransformTemplate;
            }

            // See if this is the default transformation.
            if (transformationItem.IsDefault)
            {
                selectedTemplate = _defaultTransformTemplate;
            }

            // Return the selected template.
            return selectedTemplate;
        }
    }
}

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