Search code examples
c#classcoordinateswgs84

Convert WGS84 Lat Lon coordinates to OSGR using NMEA2OSG Class – C#


I need to convert from the WGS84 Lat Lon coordinate system (basically what Google maps and Bing use) to the British Ordinance Survey Coordinates system. As the calculation is rather complicated (something about ellipsoids and projection etc) there is an off the shelf C# class that is commonly used called “NMEA2OSG”.

Link to the NMEA2OSG Class: NMEA2OSG Class

I’m struggling to understand how to use this class. Whilst there are several questions on SO about how to convert WGS84 to OSGR there are non on how to use the NMEA2OSG class.

I’ve tried running it’s second method, which seems like the correct method as it accepts long lat decimals as an input. Attempt as follows:

NMEA2OSG nMEA2OSG = new NMEA2OSG();
bool coord = nMEA2OSG.Transform(52.3, -0.1, 50);

However, this method only returns a bool, so presumable I need to access the output from the method from the instance of the class. Unfortunately I don’t understand how to do this.

Can anyone advise how to return converted coordinates from the NMEA2OSG class?

Bonus question, if anyone knows that the height variables means in the ‘Transform’ method that would also be useful. Thanks


Solution

  • The NMEA20SG class has events for notifying you of the results. Here's the code for setting that up:

    static void Main(string[] args)
    {
        NMEA2OSG nMEA2OSG = new NMEA2OSG();
        nMEA2OSG.NorthingEastingReceived += NMEA2OSG_NorthingEastingReceived;
        nMEA2OSG.NatGridRefReceived += NMEA2OSG_NatGridRefReceived;
        bool coord = nMEA2OSG.Transform(52.3, -0.1, 50);
        Console.ReadKey();
    }
    
    private static void NMEA2OSG_NatGridRefReceived(string ngr)
    {
        Console.WriteLine("NatGrid: {0}", ngr);
    }
    
    private static void NMEA2OSG_NorthingEastingReceived(double northing, double easting)
    {
        Console.WriteLine("Northing = {0}, Easting = {1}", northing, easting);
    }
    

    Results for your input:

    Northing = 268576, Easting = 529657
    NatGrid: TL 29657 68576