Share |

Convert GPS coordinates from one map grid to another using Visual C# .NET

Download Eye4Software GPS Toolkit free trial Download the Eye4Software GPS Toolkit fully functional 30 day trial version for free
Browse through the Eye4Software GPS Toolkit for Windows manual Browse through the Eye4Software GPS Toolkit manual

Introduction

The Eye4Software GPS toolkit allows software developers to add GPS functionality to their own programs without the need to have any knowledge on serial communications and GPS protocols like RS-232 and NMEA0183.

The product can be used in many programming environments, such as Visual Basic, Visual C++, Visual Studio.Net, Borland C++ Builder, Borland Delphi and VBA, but also web oriented applications such as ASP, ASP.NET and PHP, and all other programming environments that support ActiveX.

Prerequisites

First you must have Visual C# .Net and the Eye4Software GPS Component installed on your computer. We will use Visual C# .Net 2008 in this document, but other versions from 2002 and up can be used. You can download the Eye4Software GPS Component here.

Creating the project

Start the Visual Studio IDE, and select the "New" => "Project" option from the "File" menu. The "New Project" dialog now appears, select the "Windows Application" option to generate a Windows GUI application.

Add a reference to the ActiveX component

In order to declare and create the objects from your Visual C# .Net application, you need to add a reference to the ActiveX object, by choosing the "Add Reference..." option from the "Project" menu. A list of components installed on the system is displayed. Just select the "COM" tab, and select the "Eye4Software GPS Toolkit 3.0", click "Select" and finally click "OK".

Declare and create the object(s)

After adding the reference to the control, you can declare the objects like this:

private GpsProjection       objProjection;
private GpsConstants        objConstants;
private GpsDatumParameters  objDatumSrc;
private GpsDatumParameters  objDatumDst;
private GpsGridParameters   objGridSrc;
private GpsGridParameters   objGridDst;

The objects can be created in your code by double clicking on the Form you created, this will open the Formx_Load handler. Paste the following code in this function:

objProjection = new GpsProjection();
objConstants  = new GpsConstants();
objDatumSrc   = new GpsDatumParameters();
objDatumDst   = new GpsDatumParameters();
objGridSrc    = new GpsGridParameters();
objGridDst    = new GpsGridParameters();

The source code

The following code shows how to perform a map projection transformation using the GPS Toolkit. In this example a position is transformed from WGS84 to British National Grid. For a list of other map grids that are supported, please refer to our map grid list.

The second code sample shows how to define a grid by yourself, based on Lambert Conformal Conic projection. Use this method only when you want to convert from or to a map grid that is not supported by the GPS Toolkit.

Convert coordinates the simple way

// ConvGrid Visual C# .NET demo - Eye4Software GPS Toolkit
// This demo shows how to convert a latitude/longitude coordinate from one map grid to another.
// For more information on how to use the Eye4Software GPS Toolkit with Visual C# .NET, 
// visit http://www.eye4software.com/products/gpstoolkit/source#vcnet

using System;
using GpsToolkit;

namespace ConvGrid
{
  /// 
  /// Summary description for Class1.
  /// 
  class Class1
  {
    /// 
    /// The main entry point for the application.
    /// 
    [STAThread]
    static void Main(string[] args)
    {
      GpsProjection     objProjection = new GpsProjection ();
      GpsGridParameters objSrcGrid    = new GpsGridParameters ();
      GpsGridParameters objDstGrid    = new GpsGridParameters ();

      // Set Source Grid ( WGS84, Geographic Latitude and Longitude  )
      // The ID for WGS84 is 4326, see 'http://www.eye4software.com/resources/datums' 
      // for a full list of supported datums. To convert from another grid, just change the code below (EPSG code)
      // To define your own grid or datum, please have a look at the 'ConvUserGrid' Visual C# .NET demo.
      objSrcGrid.LoadFromId ( 4326 );

      // Set Destination Grid ( British National Grid )
      // The ID for the British National Grid is 27700, see 'http://www.eye4software.com/resources/grids' 
      // for a full list of supported grids. To convert to another grid, just change the code below (EPSG code)
      // To define your own grid or datum, please have a look at the 'ConvUserGrid' Visual C# .NET demo.
      objDstGrid.LoadFromId ( 27700 );

      // Set source latitude and longitude
      objProjection.Latitude = 55.400;
      objProjection.Longitude = -2.89;

      // Convert the grid
      object objSrc = (object)objSrcGrid;
      object objDst = (object)objDstGrid;
			
      objProjection.TransformGrid ( ref objSrc, ref objDst);
      
      // Get and display result
      if (objProjection.LastError == 0) 
      {
        Console.WriteLine("(WGS84) 55.400 N, 2.890 W => (British National Grid) Northing: {0}, Easting: {1}", 
                            objProjection.Northing, objProjection.Easting);
      }
      else
      {
        Console.WriteLine("Error occured during map grid transformation: {0} ({1})", 
                            objProjection.LastError, objProjection.LastErrorDescription);
      }
            
      Console.WriteLine("Ready.");
    }
  }
}

