# VBForums CodeBank > CodeBank - Visual Basic .NET >  GPS Class that maps Lat/Long to UTM Coordinates (WGS84 and others)

## Jenner

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 is the class: GPS.vb

Here's how to use it from Lat/Long in decimal coordinates (+ = N/E, - = S/W):


```
        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):


```
        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  :Smilie: 

_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._

----------


## Mithos

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.

Regards
Isaias

----------


## Jenner

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.

----------


## SprayPaint

i get 'Truncate' is not a member of 'System.Math'

any ideas??

----------


## chrisfozz

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

----------


## Jenner

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):



```
        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")
```

----------


## chrisfozz

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

----------


## Jenner

I'd say less than 1 meter is pretty good for most needs.   :Smilie:  

I think this class can be implemented a little better, but I honestly haven't had the time to do it.

----------


## kiwis

what do I have to import o get GPS working?

----------


## jmcilhinney

> what do I have to import o get GPS working?


Have you added the GPS.vb source file to your project?  If not then how is the project supposed to know the GPS class exists?

----------


## kiwis

I thought I had but clearly not.

----------


## 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.

----------


## jmcilhinney

> 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.

----------


## Jenner

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.

----------


## kiwis

How can I place these coordinates on a map?

----------


## Jenner

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.

----------


## kiwis

