GWR
GWR

Reputation: 2008

Convert DegreesMinutesSeconds to Decimal LatLng

Trying to write a vbscript function to convert coordinates in DMS to decimal format.

38°37'16.662"N, 109°36'5.01"W should translate to 38.621295,-109.601391666667

With my function below, I get the correct numbers but I can't figure out how to determine whether to put the negative symbol before the lng.

latLngDmsToDecimal(38, 37, 16.662) = 38.621295
latLngDmsToDecimal(109, 36, 5.01) = 109.601391666667

Note there is no negative symbol on the second result.

Function latLngDmsToDecimal(byVal d, byVal m, byVal s)
    d = cDbl(d)
    m = cDbl(m)
    s = cDbl(s)

    If d < 0 Then 
        latLngDmsToDecimal = -(s / 3600) - (m / 60) + d
    Else
        latLngDmsToDecimal = (s / 3600) + (m / 60) + d
    End If
End Function

Upvotes: 0

Views: 45

Answers (1)

TinyTheBrontosaurus
TinyTheBrontosaurus

Reputation: 4800

You need to parse the direction. W and S should be negative. Either this should be passed into your function, out the parser should invert the degrees portion

So the second one should read either

latLngDmsToDecimal(109, 36, 5.01, W) = -109.601391666667

Or

latLngDmsToDecimal(-109, 36, 5.01) = -109.601391666667

Upvotes: 1

Related Questions