-
Notifications
You must be signed in to change notification settings - Fork 0
/
maidenhead_distance.au3
178 lines (133 loc) · 4.61 KB
/
maidenhead_distance.au3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
; 27.09.2021 - Ferdinand Marx - GFMSoft - www.GFMSOFT.de
; This script converts MAIDENHEADLOCATOR to LATITUDE and LONGTITUDE and calculates the distance.
; Result is rounded
; It uses the haversine formula. - https://en.wikipedia.org/wiki/Haversine_formula
;~ .:Information:.
;~ 1. First you have to declare your own locator.
;~ 2. For this example our own locator is IO94GC - see $ownlocator
;~ 3. Leave $r and $mathpi as it is
;~ 4. Just call the function "Calcdistance" with a Maidenheadlocator-String !MUST HAVE 6 Characters!
;Vars
Global $qthdistance, $ownlocator, $mathpi, $r
$r = 6371000 ; Radius of earth - usually i had this in the func - defined at every call - but this is obviously a const so it goes to global
$mathpi = 3.14159 ; Thats PI - Used for some Math later
; PLEASE INSERT YOUR MAIDENHEAD-LOCATOR HERE
$ownlocator = "IO94GC"
;~ Example
MsgBox(64, "Info", "Distance to IO94GC is " & calcdistance("JN63EV") & " km !")
;This function converts Maidenheadlocator to LAT LON coords
Func convertlocator($locator)
Local $latitude, $longtitude, $array, $workarray, $asciiarray, $worklocator
Local $lat_step1, $lat_step2, $lat_step3
Local $lon_step1, $lon_step2, $lon_step3, $lon_step4
If StringLen($locator) <> 6 Then
ConsoleWrite("Targetlocator corrupt - cant calculate!" & @CRLF)
Return 0
EndIf
$worklocator = StringTrimLeft($locator, 4)
$worklocator = StringLower($worklocator)
$locator = StringTrimRight($locator, 2)
$locator = $locator & $worklocator
$asciiarray = StringToASCIIArray($locator)
$workarray = StringSplit($locator, "")
If IsArray($workarray) = False Then
ConsoleWrite("ERROR - WORKARRAY - FALSE" & @CRLF)
Return 0
EndIf
If $workarray[0] <> 6 Then
ConsoleWrite("ERROR - WORKARRAY <> 6" & @CRLF)
Return 0
EndIf
;step 1 - find ascii char for 2th char in locator code
$lat_step1 = $asciiarray[1]
$lat_step1 = $lat_step1 - 65
$lat_step1 = $lat_step1 * 10
;step2 - get number of position 4
$lat_step2 = $workarray[4]
;step 3 - find ascii char for 6th char of locator
$lat_step3 = $asciiarray[5]
$lat_step3 = $lat_step3 - 97
$lat_step3 = $lat_step3 / 24
$lat_step3 = $lat_step3 + (1 / 48)
$lat_step3 = $lat_step3 - 90
;step 4 - all together STEP1 + STEP2 + STEP3
$latitude = $lat_step1 + $lat_step2 + $lat_step3
;~ ___________
;~ LONGTITUDE
;step1 - Find the ASCII charachter code for the 1st character of the locator code
$lon_step1 = $asciiarray[0]
$lon_step1 = $lon_step1 - 65
$lon_step1 = $lon_step1 * 20
;step2 - Get number from position 3
$lon_step2 = $workarray[3]
$lon_step2 = $lon_step2 * 2
;step3 - Find the ASCII charachter code for the 5th character of the locator code
$lon_step3 = $asciiarray[4]
$lon_step3 = $lon_step3 - 97
$lon_step3 = $lon_step3 / 12
$lon_step3 = $lon_step3 + (1 / 24)
;step4 - Add results A, B and C then deduct 180
$longtitude = ($lon_step1 + $lon_step2 + $lon_step3) - 180
Return Round($latitude, 3) & "," & Round($longtitude, 3)
EndFunc ;==>convertlocator
;This function calculates the distance of two qth's
Func calcdistance($qthdistance)
;checking for inputerrors
If StringLen($qthdistance) <> 6 Or $qthdistance = "" Then
Return 0
EndIf
;this is for users trying to crash your application
If $qthdistance = " " Then
Return 0
EndIf
;Define local vars
Local $lat1, $lat2, $lon1, $lon2, $point1, $point2
Local $phi1, $phi2, $a, $c, $d
; delta = Δφ
Local $delta
; spectral = Δλ
Local $spectral
;LAT and LON of Point 1
;This is always your position
$point1 = convertlocator($ownlocator)
$point1 = StringSplit($point1, ",")
;Check for errors
If IsArray($point1) = False Then
Return 0
EndIf
$lat1 = $point1[1]
$lon1 = $point1[2]
;LAT and LON of Point 2
$point2 = convertlocator($qthdistance)
$point2 = StringSplit($point2, ",")
;Check for errors
If IsArray($point2) = False Then
Return 0
EndIf
$lat2 = $point2[1]
$lon2 = $point2[2]
$phi1 = $lat1 * $mathpi / 180
$phi2 = $lat2 * $mathpi / 180
$delta = ($lat2 - $lat1) * $mathpi / 180
$spectral = ($lon2 - $lon1) * $mathpi / 180
$a = Sin($delta / 2) * Sin($delta / 2) + Cos($phi1) * Cos($phi2) * Sin($spectral / 2) * Sin($spectral / 2)
$c = 2 * _atan2(Sqrt($a), Sqrt(1 - $a))
$d = $r * $c
Return Round($d / 1000, 0)
EndFunc ;==>calcdistance
;This function is needed for the distance calculation of two qth's
Func _atan2($y, $x)
If $x > 0 Then
Return ATan($y / $x)
ElseIf $x < 0 And $y >= 0 Then
Return ATan($y / $x) + ACos(-1)
ElseIf $x < 0 And $y < 0 Then
Return ATan($y / $x) - ACos(-1)
ElseIf $x = 0 And $y > 0 Then
Return ACos(-1) / 2
ElseIf $x = 0 And $y < 0 Then
Return -ACos(-1) / 2
Else
Return 0
EndIf
EndFunc ;==>_atan2