I'm trying to convert some old VB code into C#, and unsure of how to proceed with code that does that task mentioned in the subject. Below is the old vb code. My problem being that I do not know the modern .net sdk equivalents for the calls in C#. It seems like it should be pretty simple to get the view extent and calculate the center, but apparently, it's a bit more involved then I expected. 🙂
Try 'TRY TO GET THE CENTER SCREEN LOCATION FROM THE ACTIVE VIEW, JUST SHOW THE FORM ON ERROR
Dim pPoint As ESRI.ArcGIS.Geometry.IPoint
pPoint = New ESRI.ArcGIS.Geometry.Point
pPoint.X = (m_pArcMapAV.Extent.XMax - m_pArcMapAV.Extent.XMin) / 2 + m_pArcMapAV.Extent.XMin
pPoint.Y = (m_pArcMapAV.Extent.YMax - m_pArcMapAV.Extent.YMin) / 2 + m_pArcMapAV.Extent.YMin
Dim pGeoCoordSystem As ESRI.ArcGIS.Geometry.ISpatialReference
Dim pProjCoordSystem As ESRI.ArcGIS.Geometry.ISpatialReference
Dim pGeoTrans As ESRI.ArcGIS.Geometry.IGeoTransformation
Dim pSpatRefFact As ESRI.ArcGIS.Geometry.ISpatialReferenceFactory
pSpatRefFact = New ESRI.ArcGIS.Geometry.SpatialReferenceEnvironment
pGeoCoordSystem = pSpatRefFact.CreateGeographicCoordinateSystem(ESRI.ArcGIS.Geometry.esriSRGeoCSType.esriSRGeoCS_WGS1984)
pProjCoordSystem = pSpatRefFact.CreateProjectedCoordinateSystem(ESRI.ArcGIS.Geometry.esriSRProjCSType.esriSRProjCS_NAD1983SPCS_AK3FT)
pGeoTrans = pSpatRefFact.CreateGeoTransformation(ESRI.ArcGIS.Geometry.esriSRGeoTransformationType.esriSRGeoTransformation_NAD1983_To_WGS1984_2)
Dim pGeo As ESRI.ArcGIS.Geometry.IGeometry2
pGeo = pPoint
pGeo.SpatialReference = pProjCoordSystem
pGeo.ProjectEx(pGeoCoordSystem, ESRI.ArcGIS.Geometry.esriTransformDirection.esriTransformForward, pGeoTrans, 0, 0, 0)
Me.tbxLat.Text = CStr(pPoint.Y)
Me.tbxLon.Text = CStr(pPoint.X)
Catch ex As Exception
'DO NOTHING
End Try
Also, lol at the code paste feature not having a vb option.
Hi,
There is one sample project for VB in Esri Community ArcGIS Pro Samples. Solution is here. Try code below for your functionality:
Try 'TRY TO GET THE CENTER SCREEN LOCATION FROM THE ACTIVE VIEW, JUST SHOW THE FORM ON ERROR
Dim areaOfInterest = MapView.Active.Extent
Dim pPoint As MapPoint
pPoint = MapPointBuilderEx.CreateMapPoint((areaOfInterest.XMax - areaOfInterest.XMin) / 2 + areaOfInterest.XMin,
(areaOfInterest.YMax - areaOfInterest.YMin) / 2 + areaOfInterest.YMin, MapView.Active.Map.SpatialReference)
Dim pGeoCoordSystem As SpatialReference
Dim pProjCoordSystem As SpatialReference
Dim pGeoTrans As ProjectionTransformation
pGeoCoordSystem = SpatialReferenceBuilder.CreateSpatialReference(SpatialReferences.WGS84)
pProjCoordSystem = SpatialReferenceBuilder.CreateSpatialReference(102633)
pGeoTrans = ProjectionTransformation.Create(pProjCoordSystem, pGeoCoordSystem)
Dim pProjectedPoint As MapPoint
pProjectedPoint = CType(GeometryEngine.Instance.ProjectEx(pPoint, pGeoTrans), MapPoint)
Console.WriteLine(CStr(pProjectedPoint.Y))
Console.WriteLine(CStr(pProjectedPoint.X))
Catch ex As Exception
'DO NOTHING
End Try
As I understand your map spatial reference is esriSRProjCS_NAD1983SPCS_AK3FT. So, instead of creating pProjCoordSystem you can use Map spatial reference
Here is what I have so far, in C#:
await QueuedTask.Run(() =>
{
var extent = MapView.Active.Extent;
MapPoint mapPoint = MapPointBuilder.CreateMapPoint(
(extent.XMax - extent.XMin) / 2 + extent.XMin,
(extent.YMax - extent.YMin) / 2 + extent.YMin,
MapView.Active.Map.SpatialReference
);
MessageBox.Show($"Latitude: {mapPoint.Y}\nLongitude: {mapPoint.X}", "Coordinates", MessageBoxButton.OK, MessageBoxImage.Information);
});
For a specific location in Fairbanks, Alaska, this gives me:
However, I would expect something more along the lines of
Latitude: 64.8614161192099
Longitude: -147.90539636224
My guess is that in your vb code, lines 8-17 perform that conversion, but I'm uncertain how to do that in C#. The library names/classes appear to be different? My lack of knowledge on the various spatial reference types is probably not helping.
I understood that you need the code in VB. The same classes and methods you can call from c#.
8-17 lines in VB code makes projection with transformation. wkid 102633 is code for ArcObjects esriSRProjCS_NAD1983SPCS_AK3FT.
var areaOfInterest = MapView.Active.Extent;
MapPoint pPoint = MapPointBuilderEx.CreateMapPoint((areaOfInterest.XMax - areaOfInterest.XMin) / (double)2 + areaOfInterest.XMin, (areaOfInterest.YMax - areaOfInterest.YMin) / (double)2 + areaOfInterest.YMin, MapView.Active.Map.SpatialReference);
SpatialReference pGeoCoordSystem = SpatialReferenceBuilder.CreateSpatialReference(SpatialReferences.WGS84);
SpatialReference pProjCoordSystem = SpatialReferenceBuilder.CreateSpatialReference(102633);
ProjectionTransformation pGeoTrans = ProjectionTransformation.Create(pProjCoordSystem, pGeoCoordSystem);
MapPoint pProjectedPoint = GeometryEngine.Instance.ProjectEx(pPoint, pGeoTrans) as MapPoint;
Console.WriteLine(pProjectedPoint.Y);
Console.WriteLine(pProjectedPoint.X);
Documentation link is here
This would get you the lat / long of your extent.
var extent = rmg.Common.Methods.GetMapViewExtentAsGeometry(this, mapView, null);
// Convert to WGS84
var wgs84 = new SpatialReferenceBuilder(4326).ToSpatialReference();
var projectedCentre = GeometryEngine.Instance.Project(extent, wgs84);
// Get the center of the extent
var center = GeometryEngine.Instance.Centroid(projectedCentre);
dbllat = center?.Y.ToString();
dbllong = center?.X.ToString();
This would get you the lat / long of your extent in c#
var extent = MapView.Active.Extent;
// Convert to WGS84
var wgs84 = new SpatialReferenceBuilder(4326).ToSpatialReference();
var projectedCentre = GeometryEngine.Instance.Project(extent, wgs84);
// Get the center of the extent
var center = GeometryEngine.Instance.Centroid(projectedCentre);
dbllat = center?.Y.ToString();
dbllong = center?.X.ToString();