SwDevMan81
SwDevMan81

Reputation: 49978

Convert Lat/Lon to MGRS

Does anyone know where I could find a library of code for converting Lat/Lon position to Military Grid Reference System (MGRS)? I'm looking for a C# implementation if possible.

Upvotes: 11

Views: 10018

Answers (4)

Tronald
Tronald

Reputation: 1585

CoordinateSharp is available as a Nuget package and can do just that.

Coordinate c = new Coordinate(40.57682, -70.75678);
c.MGRS.ToString(); // Outputs 19T CE 51307 93264

Upvotes: 4

Zvi Redler
Zvi Redler

Reputation: 1758

Found on js if it's help...

https://github.com/codice/usng.js

usage-

var converter = new usngs.Converter();
alert(converter.LLtoMGRS(33.754032, -98.451233, 9));

Upvotes: 2

SwDevMan81
SwDevMan81

Reputation: 49978

We ended up using GeoTrans and create a DLL from the code and using PInvoke to call the functions. We pulled the following from the source incase anyone wanted to know (minimal solution):

  • polarst
  • tranmerc
  • ups
  • utm
  • mgrs

The PInvoke Signature we used:

[DllImport("mgrs.dll")]
public static extern int Convert_Geodetic_To_MGRS(
   double Latitude,
   double Longitude,
   int Precision, // 1 to 5, we used 4 (10 square meters)
   StringBuilder MGRS);

which corresponds to this function in mgrs.h:

MGRSDLL_API long __stdcall Convert_Geodetic_To_MGRS(
   double Latitude,
   double Longitude,
   long Precision,
   char* MGRS);

Upvotes: 5

Reed Copsey
Reed Copsey

Reputation: 564333

You can use GDAL's C# wrappers to convert from lat/lon to UTM. You then just need to format the values appropriately for MGRS, since it's just UTM with a different numerical format.

Upvotes: 2

Related Questions