【问题标题】:How To Convert DMS To DD (Co-Ordinates) With VB.NET如何使用 VB.NET 将 DMS 转换为 DD(坐标)
【发布时间】:2014-01-05 22:40:43
【问题描述】:

目前,我在 VB.NET 中将 DMS 转换为 DD 非常困难,经过无休止的搜索并且没有运气,我打开了这个 Q,希望其他用户遇到这个问题并希望在过去!

我正在尝试将 N51°4.212'、E0°12.905' 的 DMS 转换为 DD。

阅读此问答后: Convert GPS Degrees Minutes Seconds to Decimal Degrees

我有这个函数,它抛出了完全不正确的 DD 坐标(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

&然后阅读此页面:http://simoncodersoftware.wordpress.com/2013/04/09/convert-dms-to-dd-using-vb-net/

这让我想到了这个功能:

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

返回无效坐标:51.1255, 0045138

调用这两个函数: DMS_To_DD(51, 4, 212) DMS_To_DD(0, 12, 905)

我在这里出错的任何想法?这个问题快把我逼疯了!

【问题讨论】:

    标签: vb.net coordinates geography


    【解决方案1】:

    第一个函数使用了一个完全错误的公式。而第二个,该函数需要一个整数分钟,或者换句话说,将十进制分钟转换为整数。

    调用两个函数:DMS_To_DD(51, 4, 212) DMS_To_DD(0, 12, 905)

    您对坐标值表示的理解也是错误的。 N51°4.212' 不等于 51 度 4 分 212 秒。 E0°12.905' 0 度 12 分 905 秒也是如此。

    更新

    抱歉,前面的函数只适用于 +ve 度数。我已经更改了公式以正确处理 DMS 值的 NSWE,但代价是函数的额外参数 nswe 以确定返回的十进制度数的符号。试试这个:

    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
    

    例如:

    要转换 N54° 6.279':

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

    要转换 W4° 44.362':

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

    希望这会有所帮助。

    【讨论】:

    • 谢谢,这很好用,但后来它在这样的事情上失败了:N54°6.581',W4°43.638' - 这把我带到了海里!任何想法为什么会发生这种情况?
    • 另外,刚刚看到您的修改,如果这些不是度、分、秒 (E0°12.905')。如何将这种特定格式转换为 DD?
    • 如果我没有理解,我的意思是,例如你是怎么从 51, 4.212 得到 51, 4, 12.72
    • 西经和南纬 -ve 度。所以你必须通过 -ve 度。例如:对于 W4°43.638' --> DMS_To_DD(-4, 43.638, 0).
    • N51°4.212' 表示度数=51,分钟数=4.212。一分钟的 0.212 等于 12.72 秒。所以 4.212 分钟等于 4 分 12.72 秒。更简单的例子是 5.5 分钟。小数部分 (0.5) 是半分钟,因此将其表示为分和秒,它等于 5 分 30 秒。 1 分钟 = 60 秒。 0.5 分钟 = 30 秒。 0.25 分钟 = 15 秒。
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 2021-06-26
    • 2011-09-21
    • 2021-11-27
    • 2011-10-15
    • 1970-01-01
    • 2021-05-29
    • 2022-01-04
    相关资源
    最近更新 更多