Question

I am struggling very much at the moment to convert DMS to DD within VB.NET & after searching endlessly & having no luck I've opened this Q in hopes that another user has come across this issue & resolved it hopefully in the past!

I am trying to convert a DMS of N51°4.212', E0°12.905' to DD.

After reading this Q & the answer: Convert GPS Degrees Minutes Seconds to Decimal Degrees

I had this function which threw out completely incorrect DD co-ordinates (50.9298, -0.215083333333333):

Private Function DMS_To_DD(ByVal Degrees As String, ByVal Mins As String, ByVal Secs As String) As String
        Dim TmpMins As String = CStr(Mins) & "." & Secs
        Return CInt(Degrees) + (CDbl(TmpMins) / 60) * -1
    End Function

& then read this page: http://simoncodersoftware.wordpress.com/2013/04/09/convert-dms-to-dd-using-vb-net/

That lead me to this function:

Private Function DMS_To_DD(ByVal Degrees As String, ByVal Mins As String, ByVal Secs As String) As String
        Dim TotalSecs As Integer = CInt(Mins) * 60 + Secs
        Dim Total_2 As Double = TotalSecs / 3600
        Dim X1 As String = CStr(CInt(Degrees) & CStr(Total_2))
        If X1.StartsWith("-") Then
            Return X1.Substring(0, 3) & X1.Substring(4, 5)
        Else
            Return X1.Substring(0, 2) & X1.Substring(3, 5)
        End If
    End Function

Which returned invalid co-ordinates: 51.1255, 0045138

Both functions being called with: DMS_To_DD(51, 4, 212) DMS_To_DD(0, 12, 905)

Any ideas where I am going wrong here? This issue is driving me crazy!

Was it helpful?

Solution

The first function is using a totally wrong formula. Whereas the second one, the function is expecting an integer minutes, or in other words, converts the decimal minutes to integer.

Both functions being called with: DMS_To_DD(51, 4, 212) DMS_To_DD(0, 12, 905)

Also your understanding of coordinates value representation is wrong. N51°4.212' is not equal to 51 degrees, 4 minutes and 212 seconds. Same goes with E0°12.905' <> 0 degrees, 12 minutes and 905 seconds.

UPDATES

Sorry, the previous function only work for +ve degrees. I have changed the formula to take care of NSWE of DMS value properly at the cost of an extra parameter nswe to the function to determine sign of the returned decimal degrees. Try this:

Private Function DMS_To_DD(nswe As String, ByVal deg As UInteger, ByVal min As Double, ByVal sec As Double) As Double
    ' throw exception if min and/or sec are negative values
    If min < 0 Or sec < 0 Then
        Throw New ArgumentOutOfRangeException("min and/or sec", "Argument must be a positive value.")
    End If

    Dim sign As Integer

    ' nswe checking below only considers 's' or 'w' value 
    ' in order to determine -ve degrees. 
    ' 'n', 'e' or any invalid string will be default to +ve degrees.
    Select Case nswe.ToUpper
        Case "S", "W" : sign = -1
        Case Else : sign = 1
    End Select

    Return sign * (deg + (min * 60 + sec) / 3600)
End Function

E.g:

To convert N54° 6.279':

DMS_To_DD("N", 54, 6.279, 0) ' returns 54.10465

To convert W4° 44.362':

DMS_To_DD("W", 4, 44.362, 0) ' returns -4.73936666666667

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top