VBScript to convert DD to DMS
Hi,
I am attempting to implement a VBScript to convert the DD obtained from a click/tap (see (http://support.geocortex.com/SupportForums/Thread.aspx?pageid=0&mid=2&ItemID=20&thread=46659) this thread ) to DMS. There could be many points of failure, but I think my basic problem is not knowing how to use a VBScript activity. Here is the error message I get when running the workflow.
There was a workflow error running activity: Exception has been thrown by the target of an invocation. Workflow 'GetCoordinatesLL' failed Unhandled exception: 'Function 'Execute' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. Statement cannot appear within a method body. End of method assumed. 'Int' is not declared. It may be inaccessible due to its protection level. 'Abs' is not declared. It may be inaccessible due to its protection level. 'Int' is not declared. It may be inaccessible due to its protection level. 'Round' is not declared. It may be inaccessible due to its protection level. End of statement expected. End of statement expected. 'End Function' must be preceded by a matching 'Function'. ' in activity '1.19: Get X DMS'.
Here is the script I have put in a VBScript activity, with the input parameters as follows. I might have done the parameters wrong, too, and I have zero experience with VBScript.
dblX is the result of a get longitude activity, which I know works to return a double (because this whole workflow works for DD). It is assigned to myCoord .
The result is called myDMS (a concatenated string) and is assigned to xCoordDMS via the parameters. (I plan to use the same script for X and Y coord with different input/output variables.)
Function convertDMS(myCoord)
Dim degrees
Dim minutes
Dim seconds
Dim min
Dim minInt
Dim sec
Dim secRound
Dim west
Dim myDMS
degrees = Int(myCoord)
If degrees < 0 then
degrees = Abs(degrees)
west = True
End if
minutes = myCoord - degrees
min = minutes * 60
minInt = Int(min)
seconds = min - minint
sec = seconds * 60
secRound = Round(sec,6)
If west = True Then
myDMS = degrees & " d " & minInt " m " & secRound & " W"
Else
myDMS = degrees & " d " & minInt " m " & secRound & " N"
End if
convertDMS = myDMS
End Function
Any ideas would be welcome.
-
I think I've got it after lots of trial and error. Hopefully this helps someone else. It was a combination of not understanding the activity, like I said, and some syntax.
I was missing an ampersand, trying to call a function within a function, and not calling the math functions properly. Here is the finished script that goes inside the VBScript text box:
Dim degrees
Dim deg
Dim minutes
Dim seconds
Dim min
Dim minInt
Dim sec
Dim secRound
Dim west
If myCoord < 0 then
degrees = Math.Abs(myCoord)
west = True
Else
degrees = myCoord
west = False
End if
deg = Math.Floor(degrees)
minutes = degrees - deg
min = minutes * 60
minInt = Math.Floor(min)
seconds = min - minint
sec = seconds * 60
secRound = Math.Round(sec,6)
If west = True Then
myDMS = deg & " d " & minInt & " m " & secRound & " W"
Else
myDMS = deg & " d " & minInt & " m " & secRound & " N"
End if
Return myDMSParameters:
Name=myCoord, Direction= In/Out, Type=Double, Assign To dblX
Name=myDMS, Direction= In/Out, Type=String, Assign To xCoordDMS0 -
Heather,
Thanks for the code. I tried this but found that results took about 30 seconds to return the answer. I replaced your vb code with the following that I found on the internet and the results return much quicker.
Dim DecDegAbs As Decimal = Math.Abs(myCoord)
Dim ReturnValue As String = "'"
Dim DegreeSymbol As String = "°"
Dim MinutesSymbol As String = "’"
Dim SecondsSymbol As String = """"
Dim Degrees As String = Math.Truncate(DecDegAbs) & DegreeSymbol
Dim MinutesDecimal As Decimal = (DecDegAbs - Math.Truncate(DecDegAbs)) * 60
Dim SecondsDecimal As Decimal = (MinutesDecimal - Math.Truncate(MinutesDecimal))
Dim Minutes As String = Math.Truncate(MinutesDecimal) & MinutesSymbol
Dim Seconds As String = String.Format("{0:##.0000}", (SecondsDecimal * 60)) & SecondsSymbol
myDMS = Degrees & " " & Minutes & " " & Seconds
Return myDMSBest Regards,
Rick
0
Du måste logga in om du vill lämna en kommentar.
Kommentarer
2 kommentarer