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!
Solved! Go to Solution.
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
}
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
}
Thanks Richard! I actually figured it out.
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 ());
}
Thanks Wolf! If the spatial reference isn't Web Mercator, will Line 11 change the spatial reference of the dataframe to Web Mercator?
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
After i run my code snippet that Spatial Reference changed to Web Mercator