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.
Here's how to use it from Lat/Long in decimal coordinates (+ = N/E, - = S/W):
Code:
Dim lat As New GPS.Coordinate(myDecimalLatitude, GPS.CoordinateType.Latitude)
Dim lng As New GPS.Coordinate(myDecimalLongitude, GPS.CoordinateType.Longitude)
Dim utm As New GPS.UTM(lat, lng)
Me.txtNorthing.Text = utm.Northing.ToString
Me.txtEasting.Text = utm.Easting.ToString
Me.txtZone.Text = utm.Zone.ToString
Me.cmbHemisphere.Text = If(utm.UTMLatitudeHemisphere = GPS.UTMLat.North, "N", "S")
Here's how to use it in the opposite direction. You need 4 pieces of info, Northing (m), Easting (m), Zone and whether it's N or S hemisphere (default is North):
Code:
Dim utm As New GPS.UTM(myNorthing, myEasting, myZone)
utm.UTMLatitudeHemisphere = GPS.UTMLat.North
Me.txtLatitude.Text = utm.Latitude.ToString
Me.txtLongitude.Text = utm.Longitude.ToString
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.
Last edited by Jenner; Oct 15th, 2009 at 09:09 AM.
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
Hi i have a UTM coordinates like this x = 520825 , y = 2869270
How i can convert this coordinates to Latitude/Longitude? , my zone is 14 and Hemisphere is North.
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
That would be the second example I posted above. Sorry, I noticed the variable names were still listed as "Latitude" and "Longitude" and not "Northing" and "Easting".
But just make a new object, give it the 4 peices of information and read the latitude and longitude from it. It does the conversion as you change the data.
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
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
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
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):
Code:
Dim myDecimalLatitude = 51.6702014
Dim myDecimalLongitude = -76.1103928
Dim lat As New GPS.Coordinate(myDecimalLatitude, GPS.CoordinateType.Latitude)
Dim lng As New GPS.Coordinate(myDecimalLongitude, GPS.CoordinateType.Longitude)
Dim utm As New GPS.UTM(lat, lng)
'The results are instantly calculated and available:
Me.txtNorthing.Text = utm.Northing.ToString
Me.txtEasting.Text = utm.Easting.ToString
Me.txtZone.Text = utm.Zone.ToString
Me.cmbHemisphere.Text = If(utm.UTMLatitudeHemisphere = GPS.UTMLat.North, "N", "S")
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
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
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
Originally Posted by Jenner
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.
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
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.
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
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.
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
The other option is I convert the northing value into pixels. So if there is 80° S latitude and 84° 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??
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
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.
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
Thanks for the reply:
I am basicly taking a lat/lon and passing it to the LL To UTM converter then taking that result and passing it to the UTM to LL Converter.
Here is what I do:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
LLToUTM(31.5, 91.5)
End Sub
Public Sub LLToUTM(ByVal myDecimalLatitude As Single, ByVal myDecimalLongitude As Single)
Dim txtNorthing As String
Dim txtEasting As String
Dim txtzone As String
Dim cmbhemisphere As String
Dim lat As New GPS.Coordinate(myDecimalLatitude, GPS.CoordinateType.Latitude)
Dim lng As New GPS.Coordinate(myDecimalLongitude, GPS.CoordinateType.Longitude)
Dim utm As New GPS.UTM(lat, lng)
txtNorthing = utm.Northing.ToString '<<-This is 3485991.16655783
txtEasting = utm.Easting.ToString '<<-This is 357546.073706798
txtzone = utm.Zone.ToString ' <<-This is 46
cmbhemisphere = If(utm.UTMLatitudeHemisphere = GPS.UTMLat.North, "N", "S") 'N
TUMToLL(utm.Northing, utm.Easting, utm.UTMLatitudeHemisphere)
End Sub
Public Sub TUMToLL(ByVal MyNorthing As Single, ByVal myEasting As Single, ByVal myzone As String)
Dim txtLatitude As String
Dim txtLongitude As String
Dim utm As New GPS.UTM(myNorthing, myEasting, myZone)
utm.UTMLatitudeHemisphere = GPS.UTMLat.North
txtLatitude = utm.Latitude.ToString '<<-This is 31° 29' 59.989" North
txtLongitude = utm.Longitude.ToString '<<-This is '0° 0' 0.000" North
End Sub
The Longitue is returning a NORTH value.
I downloaded the GPS.VB from the top if this post.
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
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% 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
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
Hi
how to get my zone value?
if i have those values
lat=49.568511
lon=27.104216
what will be the code to convert them to N,E ?
i try this code http://www.vbforums.com/showpost.php...59&postcount=1
but it gives me wrong values !!
what will be te right code?
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
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.
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
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.
Last edited by Jenner; Mar 12th, 2010 at 08:54 AM.
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
It's really simplistic code from a conversion standpoint. There's no events or event handlers, no fancy VB tricks. Just some basic constructors and math.
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
Hello Jenner,
I finally manged to get it to compile in C#. Not as straightforward as I had hoped.
Another thing that I noticed is the follwong line:
double sin1 = Math.PI / (180 * 3600); warning CS0219: The variable 'sin1' is assigned but its value is never used.
Is this important or maybe it is just for testing?
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
It doesn't surprise me. The equation set I used was simplified for slide-rule and table calculation and some terms were used in more than one figuring.
Re: GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)
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.