Convert GPS coordinates from one map datum 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
Download Eye4Software Coordinate GPS Toolkit Manual Download Eye4Software GPS Toolkit Manual
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 objProjection As GpsProjection
Private objDatumSrc   As GpsDatumParameters
Private objDatumDst   As GpsDatumParameters

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
objDatumSrc   = New GpsDatumParameters
objDatumDst   = New GpsDatumParameters

The source code

The following code shows how to perform a geodetic datum transformation using the GPS Toolkit. In this example a position is transformed from WGS84 to NAD27. For a list of all supported map datums, please refer to our map datum list.

// ConvDatum Visual C# .NET demo - Eye4Software GPS Toolkit
// This demo shows how to convert a latitude/longitude coordinate from one map datum 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 ConvDatum
{
  /// 
  /// Summary description for Class1.
  /// 
  class Class1
  {
    /// 
    /// The main entry point for the application.
    /// 
    [STAThread]
    static void Main(string[] args)
    {
      GpsProjection       objProjection = new GpsProjection ();
      GpsDatumParameters  objSrcDatum   = new GpsDatumParameters ();
      GpsDatumParameters  objDstDatum   = new GpsDatumParameters ();

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

      // Set Destination Datum ( NAD27 )
      // The ID for NAD27 is 4267, see 'http://www.eye4software.com/resources/datums'  
      // for a full list of supported datums. To convert to another datum, just change the code below (EPSG code)
      // To define your own datum, please have a look at the 'ConvUserGrid' Visual C# .NET demo.
      objDstDatum.LoadFromId(4267);

      // Set source latitude and longitude coordinates
      objProjection.Latitude = 33.59;
      objProjection.Longitude = -112.15;

      // Cast GpsDatumParameter interfaces to objects
      object objSrc = (object)objSrcDatum;
      object objDst = (object)objDstDatum;

      // Perform geodetic datum transformation
      objProjection.TransformDatum( ref objSrc, ref objDst);

      // Get and display result
      if (objProjection.LastError == 0)
      {
        Console.WriteLine("(WGS84) 33.590 N, 112.15 W => (NAD27) {0} N, {1} W", 
                          objProjection.Latitude, objProjection.Longitude);
      }
      else
      {
        Console.WriteLine("Error occured during datum transformation: {0} ({1})", 
                          objProjection.LastError, objProjection.LastErrorDescription);
      }

      Console.WriteLine("Ready.");
    }
  }
}