Convert coordinates with an user defined grid (Expirienced Users)

// ConvUserGrid Visual C# .NET demo - Eye4Software GPS Toolkit
// This demo shows how to convert a coordinate from one (user defined) map grid to another.
// If you only want to use well known map grids, you should have a look at the 'ConvGrid' demo.
// For more information on how to use the Eye4Software GPS Toolkit with Visual C# .NET, 
// visit http://www.eye4software.com/products/gpstoolkit/source#vcnet

using System;
using GpsToolkit;

namespace ConvUserGrid
{
  /// 
  /// Summary description for Class1.
  /// 
  class Class1
  {
    /// 
    /// The main entry point for the application.
    /// 
    [STAThread]
    static void Main(string[] args)
    {
      GpsProjection       objProjection = new GpsProjection();
      GpsConstants        objConstants  = new GpsConstants();

      GpsDatumParameters  objDatumSrc   = new GpsDatumParameters();
      GpsDatumParameters  objDatumDst   = new GpsDatumParameters();

      GpsGridParameters   objGridSrc    = new GpsGridParameters();
      GpsGridParameters   objGridDst    = new GpsGridParameters();

      // Set the Datum paramaters for WGS84
      objDatumSrc.Clear();

      // Set Semi Major Axis of WGS84 ellipsoid
      objDatumSrc.Axis = 6378137.0;

      // Set inverse flattening of WGS84 ellipsoid
      objDatumSrc.Flattening = 298.257223563;

      // Set destination datum ( Belgium 1972 )
      objDatumDst.Clear();

      // Set semi major axis of Hayford 1924 ellipsoid
      objDatumDst.Axis = 6378388.0;

      // Set inverse flattening of Hayford 1924 ellipsoid
      objDatumDst.Flattening = 297.0;

      // Set Helmert7 parameters Belgium 1972 => WGS84
      objDatumDst.TranslationX  = -99.059;
      objDatumDst.TranslationY  = 53.322;
      objDatumDst.TranslationZ  = -112.486;
      objDatumDst.RotationX     = 0.419;
      objDatumDst.RotationY     = -0.83;
      objDatumDst.RotationZ     = 1.885;
      objDatumDst.ScaleFactor   = -1.0;
      
      object objSrc = (object)objDatumSrc;
      object objDst = (object)objDatumDst;

      // Set source grid ( Latitude / Longitude, WGS84 )
      objGridSrc.Projection = objConstants.GPS_PROJECTION_NONE;
      objGridSrc.set_Datum ( ref objSrc );

      // Set destination grid ( Lambert Conformal Conical projection, Belgium 1972, Belgium Lambert 72 )
      objGridDst.Projection = objConstants.GPS_PROJECTION_LAMBERT2SP;
      objGridDst.set_Datum ( ref objDst );

      // Set Lambert parameters
      objGridDst.FalseEasting = 150000.01256;
      objGridDst.FalseNorthing = 5400088.438;
      objGridDst.OriginLatitude = 90.0;
      objGridDst.OriginLongitude = 4.367487;
      objGridDst.ParallelNorth = 49.833333333;
      objGridDst.ParallelSouth = 51.166666666;
      
      // Set source latitude and longitude
      objProjection.Latitude = 50.505;
      objProjection.Longitude = 4.47;

      // Perform map grid transformation
      objSrc = (object)objGridSrc;
      objDst = (object)objGridDst;

      objProjection.TransformGrid ( ref objSrc, ref objDst);

      // Get and display result
      if (objProjection.LastError == 0) 
      {
        Console.WriteLine("(WGS84) 50.5050 N, 4.4700 E => (Belgium Lambert 1972) Northing: {0}, Easting: {1}", 
                          objProjection.Northing, objProjection.Easting);
      }
      else
      {
        Console.WriteLine("Error occured during map grid transformation: {0} ({1})", 
                          objProjection.LastError, objProjection.LastErrorDescription);
      }
            
      Console.WriteLine("Ready.");
    }
  }
}