将纬度/经度转换为十进制度数

时间:2012-05-24 14:20:00

标签: vbscript coordinate-systems coordinate-transformation

我需要将带有纬度的大型txt文件和带有经度的大型txt文件转换为十进制度。

我拥有的数据采用当前格式:

7d44'31.495"W for longitude

41d3'40.313"N for latitude

我需要将这两个文件转换为十进制度(最好使用脚本)。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

对* itudes一无所知,我只能给你一个关于如何的一般概念 解决你的问题:

使用RegExp剪切数据中的数字并以可信公式提供

为了让你对我的建议有信心,我告诉你,我将如何解决 你的任务的两个子问题。

(1)在输入字符串上应用RegExp并计算。

基于此部分

  

从DMS转换为十进制度

     

给定DMS(度,分,秒)坐标,例如W87°43'41“,   使用以下方法将其转换为十进制度数:

Calculate the total number of seconds:
43'41" = (43*60 + 41) = 2621 seconds.
The fractional part is total number of seconds divided by 3600:
2621 / 3600 = ~0.728056
Add fractional degrees to whole degrees to produce the final result:
87 + 0.728056 = 87.728056
Since it is a West longitude coordinate, negate the result.
The final result is -87.728056.

找到here

代码:

  Dim oFmt   : Set oFmt = New cFormat
  Dim oRE    : Set oRE  = New RegExp
  '                 0    1     2     3
  oRE.Pattern = "^([W])(\d+)°(\d+)'(\d+)""$"
  Dim aTests : aTests   = Array( _
      Array("W87°43'41""", -87.728056) _
  )
  Dim aTest
  For Each aTest In aTests
      Dim sInp : sInp     = aTest(0)
      Dim nExp : nExp     = aTest(1)
      Dim oMTS : Set oMTS = oRE.Execute(sInp)
      Dim sMsg
      If 1 <> oMTS.Count Then
         sMsg = oFmt.formatTwo("|{0}| didn't match /{1}/", sInp, oRE.Pattern)
      Else
         Dim sLoLa    : sLoLa    =      oMTS(0).SubMatches(0)
         Dim nDegrees : nDegrees = CDbl(oMTS(0).SubMatches(1))
         Dim nMinutes : nMinutes = CDbl(oMTS(0).SubMatches(2))
         Dim nSeconds : nSeconds = CDbl(oMTS(0).SubMatches(3))
         Dim nRes

'        Calculate the total number of seconds:
'        43'41" = (43*60 + 41) = 2621 seconds.
         nRes = nMinutes * 60 + nSeconds
'        WScript.Echo "***", nRes

'        The fractional part is total number of seconds divided by 3600:
'        2621 / 3600 = ~0.728056
         nRes = nRes / 3600
'        WScript.Echo "***", nRes

'        Add fractional degrees to whole degrees to produce the final result:
'        87 + 0.728056 = 87.728056
         nRes = nDegrees + nRes
'        WScript.Echo "***", nRes

'        Since it is a West longitude coordinate, negate the result.
'        The final result is -87.728056.
         Select Case sLoLa
           Case "W"
             nRes = -nRes
         End Select

         sMsg = oFmt.formatArray(_
              "{0,-12}: R: {1,12:N6} E: {2,12:N6} D: {3,12:N6}" _
            , Array(sInp, nRes, nExp, nRes - nExp) _
         )
      End If
      WScript.Echo sMsg
  Next

输出:

Step00 - conversion a la wikipedia
================================================================
W87°43'41"  : R:   -87,728056 E:   -87,728056 D:     0,000000
================================================================
xpl.vbs: Erfolgreich beendet. (0) [0.08594 secs]

从输入样本开始,我派生(第一次尝试)Regexp 图案

                "   W   87  ° 43  ' 41  "" "
  '                 0    1     2     3
  oRE.Pattern = "^([W])(\d+)°(\d+)'(\d+)""$"

对于您的数据,类似这样的

                "   7  d 44  ' 31.495   ""  W    "
                "  41  d  3  ' 40.313   ""  N    "
  '                 0     1        2        3
  oRE.Pattern = "^(\d+)d(\d+)'(\d+\.\d+)""([WN])$"

可能是合适的。

要获取RegExps组()s捕获的部分,我将进行访问和转换 比赛的子比赛

 Dim sLoLa    : sLoLa    =      oMTS(0).SubMatches(0)
 Dim nDegrees : nDegrees = CDbl(oMTS(0).SubMatches(1))
 Dim nMinutes : nMinutes = CDbl(oMTS(0).SubMatches(2))
 Dim nSeconds : nSeconds = CDbl(oMTS(0).SubMatches(3))

然后它只是根据被盗的算法/公式计算 来自维基百科(参见代码/评论)。

(2)从您的文件中获取数据

如果文件不大,你可以.ReadAll()将它们放入内存中 在多行模式下应用RegExp:

  Dim oFmt : Set oFmt = New cFormat

  Dim oRE  : Set oRE  = New RegExp
  oRE.Global    = True
  oRE.Multiline = True
  '                 0     1        2        3
  oRE.Pattern = "^(\d+)d(\d+)'(\d+\.\d+)""([WN])$"

  Dim sAll : sAll = goFS.OpenTextFile("..\data\f00.txt").ReadAll()
  WScript.Echo sAll

  WScript.Echo oFmt.formatArray( _
      "|{0,-5}|{1,-11}|{2,-11}|{3,-15}|" _
    , Array("LoLa", "Degrees", "Minutes", "Seconds") _
  )

  Dim oMTS : Set oMTS = oRE.Execute(sAll)
  Dim oMT
  For Each oMT In oMTS
      Dim sLoLa    : sLoLa    =      oMT.SubMatches(3)
      Dim nDegrees : nDegrees = CDbl(oMT.SubMatches(0))
      Dim nMinutes : nMinutes = CDbl(oMT.SubMatches(1))
      Dim nSeconds : nSeconds = CDbl(oMT.SubMatches(2))

      WScript.Echo oFmt.formatArray( _
          "|{0,-5}|{1,11:N2}|{2,11:N2}|{3,15:N6}|" _
        , Array(sLoLa, nDegrees, nMinutes, nSeconds) _
      )
  Next

输出:

======================================================
7d44'31.495"W
41d3'40.313"N


|LoLa |Degrees    |Minutes    |Seconds        |
|W    |       7,00|      44,00|      31,495000|
|N    |      41,00|       3,00|      40,313000|
======================================================

here

获取cFormat类