Calculate distance flown using Java
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.
caution
This tutorial assumes that you are already familiar Java Maven projects.
#
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 flightUsing 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 filtered out a single flight that fit the route we want (LHR to CDG).
#
Get the picked flight data and calculate distance flownLet's start by creating a new Flight
class, which will fetch the single flight data, and do the calculations.
Let's break down what we did here:
- We parsed the JSON object used in the constructor parameters.
- We fetched the flight of interest using its
icao_address
,departure_scheduled_time
andarrival_scheduled_time
. - We parsed each line of the answer, and then prepared a method
calculateFlightDistance
that calculate the distance between two sets of coordinates, counting the total for all coordinates.
#
Get countries flown over by the flightFor this step to work, we will need to install the Atlas package, and add it to our Maven dependencies.
We can now add an additional method to our Flight
class. We will name it getFlightCountries
:
#
OutputHere is the expected output from our example: