Share |

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

Dowload 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 Basic .Net and the Eye4Software GPS Component installed on your computer. We will use Visual Basic .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 Basic .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 objConstants  As GpsConstants
Private objDatumSrc   As GpsDatumParameters
Private objDatumDst   As GpsDatumParameters
Private objGridSrc    As GpsGridParameters
Private objGridDst    As GpsGridParameters

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 NAD27 State Plane (Arizona Central) grid. For a list of other supported map grids, 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 GPS coordinates the simple way

' ConvGrid Visual Basic .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 Basic .NET, 
' visit http://www.eye4software.com/products/gpstoolkit/source#vbnet

Imports GpsToolkit

Module Module1

  Private objProjection As GpsProjection
  Private objSrcGrid    As GpsGridParameters
  Private objDstGrid    As GpsGridParameters

  Sub Main()

    objProjection = New GpsProjection
    objSrcGrid = New 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 list of supported datums 
    ' To convert from another datum or grid, just change the code below (EPSG code)
    ' To define your own grid or datum, please have a look at the 'ConvUserGrid' Visual Basic .NET demo.
    objSrcGrid.LoadFromId(4326)

    ' Set Destination Grid ( Arizona Central State Plane NAD27 )
    ' The ID for the Arizona Grid is 26749, see 'http://www.eye4software.com/resources/grids' for a list of 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 Basic .NET demo.
    objDstGrid.LoadFromId(26749)

    ' Set source latitude and longitude
    objProjection.Latitude = 34.165
    objProjection.Longitude = -111.89

    ' Perform map grid transformation
    objProjection.TransformGrid(objSrcGrid, objDstGrid)

    ' Get and display result
    If (objProjection.LastError = 0) Then
      Console.WriteLine("(WGS84) 34.165 N, 111.89 W => (Arizona Central) Norhing: {0}, Easting: {1}", 
                          objProjection.Northing, objProjection.Easting)
    Else
      Console.WriteLine("Error occured during map grid transformation: {0} ({1})", 
                          objProjection.LastError, objProjection.LastErrorDescription)
    End If

    Console.WriteLine("Ready.")
  End Sub
End Module

Convert coordinates with an user defined grid (Expirienced Users)

' ConvDatum Visual Basic .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 Basic .NET, 
' visit http://www.eye4software.com/products/gpstoolkit/source#vbnet

Imports GpsToolkit

Module Module1

  Private objProjection As GpsProjection
  Private objConstants As GpsConstants
  Private objDatumSrc As GpsDatumParameters
  Private objDatumDst As GpsDatumParameters
  Private objGridSrc As GpsGridParameters
  Private objGridDst As GpsGridParameters

  Sub Main()
    ' Create instances of objects
    objProjection = New GpsProjection
    objConstants = New GpsConstants
    objDatumSrc = New GpsDatumParameters
    objDatumDst = New GpsDatumParameters
    objGridSrc = New GpsGridParameters
    objGridDst = New GpsGridParameters

    ' Set source datum ( 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

    ' Set source grid ( Latitude / Longitude, WGS84 )
    objGridSrc.Projection = objConstants.GPS_PROJECTION_NONE
    objGridSrc.Datum = objDatumSrc

    ' Set destination grid ( Lambert Conformal Conical projection, Belgium 1972, Belgium Lambert 72 )
    objGridDst.Projection = objConstants.GPS_PROJECTION_LAMBERT2SP
    objGridDst.Datum = objDatumDst

    ' 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
    objProjection.TransformGrid(objGridSrc, objGridDst)

    ' Get and display result
    If (objProjection.LastError = 0) Then
      Console.WriteLine("(WGS84) 50.5050 N, 4.4700 E => (Belgium Lambert 1972) Norhing: {0}, Easting: {1}", 
                          objProjection.Northing, objProjection.Easting)
    Else
      Console.WriteLine("Error occured during map grid transformation: {0} ({1})", 
                          objProjection.LastError, objProjection.LastErrorDescription)
    End If

    Console.WriteLine("Ready.")
  End Sub
End Module