I can not seem to find a world map :-(

----------


## kiwis

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??

----------


## Jenner

Right, at this point, your problem seems to be one of conversion between real-world coordinates and what is displayed on the screen.

----------


## kiwis

Yes. I want to be able to convert long and lat coord's onto a world map displayed in front of me on my screen

----------


## VBGarry

I downloaded the file GPS.VB and loaded into VB.net. Using UTM to Lat/Lon, the Longitude that is returned is always 0.

You mentioned in one of the post that the code had an error.  Could you Please post the corrected code?

Thanks

----------


## Jenner

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:



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

to this:



```
            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.

----------


## VBGarry

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.


Thanks again

----------


## VBGarry

Well....as soon as i sent the previous reply...I saw my error.  I sent the wrong parameter for the ZONE.


Thanks  It works GREAT

----------


## VBGarry

Jenner
Your class works great...Thanks
But, I do have a question for you.

Since the Easting and Northing returns values from their ZONES.  How can I calculate a value such as distance from two points in two different Zones?


Thanks again

----------


## Jenner

The only way would be to convert the WGS coordinates to Lat/Long and perform a Great Circle calculation to get the arc length between them.

----------


## chrisfozz

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

----------


## macnux

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?

thanks in advance.

----------


## Jenner

If it gives you the wrong values, then you're using it wrong.

It works perfectly:

----------


## macnux

thanks for reply

yea right this result also for me
Northing=5490662.87262606
Easting=507535.487639871
Zone=35
Hemisphere=N

BUT when im in Google earth and i convert from lat and Lon to N,E from options it gives me 
27&#176; 6.253 N
49&#176; 34.111 E

so how to make it like Google result

----------


## Bytechanger

Hi,

thanks for you great class! But it doesn&#180;t work for me...

I&#180;ve                                  

* UTM  in Format     
32337666 
5656482   
32N*
and I need to get WGS84 in this format   

*50.94082111  
6.68391562*  !

But when I start this code


```
        Dim utm As New GPS.UTM(5656482.0, 32337666.0, 32)
        utm.UTMLatitudeHemisphere = GPS.UTMLat.North

        Label6.Text = utm.Latitude.ToString
        Label7.Text = utm.Longitude.ToString
```

I get
  0&#176; 0' 0,000" North
  0&#176; 0' 0,000" North
as result!!!

Greets

Byte

----------


## Jenner

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.

----------


## Bytechanger

Hi,

I get the data from a address-database!
What do you mean, my data is crazy?

Do you think it isn&#180;t UTM ?


ATENTION:

on Site  http://www.ipf.uni-karlsruhe.de/cgi-bin/trafo.tcl
   UTM 32 WGS84   ->   Gauss-Krueger Nr.2/6 Grad (West-D) 

WORKS FINE !!!!!! But theres no source
Need help, please....


Greets

Byte

----------


## Jenner

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*: 32*337666*  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.

----------


## cblandy

Any chance you have the code for C#. I tried a bunch of converters but code would not compile. What imports ("using" in C# to I need).

Cam

----------


## Jenner

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.  

Of course, you could always take up VB.NET.   :Wink:

----------


## cblandy

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?

Cam

----------


## Jenner

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.

----------


## cblandy

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.

Cameron

----------


## Jenner

I get: 49.119 lat, -122.856 long (assuming north hemisphere)

Yea, check the math in the latitude conversion.

----------


## macnux

Hi
i have these coordinates *N53200,E70300* from a map and this place in saudi arabia

and i want to convert to it corresponding lat and lon (lat=27 13.0947 lon=49 539406)
how?
i try the above code but it gives me wrong value 
thanks in advance.

----------


## jstnice

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#.

----------


## ingekr

In sample 2 from your first post, how do I return decimal degree?

Regds
Inge

----------


## macnux

any idea how to convert those coordinates N53200,E70300
to degrees? because i don't know what are these coordinates are?
thanks in advance.

----------


## Jenner

Please verify all coordinates with this (or any other) online conversion engine:  http://www.rcn.montana.edu/resources...ordinates.aspx

I've verified this class many times with the example one I gave and several others, and it's been extremely accurate.  If you don't know what kind of coordinate data you have, then I suggest reviewing your own data and researching various coordinate systems on Google.   

Looking over the code, you should easily find where it generates the decimal values of Latitude and Longitude before converting it to the output strings.

Finally, no, I don't know what global coordinate system Google Maps uses or if it works/doesn't work with this class.  Once again, it's something you'll have to research with Google.  Like the title of this post says, this class converts between  Lat/Long and various UTM Coordinates such as WGS84.  If you don't understand what every word in that last sentence means, then you've got some research to do.  :Smilie: 

Converting map coordinates is far-far trickier than most people think.  There are a LOT of coordinate/mapping standards out there; some global, some outdated, many local or country-specific. Some are even patch-work which means depending on where you are, the computational data changes (such as NAD27).  Know what you're working with before posting please.  

Thanks you.

----------


## 23michaelj45

Hello Jenner,
I am new to coordinate conversion, but was googling around and found your class and it is exactly what I need. I am following your examples, using a Lat/Long of 35.97327766/-85.96502471 and trying to convert it to NAD83 UTM (TN Zone 4100). I am getting the values easting: 593312.90865883883 and northing: 3981479.7292554607, but the values I have supplied to me by a flight client for the easting/northing are 1978849.094,596951.126. Any help or expertise you could provide would be greatly appreciated.

Thank you.

----------


## jvonbokel

Can this be used to override zones?  By that I mean, can I force it to calculate the Easting/Northing in a specific zone?

Thanks,
John

----------


## jlundstocholm

Hello Jenner,

(I hope you are still paying attention to this thread)

I am trying to use the class you built in VB in my project. However, I am having problems with coordinates in the southern hemisphere.

I have created a C# test class based on the Excel spreadsheet from http://www.uwgb.edu/dutchs/UsefulDat...ertUTMNoOZ.HTM . I have put the file on GitHub - it is available on https://github.com/jlundstocholm/Coo...Transformation .

All coordinates in the Northern hemissphere are calculated correctly, but all coordinates in South fail. As an example, the coordiates

Northern: 3000000
Eastern: 430000
Hemisphere: South
Zone: 28

Is supposed (from the spreadsheet as well at the test page on http://www.rcn.montana.edu/resources...&n=3000000&h=S ) to be 



```
Longitude: 15&#176; 42' 22,616" West
Latitude: 27&#176; 8' 45.503" South
```

But your class yields the result



```
Longitude: {16&#176; 23' 16,314" West
Latitude: 63&#176; 7' 21,179" South
```

I have looked thru the code and as far as I can see, it pretty much mimics the formulas in the spreadsheet.

Do you have an idea of what I am doing wrong?

And also: have you thought about putting an Open Source license to your class like Apache, BSD or similar? It would make it easier to reuse in projects.

Thank you,

:-)

/Jesper Lund Stocholm
Copenhagen, Denmark

----------


## Freelance

Hello Jenner.
I need to convert Lat/Lon coordinates to MGRS and vice-versa ina VB. Net web application (VS2010).

Could you help?
T.I.A.

----------


## Jenner

MGRS is based on UTM and isn't too hard to convert between the two.  Just Google "MGRS to UTM formula" and you should find something you can use.  Thus, you can use this class to convert from Lat/Long to UTM and then from UTM to MGRS and back.

----------


## krepro

> Can this be used to override zones?  By that I mean, can I force it to calculate the Easting/Northing in a specific zone?
> 
> Thanks,
> John


The GPS.vb was just what I've been looking for. It works great. :big yellow: 
But I also need to "force" zones. In Norway we only use UTM zone 32,33 and 35. We skip 31, 34 and 36.

Can anyone give me a hint of what to do?

Thanks.

----------


## Jenner

You can probably change the math routine to skip certain zones and calculate to adjacent zones. It sounds like Norway uses a local variant of general purpose UTM.  Several countries do apparently, there are dozens and dozens of "local" systems that are anywhere from small tweaks to UTM to complete hybrids with local patch transforms.

----------


## krepro

> You can probably change the math routine to skip certain zones and calculate to adjacent zones. It sounds like Norway uses a local variant of general purpose UTM.  Several countries do apparently, there are dozens and dozens of "local" systems that are anywhere from small tweaks to UTM to complete hybrids with local patch transforms.


I could substitue GetZoneCM() with 9 (centermeridian of zone 32). that would probably do the trick. Btw all of southern Norway is in zone 32, although the western part should be in zone 31. 

http://en.wikipedia.org/wiki/File:LA...-UTM-zones.png

Anyway thnx  :Smilie:

----------


## TimHaydnJones

Hi Jenner - I really appreciate the effort you have invested to make this code available and I accept that it is accurate, so I also accept any error is my error but hoping to prevail on your good nature (what a creep, eh)

OK, although I'm new to this vb .Net lark, I think I have installed the code OK on the grounds that it doesn't crash and it doesn't complain and it does something.


vb Code:
Dim utm As New GPS.UTM(340374, 366857, 30)         utm.UTMLatitudeHemisphere = GPS.UTMLat.North        utm.Datum = GPS.UTMDatum.WGS84         txtLatitude = utm.Latitude.ToString         txtLongitude = utm.Longitude.ToString        MsgBox(txtLatitude & "," & txtLongitude)

The txtlatitude variable returned is 3 degrees, 4 minutes and 43.550 seconds
the txtLongitude variable returned is 4 degrees, 11 minutes and 53.319 seconds

but if I use an external website thus

http://rcn.montana.edu/resources/too...4&n=366858&h=N

I get 
Latitude: 3&#176; 19' 4.776" N
Longitude: 4&#176; 26' 12.338" W

I don't understand how I have made an error. Can you help?

A second question, if I may - can the code return Degree Decimal format results?

Thanks in anticipation

Tim

----------


## gep13

Hello TimHaydnJones,

Welcome to the forums!

Just a quick note...

When you are posting code into the forum, can you please remember to surround it in [code][/code] or [HIGHLIGHT=vb][/highlight] tags?  It makes it a lot easier to read.  I have done this for you in your above post.

Gary

----------


## Jenner

> Hi Jenner - I really appreciate the effort you have invested to make this code available and I accept that it is accurate, so I also accept any error is my error but hoping to prevail on your good nature (what a creep, eh)
> 
> OK, although I'm new to this vb .Net lark, I think I have installed the code OK on the grounds that it doesn't crash and it doesn't complain and it does something.
> 
> 
> vb Code:
> Dim utm As New GPS.UTM(340374, 366857, 30)         utm.UTMLatitudeHemisphere = GPS.UTMLat.North        utm.Datum = GPS.UTMDatum.WGS84         txtLatitude = utm.Latitude.ToString         txtLongitude = utm.Longitude.ToString        MsgBox(txtLatitude & "," & txtLongitude)
> 
> The txtlatitude variable returned is 3 degrees, 4 minutes and 43.550 seconds
> ...


Your Northing and Easting are reversed.  In my tool, you use the format:
GPS.UTM(Northing, Easting, Zone)
GPS.UTM(340374, 366857, 30)

but in the web tool, your link shows: 
http://rcn.montana.edu/resources/tools/coordinates.aspx?nav=11&c=UTM&md=83&mdt=NAD83/WGS84&z=30&*e=340374*&*n=366858*&h=N

If you flip the two on the web tool, you'll see you get a result that matches my tool and vice-versa.  :Smilie:

----------


## TimHaydnJones

DOH! - do I feel sheepish or what!

Moving onto my next issue, if I may - Is there a way I can pass in 6x6 digit UK Eastings and Northings, based on the Ordnance Survey National Grid (OSNG)?

The OSNG coordinates (Eastings) 329636, (Northings) 407288 relate to a lat/long of 53.5575437399072, -3.06531429290771 but convert back to a
UTM Easting of 495673 and a UTM Northing of 5934298 (WGS84 Zone 30).
(Reading some documentation from the Ordnance Survey, I guess this is something to do with the fact that the OSNG origin is at Lat 49 degrees North and Long 2 degrees West, which means that my OSNG Northings are too small and (it seems) I can't pass them into your code in their native form)

So, I understand that I can use your code to translate from the UTM to Lat/Long and vice versa but can I use it to convert my 6x6 OSNG co-ordinate to Lat/Long?

----------


## Jenner

As written, I'm afraid not.  Every local/country grid has it's own sets of peculiarities that must be programmed around.  I mean, it's certainly possible to write a conversion, but I'm afraid I'm not that good at math to undertake such a task.  :Embarrassment: 

Back when I found the formulations and decided to write this class up, I looked into writing up routines to convert into the United States system as well... but researching the problem showed me just how immensely complex that would have been since almost every state in our country has it's own "local grid data" with table-loads of conversions required to stitch it all together into the monstrosity known as NAD83.

----------


## TimHaydnJones

Thanks for a rapid response, Jenner and thanks again for the code.
Oh, and by the way, if you think your Maths is questionable, you should see mine :Cry: 
regards

Tim

----------


## goninjago

A word of warning - as you might have gathered from some of the posts in this thread, porting this class to C# using an online converter will screw some things up.  My latitude was off by about 10 miles.  If I ever get a C# version running, I will edit this post with a link to the code.

----------


## lar3ry

Very nice! I downloaded this for two reasons. The first was to learn how to convert UTM to Lat/Long (and vice versa). The second was as an example of a Class. Being a relative newcomer to the whole OOP thing (though I've had many years of programming experience), I wanted to learn more about it.

Quick question. Would this class be a good place to put conversion routines for doing the various lat/long formats (DMS.dd, DM.dd, D.dd)?

Thanks for the code!
Larry

----------


## Jenner

Absolutely!  Classes are as flexible as you need them to be.   :Big Grin:

----------


## Security Geek

We are looking to use this in a research project.  Is there a license agreement for this code?

----------


## Jenner

Nope, freeware as far as I'm concerned.  That's why it's posted on here.

----------


## memento87

I'm afraid your class is extremely accurate and useful.

Also for peeps who be tryin to convert to C#. This is what I did:
1) Create a VB Library Project. 
2) Add the GPS.vb class
3) Compile
4) Start a new C# project
5) Reference the first VB Library Project directly or through the compiled DLL
6) There you can use the class as if it were c#


Anw. Thanks again. You have saved so many lives by posting this code.

----------


## michaelchora

It seems like a simple question, but i just cant seem to work it out. 

I have Eastings, Northings, Hemisphere and the Zone, I can get the class to work and it outputs Latitudes and Logditudes for me, but they are in DMS, but I really need to get the output in Decimal Long/Lat. 

I know im missing something simple. 

this is the code i use to output the Long/Lat, is there a conversion function?




> Dim utm As New GPS.UTM(Northing, Easting, Zone)
>                 utm.UTMLatitudeHemisphere = GPS.UTMLat.South
> 
>                 MyLatitude = utm.Latitude.ToString
>                 MyLongitude = utm.Longitude.ToString


i feel like it should be 




> MyLatitude = utm.Decimal.Latitude.ToString
>                 MyLongitude = utm.Decimal.Longitude.ToString


Or something similar. 

Thanks, Mike

----------


## lar3ry

To get decimal degrees, it's

degrees + (minutes/60) + (seconds/3600)

----------


## lar3ry

To get decimal degrees, it's

degrees + (minutes/60) + (seconds/3600)

----------


## michaelchora

> To get decimal degrees, it's
> 
> degrees + (minutes/60) + (seconds/3600)


I understand the Maths involved, im just unsure of how to implement this. Would i do it in the GPS.vb class or would i do this in my function?

This is a snippet of my code:




> If reader.HasRows Then
>             While reader.Read()
>                 Northing = reader("Northing")
>                 Easting = reader("Easting")
>                 CarNumber = reader("CarNbr")
>                 Zone = reader("Zone")
> 
>                 Dim utm As New GPS.UTM(Northing, Easting, Zone)
>                 utm.UTMLatitudeHemisphere = GPS.UTMLat.South
> ...


It reads the values from a DB then converts them from UTM to Lat Long, but my application needs lat longs in decimal format. Im just unsure of how to do that.

----------


## michaelchora

Dismiss my last question... it seems with a little more obvious thinking i worked it out!




> If reader.HasRows Then
>             While reader.Read()
>                 Northing = reader("Northing")
>                 Easting = reader("Easting")
>                 CarNumber = reader("CarNbr")
>                 Zone = reader("Zone")
> 
>                 Dim utm As New GPS.UTM(Northing, Easting, Zone)
>                 utm.UTMLatitudeHemisphere = GPS.UTMLat.South
> ...

----------


## rfrank5356

Hi Jenner - I have implemented your code to geolocate aerial images - and the locations are only somewhat close. I have a centroid located at  35.44752324, -119.0344939 and the UTM comes out as 11N 3925110.67478774, 314745.859021866 - this is about 373 and 741 meters off. The Lat/Lon are in WGS84, and I am loading the resulting UTM Arcmap using NAD83_UTM_Zone11 as my projection. . Thanks for your help

----------


## Colonel_Korg

I see a lot of posts here requesting *decimal* Lat/Long output instead of ddd mm' ss.ss".

So, I added two read only properties to the class.  Here is the code with the new parts highlighted:

Added two local private double variables...


```
    Public Class UTM
        Private Const k0 As Double = 0.9996


        Private cooLat As New Coordinate(0, CoordinateType.Latitude)
        Private cooLong As New Coordinate(0, CoordinateType.Longitude)
        Private utmDat As UTMDatum = UTMDatum.WGS84
        Private dblNorthing As Double = 0
        Private dblEasting As Double = 0
        Private DecLat As Double = 0.0
        Private DecLong As Double = 0.0
        Private utmL As UTMLat = UTMLat.North
        Private intZone As Int32
```

Added two read only properties...



```
#Region "Properties"
     .
     .
     .
       Public ReadOnly Property DecLatitude() As Double
            Get
                Return DecLat
            End Get
        End Property

        Public ReadOnly Property DecLongitude() As Double
            Get
                Return DecLong
            End Get
        End Property
     .
     .
     .
#End Region
```

Filled in the private class variables with the values in the UTM to LL conversion ....


```
        Public Sub GetLatLong()
                    .
                    .
                    .
            cooLat = New Coordinate(Convert.ToDecimal(lat), CoordinateType.Latitude)

            DecLat = lat

            Dim lon As Double = GetZoneCM() - ((lof1 - lof2 + lof3) / Math.Cos(phi1)) * 180 / Math.PI

            DecLong = lon

            cooLong = New Coordinate(Convert.ToDecimal(lon), CoordinateType.Longitude)
        End Sub
```

My question to the forum here is, has anyone seen a class like this, but for U.S. State-Planes <-> LatLong ?

Korg sends

----------


## nixonb

I have been testing the class and it works well ... thank you.
When I try to convert a UTM coordinate to a Lat/Long coordinate, the latitude is off slightly but the longitude is the same. I am comparing my results to the National Resources Canada website at:
http://webapp.geod.nrcan.gc.ca/geod/...outils/trx.php.

The values I am using are:
N: 5453095.17
E: 704731.39
Zone: 13

The results I get from the class are:
Lat: 49.196384246396228 (49° 11' 46.9833")
Long: -102.18969397760389 (-102° 11' 22.8983")

but the results from the Geodetic site are:
Lat: 49° 11' 47.17321"
Long: -102° 11' 22.89833"
From other samples I tested, it appears that the longitude is always the same but the latitude is always off a bit.

Here is the code I am using:

            Dim utm As New clsGPS.UTM(myNorthing, myEasting, Zone)

            utm.UTMLatitudeHemisphere = clsGPS.UTMLat.North
            lat = utm.Latitude.GetDecimalCoordinate
            lon = utm.Longitude.GetDecimalCoordinate

Am I missing something?
Blair

----------


## edwardgonzalez

I made some changes in the function GetLatLong() that solve the problem in the south hemisphere, added local variable dNorth and dEast :

            Dim dNorth As Double = 0
            Dim dEast As Double = 0
            '  dblNorthing = If(utmL = UTMLat.North, dblNorthing, 10000000 - dblNorthing)
            dNorth = If(utmL = UTMLat.North, dblNorthing, 10000000 - dblNorthing)
            dEast = dblEasting
            Dim arc As Double = dNorth / k0

----------

