Distance and Bearing: Difference between revisions

m
julia example
No edit summary
m (julia example)
Line 24:
:*   Movable Type Scripts:  [https://www.movable-type.co.uk/scripts/latlong.html Calculate distance, bearing and more between Latitude/Longitude points]
<br><br>
 
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using DataFrames
using CSV
 
const EARTH_RADIUS_KM = 6372.8
const TASK_CONVERT_NM = 0.0094174
const AIRPORT_DATA_FILE = "airports.dat.txt"
 
const QUERY = (latitude = 51.514669, longitude = 2.198581)
 
"""
Given two latitude, longitude pairs in degrees for two points on the Earth,
get distance (nautical miles) and initial direction of travel (degrees)
for travel from lat1, lon1 to lat2, lon2
"""
function haversine(lat1, lon1, lat2, lon2)
dlat = lat2 - lat1
dlon = lon2 - lon1
a = sind(dlat / 2)^2 + cosd(lat1) * cosd(lat2) * sind(dlon / 2)^2
c = 2.0 * asind(sqrt(a))
theta = atand(sind(dlon) * cosd(lat2),
cosd(lat1) * sind(lat2) - sind(lat1) * cosd(lat2) * cosd(dlon))
theta = (theta + 360) % 360
return EARTH_RADIUS_KM * c * TASK_CONVERT_NM, theta
end
 
""" Given latitude and longitude, find `wanted` closest airports in database file csv. """
function find_nearest_airports(latitude, longitude, wanted = 20, csv = AIRPORT_DATA_FILE)
airports = DataFrame(CSV.File(csv))[:, [2, 4, 6, 7, 8]]
airports[!, "d"] .= 0.0
airports[!, "b"] .= 0.0
rename!(airports, [:Name, :Country, :ICAO, :Latitude_deg, :Longitude_deg, :Distance, :Bearing])
for r in eachrow(airports)
distance, bearing = haversine(latitude, longitude, r.Latitude_deg, r.Longitude_deg)
r.Distance, r.Bearing = round(distance, digits = 1), round(bearing, digits = 1)
end
sort!(airports, :Distance)
return airports[1:wanted, [:Name, :Country, :ICAO, :Distance, :Bearing]]
end
 
println(find_nearest_airports(QUERY.latitude, QUERY.longitude))
</syntaxhighlight>{{out}}
<pre>
20×5 DataFrame
Row │ Name Country ICAO Distance Bearing
│ String String String7 Float64 Float64
─────┼───────────────────────────────────────────────────────────────────────────────
1 │ Koksijde Air Base Belgium EBFN 30.6 146.0
2 │ Ostend-Bruges International Airp… Belgium EBOS 31.3 127.0
3 │ Kent International Airport United Kingdom EGMH 33.5 252.4
4 │ Calais-Dunkerque Airport France LFAC 34.4 195.5
5 │ Westkapelle heliport Belgium EBKW 42.6 105.3
6 │ Lympne Airport United Kingdom EGMK 51.6 240.1
7 │ Ursel Air Base Belgium EBUL 52.8 114.4
8 │ Southend Airport United Kingdom EGMC 56.2 274.1
9 │ Merville-Calonne Airport France LFQT 56.3 162.5
10 │ Wevelgem Airport Belgium EBKT 56.4 137.5
11 │ Midden-Zeeland Airport Netherlands EHMZ 57.2 89.5
12 │ Lydd Airport United Kingdom EGMD 58.0 235.2
13 │ RAF Wattisham United Kingdom EGUW 59.0 309.1
14 │ Beccles Airport United Kingdom EGSM 59.3 339.0
15 │ Lille/Marcq-en-Baroeul Airport France LFQO 59.7 146.0
16 │ Lashenden (Headcorn) Airfield United Kingdom EGKH 62.2 250.4
17 │ Le Touquet-Côte d'Opale Airport France LFAT 63.7 200.3
18 │ Rochester Airport United Kingdom EGTO 64.2 261.9
19 │ Lille-Lesquin Airport France LFQQ 66.2 149.2
20 │ Thurrock Airfield United Kingdom EGMT 68.4 271.9
</pre>
 
 
=={{header|Phix}}==
4,104

edits