Project

View inAndroidFormsUWPWPFWinUIiOSView on GitHub

Project a point from one spatial reference to another.

Image of project

Use case

Being able to project between spatial references is fundamental to a GIS. An example of when you would need to re-project data is if you had data in two different spatial references, but wanted to perform an intersect analysis with the GeometryEngine.intersect function. This function takes two geometries as parameters, and both geometries must be in the same spatial reference. If they are not, you could first use GeometryEngine.project to convert the geometries so they match.

How to use the sample

Tap anywhere on the map. A callout will display the clicked location's coordinate in the original (basemap's) spatial reference and in the projected spatial reference.

How it works

  1. Call the static method, GeometryEngine.Project, passing in the original Geometry and a SpatialReference to which it should be projected.

Relevant API

  • GeometryEngine
  • Point
  • SpatialReference

Additional information

In cases where the the output spatial reference uses a different geographic coordinate system than that of the input spatial reference, see the GeometryEngine.project method that additionally takes in a DatumTransformation parameter.

Tags

coordinate system, coordinates, latitude, longitude, projected, projection, spatial reference, Web Mercator, WGS 84

Sample Code

Project.cs
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
// Copyright 2019 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 ArcGISRuntime;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using Esri.ArcGISRuntime.UI.Controls;
using Foundation;
using System.Drawing;
using UIKit;

namespace ArcGISRuntimeXamarin.Samples.Project
{
    [Register("Project")]
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Project",
        category: "Geometry",
        description: "Project a point from one spatial reference to another.",
        instructions: "Tap anywhere on the map. A callout will display the clicked location's coordinate in the original (basemap's) spatial reference and in the projected spatial reference.",
        tags: new[] { "WGS 84", "Web Mercator", "coordinate system", "coordinates", "latitude", "longitude", "projected", "projection", "spatial reference" })]
    public class Project : UIViewController
    {
        // Hold references to UI controls.
        private MapView _myMapView;

        public Project()
        {
            Title = "Project";
        }

        private async void Initialize()
        {
            // Show a map in the default WebMercator spatial reference.
            _myMapView.Map = new Map(BasemapStyle.ArcGISTopographic);

            // Add a graphics overlay for showing the tapped point.
            GraphicsOverlay overlay = new GraphicsOverlay();
            SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, Color.Red, 5);
            overlay.Renderer = new SimpleRenderer(markerSymbol);
            _myMapView.GraphicsOverlays.Add(overlay);

            // Zoom to Minneapolis.
            Envelope startingEnvelope = new Envelope(-10995912.335747, 5267868.874421, -9880363.974046, 5960699.183877,
                SpatialReferences.WebMercator);
            await _myMapView.SetViewpointGeometryAsync(startingEnvelope);
        }

        private void MapView_Tapped(object sender, GeoViewInputEventArgs e)
        {
            // Get the tapped point - this is in the map's spatial reference,
            // which in this case is WebMercator because that is the SR used by the included basemaps.
            MapPoint tappedPoint = e.Location;

            // Update the graphics.
            _myMapView.GraphicsOverlays[0].Graphics.Clear();
            _myMapView.GraphicsOverlays[0].Graphics.Add(new Graphic(tappedPoint));

            // Project the point to WGS84
            MapPoint projectedPoint = (MapPoint) GeometryEngine.Project(tappedPoint, SpatialReferences.Wgs84);

            // Format the results in strings.
            string originalCoords = $"Original: {tappedPoint.X:F4}, {tappedPoint.Y:F4}";
            string projectedCoords = $"Projected: {projectedPoint.X:F4}, {projectedPoint.Y:F4}";

            // Define a callout and show it in the map view.
            CalloutDefinition calloutDef = new CalloutDefinition(projectedCoords, originalCoords);
            _myMapView.ShowCalloutAt(tappedPoint, calloutDef);
        }

        public override void LoadView()
        {
            // Create the views.
            View = new UIView() { BackgroundColor = ApplicationTheme.BackgroundColor };
            _myMapView = new MapView();

            _myMapView.TranslatesAutoresizingMaskIntoConstraints = false;

            // Add the views.
            View.AddSubviews(_myMapView);

            // Lay out the views.
            NSLayoutConstraint.ActivateConstraints(new[]
            {
                _myMapView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
                _myMapView.BottomAnchor.ConstraintEqualTo(View.BottomAnchor),
                _myMapView.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
                _myMapView.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor)
            });
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            Initialize();
        }

        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            // Subscribe to events.
            _myMapView.GeoViewTapped += MapView_Tapped;
        }

        public override void ViewDidDisappear(bool animated)
        {
            base.ViewDidDisappear(animated);

            // Unsubscribe from events, per best practice.
            _myMapView.GeoViewTapped -= MapView_Tapped;
        }
    }
}

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