/
Distance.elm
369 lines (269 loc) · 7.66 KB
/
Distance.elm
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
module Distance exposing (main)
{-| Calculates the shortest distance between the given two locations on Earth.
The locations are defined through the geographic coordinates
latitude and longitude in the unit degree.
<https://ellie-app.com/nwGccLspGGPa1>
Usage
-- Input the start and destination as geographic location points
-- (latitude, longitude)
-}
import Browser
import FormatNumber exposing (format)
import FormatNumber.Locales exposing (Decimals(..), frenchLocale)
import Html exposing (Html, div, h2, input, table, text, tr)
import Html.Attributes exposing (placeholder, style, type_, value)
import Html.Events exposing (onInput)
-- MAIN
main : Program () Model Msg
main =
Browser.sandbox { init = init, update = update, view = view }
-- MODEL
type alias Model =
{ currLat : String
, currLon : String
, destLat : String
, destLon : String
}
init : Model
init =
Model "60.17" "24.95" "40.72" "-74.02"
-- UPDATE
type Msg
= CurrLat String
| CurrLon String
| DestLat String
| DestLon String
update : Msg -> Model -> Model
update msg model =
case msg of
CurrLat currLat ->
{ model | currLat = currLat }
CurrLon currLon ->
{ model | currLon = currLon }
DestLat destLat ->
{ model | destLat = destLat }
DestLon destLon ->
{ model | destLon = destLon }
-- VIEW
getDecVar : String -> Float
getDecVar x =
Maybe.withDefault 0 (String.toFloat x)
calcDist : Model -> String
calcDist mod =
fix2 (distance mod)
fix2 : Float -> String
fix2 x =
format { frenchLocale | decimals = Exact 2, decimalSeparator = "." } x
view : Model -> Html Msg
view model =
div
[ style "height" "400px"
, style "background" "linear-gradient(to bottom, #ccccff 0%, #ffffff 100%)"
, style "margin" "2cm"
]
[ viewInput "text" "Start A latit." model.currLat CurrLat
, viewInput "text" "Start A longit." model.currLon CurrLon
, viewInput "text" "Dest latit." model.destLat DestLat
, viewInput "text" "Dest longit." model.destLon DestLon
, viewResult model
, viewFooter
]
viewInput : String -> String -> String -> (String -> msg) -> Html msg
viewInput t p v toMsg =
input [ type_ t, placeholder p, value v, style "width" "80px", onInput toMsg ] []
viewResult : Model -> Html msg
viewResult model =
div [ style "margin-left" "0.5cm" ]
[ div [ style "color" "green" ] [ h2 [] [ text "Distance Calculator" ] ]
, table [ style "color" "darkCyan", style "font-size" "120%" ]
[ tr [] [ text ("Start Ⓐ Latitude " ++ model.currLat ++ "°") ]
, tr [] [ text ("Start Ⓐ Longitude " ++ model.currLon ++ "°") ]
, tr [ style "color" "blue" ] [ text ("End Ⓑ Latitude " ++ model.destLat ++ "°") ]
, tr [ style "color" "blue" ] [ text ("End Ⓑ Longitude " ++ model.destLon ++ "°") ]
, tr [] [ text ("Distance " ++ calcDist model ++ " km") ]
, tr [] [ text (opInitBearing model) ]
, tr [] [ text (opFinalA model) ]
, tr [] [ text (opBackBearing model) ]
, tr [] [ text (opFinalB model) ]
]
]
opInitBearing : Model -> String
opInitBearing mod =
"Initial Bearing to Ⓑ "
++ fix2 (initBearing mod)
++ "° "
++ heading (initBearing mod)
opFinalA : Model -> String
opFinalA mod =
"Final Bearing to Ⓑ "
++ fix2 (finalBearA mod)
++ "° "
++ heading (finalBearA mod)
opFinalB : Model -> String
opFinalB mod =
"Final Bearing to Ⓐ"
++ fix2 (finalBearB mod)
++ "° "
++ heading (finalBearB mod)
opBackBearing : Model -> String
opBackBearing mod =
"Back Bearing to Ⓐ "
++ fix2 (backBear mod)
++ "° "
++ heading (backBear mod)
-- Footer text
viewFooter : Html Msg
viewFooter =
div
[ style "margin-left" "0.5cm"
, style "margin-top" "5em"
]
[ text "© Jarmo Lammi 2023"
]
distance : Model -> Float
distance mod =
12742
-- The diameter of the Earth (KM).
* asin
(sqrt
(square
(sin
((getDecVar mod.currLat - getDecVar mod.destLat)
* pi
/ 360
)
)
+ cos
(getDecVar mod.currLat * pi / 180)
* cos (getDecVar mod.destLat * pi / 180)
* square
(sin
((getDecVar mod.currLon - getDecVar mod.destLon)
* pi
/ 360
)
)
)
)
initBearing : Model -> Float
initBearing mod =
let
radians : Float -> Float
radians =
\v -> v * pi / 180.0
lat1 : Float
lat1 =
radians (getDecVar mod.currLat)
lat2 : Float
lat2 =
radians (getDecVar mod.destLat)
lon1 : Float
lon1 =
radians (getDecVar mod.currLon)
lon2 : Float
lon2 =
radians (getDecVar mod.destLon)
in
bearCommon lat2 lat1 lon2 lon1
backBear : Model -> Float
backBear mod =
let
radians : Float -> Float
radians =
\v -> v * pi / 180.0
lat2 : Float
lat2 =
radians (getDecVar mod.currLat)
lat1 : Float
lat1 =
radians (getDecVar mod.destLat)
lon2 : Float
lon2 =
radians (getDecVar mod.currLon)
lon1 : Float
lon1 =
radians (getDecVar mod.destLon)
in
bearCommon lat2 lat1 lon2 lon1
bearCommon : Float -> Float -> Float -> Float -> Float
bearCommon fi2 fi1 lm2 lm1 =
let
lat2 : Float
lat2 =
fi2
lat1 : Float
lat1 =
fi1
lon2 : Float
lon2 =
lm2
lon1 : Float
lon1 =
lm1
y : Float
y =
sin (lon2 - lon1) * cos lat2
x : Float
x =
(cos lat1 * sin lat2) - (sin lat1 * cos lat2 * cos (lon2 - lon1))
brn : Float
brn =
atan2 y x
db : Float
db =
if brn < 0.0 then
360.0
else if brn > 2.0 * pi then
-360
else
0.0
in
(brn * 180.0 / pi) + db
finalBearA : Model -> Float
finalBearA mod =
let
brndeg : Float
brndeg =
180.0 + backBear mod
in
if brndeg > 360.0 then
brndeg - 360.0
else
brndeg
finalBearB : Model -> Float
finalBearB mod =
let
brndeg : Float
brndeg =
-180.0 + initBearing mod
-- init bear A - 180
in
if brndeg < 0 then
brndeg + 360.0
else
brndeg
heading : Float -> String
heading g =
if (g > 30) && (g < 75) then
"North-East ↗️"
else if (g > 75) && (g < 105) then
"East ➡️"
else if (g > 105) && (g < 150) then
"South-East ↘️"
else if (g > 150) && (g < 210) then
"South ⬇️"
else if (g > 210) && (g < 255) then
"South-West ↙️"
else if (g > 255) && (g < 285) then
"West ⬅️"
else if (g > 285) && (g < 330) then
"North-West ↖️"
else if (g > 330) && (g < 360) then
"North ⬆️"
else if (g > 0) && (g < 30) then
"North ⬆️"
else
"⁉"
square : Float -> Float
square x =
x * x