 |
|
 |
Ghobashi Level: Sage

 Registered: 28-02-2003 Posts: 64
|
Please let me open this code with VB.Net
Hello
I have a text file with alot of observations in Degree minutes seconds
I want to develope the attached code so as to read from the file and then convert it to decimal format
can you help me?
The text file is also attached with name of "coordinates"
Private Function invert() As Double
' Declare the variables to be double precision floating-point.
Dim degrees As Double
Dim minutes As Double
Dim seconds As Double
Dim sixarc As String
sixarc = "10° 27' 36"""
' Set degree to value before "°" of Argument Passed
degrees = Val(Left(sixarc, InStr(1, sixarc, "°") - 1))
' Set minutes to the value between the "°" and the "'"
' of the text string for the variable Degree_Deg divided
' 60. The Val function converts the text string to a number.
minutes = Val(Mid(sixarc, InStr(1, sixarc, "°") + 2, _
InStr(1, sixarc, "'") - InStr(1, sixarc, _
"°") - 2)) / 60
' Set seconds to the number to the right of "'" that is
' converted to a value and then divided by 3600.
seconds = Val(Mid(sixarc, InStr(1, sixarc, "'") + _
2, Len(sixarc) - InStr(1, sixarc, "'") - 2)) _
/ 3600
MsgBox degrees + minutes + seconds
End Function
|
____________________________
with Respect
|
|
23-07-2007 at 11:48 PM |
|
|
stickleprojects Level: Moderator

 Registered: 09-09-2002 Posts: 1060
|
Re: Please let me open this code with VB.Net
Hi,
Just saw this message.
I've rewritten the conversion code into simpler steps to show.
Paste the code below into a module and call the test1 routine to see the functions in action.
Hope they help
Kieron
Option Explicit
Sub main()
End Sub
Sub test1()
Dim m As Integer, d As Integer, s As Integer
Dim sec As Double
Dim strSec As String
strSec = "40 20 50"
sec = DegToDec(strSec)
'Debug.Print sec
If DecToDegMinSec(sec, d, m, s) Then
Debug.Print d, m, s
Else
Debug.Print "Failed"
End If
End Sub
Public Function DegToDec(strDegMinSec As String) As Double
' Convert the given string in dd mm ss format into decimal
' Split into the 3 components
Dim ar() As String
ar = Split(strDegMinSec, " ")
Dim deg As Integer
Dim min As Integer
Dim sec As Integer
deg = Val(ar(0))
min = Val(ar(1))
sec = Val(ar(2))
DegToDec = DegMinSecToDec(deg, min, sec)
End Function
Private Function DegMinSecToDec(deg As Integer, min As Integer, sec As Integer) As Double
Dim dblResult As Double
Dim dbl1Second As Double
dbl1Second = 1 / 60
dblResult = deg + (min * dbl1Second) + (sec * (dbl1Second * dbl1Second))
DegMinSecToDec = dblResult
End Function
Public Function DecToDegMinSec(dblDec As Double, ByRef deg As Integer, ByRef min As Integer, ByRef sec As Integer) As Boolean
' Returns OK if done
Dim strDec As String
Dim r As Double
Dim dbl1Sec As Double
strDec = CStr(dblDec)
dbl1Sec = 1 / 60
If InStr(strDec, ".") > 0 Then
' Get the full degrees
deg = Val(Left(strDec, InStr(strDec, ".") - 1))
' Get the mins (how many times in secs)
r = Val(Mid$(strDec, InStr(strDec, ".")))
r = r * 60
min = Int(r)
' Get secs using remainder after minutes have been removed
r = r - (min)
sec = Round(r * 60, 0)
Else
deg = Val(strDec)
min = 0
sec = 0
End If
DecToDegMinSec = True
End Function
|
____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)
|
|
10-09-2007 at 12:35 PM |
|
|
|
|
 |
 |