Calculate distance flown using Python
Let's say we want to be able to calculate the distance that an aircraft flew, and over which countries it flew.
In this example, we will go through the steps necessary to recover the data from a flight and calculate the distance flown during the flight using the Tracking History API.
#
Source codeYou can find and download the source code for this example here.
#
Geo filteringFirst, we will look at a specific airport, from which we will single out a specific flight to analyze.
Let's take CDG airport here. We will look at this GeoJSON representing the area of the airport:
Which is represented like that on a map:
#
Calling the Tracking History API and pick a flightLet's start by installing the request, pandas and colorama packages that will be necessary for this first step to work.
Using the coordinates previously shown, we can filter the API results for the area we want:
- Latitude range:
48.970752,49.041694
need to be South to North, so values can vary from-90
to90
. - Longitude range:
2.481443,2.642431
need to be West to East, so values can vary from-170
to170
.
Let's breakdown what we did here:
- We queried our endpoint around CDG airport during a specific time.
- We parsed the results and generated a Pandas Dataframe from them.
- We filtered out all flights with the route LHR to CDG.
#
Get the picked flight data and calculate distance flownFrom the filtered flights fitting our requirement (LHR to CDG), we pick out the first of those and query the Tracking History endpoint again, this time removing geo limitations. For this work out, we will need to install an additional package: geopy
Let's break down what we did here:
- We selected the first flight in our filtered data
flight_analysed.iloc[0]
, and queried the Tracking History endpoint to get this specific flight using its ICAO address as the API filter. - We generated a Panda Dataframe with the retrieved data.
- We iterate over every data point, and generated an array of coordinates, which we will use to add up the distance between every points. For that we will use the geopy package, that calculates distance between 2 sets of coordinates.
#
Get countries flown over by the flightWe can now make use of the reverse_geocoder package, which is an offline geocoder that will allow us to get useful information from coordinates such as the country.
#
OutputHere is the expected output from our example: