我需要将一个带有纬度的大 txt 文件和一个带有经度的大 txt 文件转换为十进制度。
我拥有的数据是当前格式:
7d44'31.495"W for longitude
和
41d3'40.313"N for latitude
我需要将这两个文件都转换为十进制度(最好使用脚本)。我怎样才能做到这一点?
我需要将一个带有纬度的大 txt 文件和一个带有经度的大 txt 文件转换为十进制度。
我拥有的数据是当前格式:
7d44'31.495"W for longitude
和
41d3'40.313"N for latitude
我需要将这两个文件都转换为十进制度(最好使用脚本)。我怎样才能做到这一点?
对*itudes一无所知,我只能给你一个关于如何解决你的问题的大致思路:
使用正则表达式从您的数据中删除数字并将它们提供给可信的公式
为了让您对我的建议有信心,我向您展示了我将如何解决您任务的两个子问题。
(1) 对输入字符串应用正则表达式并计算。
基于本节
从 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.
在这里找到
代码:
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]
从输入样本开始,我推导出(第一次尝试)正则表达式模式
" 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() 放入内存并在多行模式下应用正则表达式:
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|
======================================================
从这里获取 cFormat 类