Select to view content in your preferred language

How can you get the spatial reference of a map dataframe?

899
5
Jump to solution
04-12-2023 02:37 PM
tzz_12
by
New Contributor III

I was wondering how can I get the spatial reference of the map dataframe? I tried to find the answer searching the online resources with no luck. I appreciate the help. Is it possible to change the spatial reference of the dataframe using C#/ ArcGIS Pro SDK? 

I want to find out the current spatial reference of the dataframe to create a feature class with an appropriate spatial reference for accurate calculation. If the dataframe does not meet the criteria (e.g. not a projected spatial reference), I want to change it to a appropriate spatial reference. 

Thanks for your help!

0 Kudos
1 Solution

Accepted Solutions
RichardDaniels
Regular Contributor

This is actually one of the simpler properties you can get from the map since you do not have to be async or queuedTask.Run.  See Map Class—ArcGIS Pro

 

var myMap = MapView.Active;
SpatialReference mySpatialReference = mapView.Map.SpatialReference;
if (mySpatialReference.GcsWkid == 3857) //if web Mercator then
{
//do some work
}

 

View solution in original post

0 Kudos
5 Replies
RichardDaniels
Regular Contributor

This is actually one of the simpler properties you can get from the map since you do not have to be async or queuedTask.Run.  See Map Class—ArcGIS Pro

 

var myMap = MapView.Active;
SpatialReference mySpatialReference = mapView.Map.SpatialReference;
if (mySpatialReference.GcsWkid == 3857) //if web Mercator then
{
//do some work
}

 

0 Kudos
tzz_12
by
New Contributor III

Thanks Richard! I actually figured it out. 

 

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

In order to change the Spatial Reference for the Map you have to use the SetSpatialReference of the Map class: SetSpatialReference Method (Map)—ArcGIS Pro.  

For this you have to use QueuedTask.Run:

try
{
  // Get the active map view.
  var mapView = MapView.Active;
  if (mapView == null) return;
  // If Spatial Reference is already have Web Mecartor do nothing
  if (mapView.Map.SpatialReference.Equals(SpatialReferences.WebMercator)) return;
  _= QueuedTask.Run(() =>
  {
    // Run within QueuedTask
    mapView.Map.SetSpatialReference(SpatialReferences.WebMercator);
  });
}
catch (Exception ex)
{
  MessageBox.Show (ex.ToString ());
}
0 Kudos
tzz_12
by
New Contributor III

Thanks Wolf! If the spatial reference isn't Web Mercator, will Line 11 change the spatial reference of the dataframe to Web Mercator? 

 

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

I am not sure what you mean with dataframe, but this is what my code snippet does:

Before running the code snippet, my Map is using the NAD 1983 spatial reference

Wolf_0-1681409075042.png

After i run my code snippet that Spatial Reference changed to Web Mercator

Wolf_1-1681409119702.png

 

0 Kudos