Need help converting Lon/Lat to MGRS for display

3316
3
08-04-2015 12:33 PM
KennethTriplett
New Contributor

I am working on an add-in for the ArcGIS Explorer Desktop mapping client and need to take
Lon/Lat coodinates and display them in an MGRS formatted string in a Note or MessageBox.

Ideally, Lon/Lat (0, 51.4791) would end up being displayed as MGRS 31UBT9168707373

I used sample code from the ArcGIS Resource Center, shown below, but when I do a ToString on the resulting point in the first code sample using the Geographic Coordinate System, I get:
resultString = "Point: X = 0 Y = 51.4791000009189" (obviously the original Lon/Lat)
In the second code sample using the Projected Coordinate System, I get:
resultString1 = "Point: X = -538880.521630103 Y = 5810593.60821005" (something entirely different, but not the final solution)

What do I need to do to end up with the string "31UBT9168707373"?
Thanks for any help!

First code sample:
  // Create a Point at Greenwich, UK. By default new Geometries have the WGS 1984 geographical coordinate system.
  ESRI.ArcGISExplorer.Geometry.Point projectPoint1 = new ESRI.ArcGISExplorer.Geometry.Point(0, 51.4791);
 
  // Create an instance of the CoordinateSystem we want to project the Point into.
  CoordinateSystem projectIntoHTRS96 = CoordinateSystem.GeographicCoordinateSystems.Europe.HTRS96;
 
  // Retrieve suitable transformations between the two coordinate systems.
  System.Collections.Generic.IList<GeographicTransformation> suitableTrans1 = GeographicTransformation.GetTransformations(projectPoint1.CoordinateSystem, projectIntoHTRS96);
  ESRI.ArcGISExplorer.Geometry.Point outputPoint1 = null;
  // For brevity here, simply pick the first suitable transformation.
  GeographicTransformation geoT1 = suitableTrans1[0];
  // Project the point using the selected transformation. Cast back to a Point - Project does
  // not change the type of Geometry.
  outputPoint1 = GeometryOperations.Project(projectPoint1, projectIntoHTRS96, geoT1) as ESRI.ArcGISExplorer.Geometry.Point;
  String resultString = outputPoint1.ToString();

Second code sample: 
  // Create an instance of the CoordinateSystem we want to project the Point into.
  CoordinateSystem projectIntoHTRS96Z = CoordinateSystem.ProjectedCoordinateSystems.UTM.Europe.HTRS96UTMZone33N;
 
  // Retrieve suitable transformations between the two coordinate systems.
  System.Collections.Generic.IList<GeographicTransformation> suitableTransZ = GeographicTransformation.GetTransformations(projectPoint1.CoordinateSystem, projectIntoHTRS96Z);
  ESRI.ArcGISExplorer.Geometry.Point outputPointZ = null;
  // For brevity here, simply pick the first suitable transformation.
  GeographicTransformation geoTZ = suitableTransZ[0];
  // Project the point using the selected transformation. Cast back to a Point - Project does
  // not change the type of Geometry.
  outputPointZ = GeometryOperations.Project(projectPoint1, projectIntoHTRS96Z, geoTZ) as ESRI.ArcGISExplorer.Geometry.Point;
  String resultString1 = outputPointZ.ToString();

Other notes:

I was hoping for:
MGRS 31UBT9168707373

geoT1 and geoTZ are both = {HTRS96_To_WGS_1984_1}
_core = {ESRI.ArcGISExplorer.Internal.WrapperCore<ESRI::ArcGISExplorer::Geometry::GeographicTransformation>}
Name = "HTRS96_To_WGS_1984_1"

projectIntoHTRS96 = {GCS_HTRS96 (4761)}
resultString = "Point: X = 0 Y = 51.4791000009189"

projectIntoHTRS96Z = {HTRS96_UTM_Zone_33N (3767)}
resultString1 = "Point: X = -538880.521630103 Y = 5810593.60821005"

0 Kudos
3 Replies
MelitaKennedy
Esri Notable Contributor

Even though I work at Esri, I am not familiar at all with the ArcGIS Explorer Desktop SDK capabilities. I did look through the SDK help, and didn't see anything relevant.

I wanted to point out that the Project method won't support input or output of MGRS because that's what we call a coordinate notation not true "coordinates". MGRS will be defined as a string.

I'm also confused why you used HTRS96 as part of the coordinate system. Did you know that's the GCS for Croatia?

So, my guess is that MGRS conversion is not supported. Many of the other APIs/SDKs do support it, like the ArcGIS Runtime SDK does include support for MGRS conversions.

Melita

0 Kudos
NormanDeschamps
New Contributor III

Hey Kenneth,

Melita is right; out of the box, AGX does not deal with MGRS coordinates very well within the API. For my project I use a GPL library called .NET Coordinates for getting the MGRS coordinates (or other coordinate systems as well) of any lat/long point. You can download the library from here:

.NET Coordinates

The library works very well. Certainly I have never had any issues with it.

I have also written other stuff myself to get the correct UTM coordinate system within AGX for a given set of latitude/longitude coordinates if you are also looking to change coordinate systems in addition to acquiring MGRS coordinates. If you also want that, let me know.

Norm

0 Kudos
KennethTriplett
New Contributor

Thank you both for the quick, insightful responses.

Melita,

I used the HTRS96 because when I examine the ActiveMapDisplay object, that's what it shows here:

_coordinateProjectedSystem2D = {HTRS96_UTM_Zone_33N (3767)}. How it got set to that is unknown to me.  After further testing, it doesn't seem to change when I set the coordinates in the menu bar to other coordinate systems. In fact I don't see anything change in the ActiveMapDisplay object when I change coordinates, so perhaps it only affects the grid overlay that shows the various zones and displays the center-screen coordinates in the selected coordinate system.

Norman,

The grid overlay switches easily between coordinate systems, and displays the various coordinate notations, so I'm just surprised that the methods to do so are not exposed in the AGX SDK.  Thanks for the link to the library you use. I may take you up on your generous offer as well.  Thanks again.

0 Kudos