If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)

A class for calculating between Latitude/Longitude and Universal Transverse Mercator (UTM) coordinates which accurate maps like to use. It will reverse calculate as well.

You can set the class to use one of various datum ellipsoids such as WGS84, NAD83 and several others. The numbers programmed for each ellipsoid aren't the most accurate, but they can be easily modified to suit tastes. Currently, the error produced seems to be within a meter or so.

All equations and figures taken from an Excel spreadsheet example written by Professor Steven Dutch, of University of Wisconsin-Green Bay and is primarily ripped from the 1973 U. S. Army Technical Manual on the Universal Transverse Mercator Grid. I only take credit for turning it into a VB.NET 2008 Class

EDIT: I found a bug in the code that was flipping the sign of the Longitude during the UTM to Lat/Long conversion. New file uploaded with the fix.

Hi,
I’m working with data downloaded from a gps device, using vb2008 express I can open a file, load this into an array and extract the required lat long co-ordinates. I now need to convert these to utm so that I can do some basic trig to calculate distances.
Your class seems ideal for the next step but as I'm only at the stage of creating basic programs I need some help to get the data from the array processed by the class and extract the utm data at the end.
I’m just doing this for fun, in connection with my other hobby, paragliding, any help or direction as to where to get more info would be greatly appreciated.
Thanks
Chris

Get your numbers from the array. You should have a Lat and a Long number. Then just follow my example above. Put those numbers in the myDecimalLatitude and myDecimalLongitude spots in that example. For example, if you had Lat: 51.6702014 Long: -76.1103928, you'd do this (remember, negative coordinates indicate South latitudes and West longitudes):

thank you for your help with this, I still have some work to do on processing the data form the array but I am making progress with this. I can now see how the class works and think it is brilliant. I have tested the accuracy, at home here in Northumberland, UK, it is accurate to under 1 meter.
Again, many thanks for your help.
Chris

What errors are you getting exactly, because looking at your screenshot, it looks like you have the GPS class on the tab behind the Form1 object.

Having the file open in VS doesn't mean it's part of the project. If you already have VS open and you download a VB file it will be opened in the same VS window, but it's not added to the project automatically.

True, hadn't thought of that. You can always "view" .vb files in the editor, it doesn't mean they've been added to your solution. He copy-pasted my code example in the global area of his class object as well. It needs to be inside a subroutine or a function block like all other non-defining code.

You just need a proper map that has UTM alignment information from what I understand. It'll have a grid of Northing and Easting on it with UTM coordinates; as opposed to a map with Lat and Long projections.

The other option is I convert the northing value into pixels. So if there is 80&#176; S latitude and 84&#176; N then there is 164 in total start from the bottom. If my map was 492px high and I wanted to find something at 76S I would go 12 pixels high. 3 times.

Only problem is this would be way out unless I made the 164 the true value as such, how many meters is it between 80 and 79??

The error was fixed and posted long ago. The only error found in the code that has not been fixed in the download is an insignificant math error in the arc second calculation that makes the number off at most 1 inch at the equator. If you want to fix that, then just change this line in the object:

Code:

Dim e0 As Double = (315 * a * n ^ 4 / 51) * (1 - n)

to this:

Code:

Dim e0 As Double = (315 * a * n ^ 4 / 512) * (1 - n)

Are you instancing the class properly? Are you specifying the zone, northing, easting and hemisphere? You need all four pieces of information to convert from UTM to Lat/Long. See my example above.

Hi Gary,
You could use the Haversine formula, see http://en.wikipedia.org/wiki/Haversine_formula
This calculates distance between two lat/long positions based on the earth as a perfect sphere; it gives an accuracy of about 0.3&#37; and is not too difficult to understand and code. The more complex Versine formula uses a model that can be based on a map datum such as wgs84 and is more accurate - but I couldn’t figure out how to code this, perhaps someone more able might have a go.
cheers
chris

That's because your data is bad. How do you have numbers in the 32 million and 5 million range? If you put your numbers in any online converter you'll see some crazy funny-number results like a Lat of 520 degrees.

Alright, I figured it out. This is such a stupid notation.
Take your data:
32337666
5656482
32N

The first number is a combination of the Zone and the Easting: 32337666 You want to remove the Zone numbers before converting. They're always the first two. This is apparently a standard notation used by the Germans.
So, put THESE numbers into the converter:
Northing: 5656482
Easting: 337666
Zone: 32
Hemisphere: N

Of course, you can always edit the converter to accept that notation as the standard input and strip the zone off then.

After compiling and using the code in C# I have tried to convert the coordinates from a user click on my Web Mapping Application. My coordinates are in UTM Zone 10.

Sample coords are: 510472 E and 5440699 N.
This gives results of 48.97625305 and -122.8564619 which is many miles to the south of where I want. I want to be able to zoom to google maps based on the user click.

maybe something happened when I convert to C#.

Jenner using the vb code what lat/long do you get for the above coords.