Calculate the Farthest Flight Arriving at a Specific Airport

This guide demonstrates how to use the Flight Radar SDK to find the farthest flight currently en route to a specific airport (in this case, Stockholm Arlanda Airport, ESSA).

Prerequisites

  • An API token from FlightRadar24.

  • The flight-radar and geopy libraries installed.

How It Works

The script is divided into three main steps:

  1. Retrieve Arriving Flights: It first queries the FlightRadar24 API to get a list of all flights that are currently inbound to the specified airport (ESSA).

  2. Get Airport Coordinates: It then fetches the geographical coordinates (latitude and longitude) of the destination airport.

  3. Calculate and Compare Distances: Finally, it iterates through each flight, calculates the great-circle distance from the flight’s current position to the destination airport, and identifies which flight is the farthest away.

Step-by-Step Breakdown

Step 1: Retrieve Flights Arriving at ESSA

The script sends a request to the API, filtering for flights with a destination of ESSA.

request = LiveFlightPositionRequest(
    airports=[
        AirportWithDirection(
            airport='ESSA',
            direction=Direction.INBOUND,
        )
    ]
)
response = service.get_live_flight_positions(request)

Step 2: Retrieve Airport Coordinates for ESSA

To calculate the distance, we need the exact coordinates of the destination.

airport_response = service.get_airports('ESSA')
destination_coords = (airport_response.lat, airport_response.lon)

Step 3: Calculate the Farthest Flight

The script uses the geopy library to calculate the distance and finds the flight with the maximum distance from ESSA.

farthest_flight = None
max_distance = 0

for flight in sorted_flights:
    origin_coords = (flight.lat, flight.lon)
    distance = calculate_distance(origin_coords, destination_coords)
    if distance > max_distance:
        max_distance = distance
        farthest_flight = flight

Complete Example

Here is the complete code for the example. You can run this script to see it in action.

from flight_radar.enums.enums import Direction
from flight_radar.factory import get_flight_radar_client
from flight_radar.models import LiveFlightPositionRequest
from geopy.distance import great_circle

from flight_radar.models.common import AirportWithDirection

# Define your API token and the endpoint URLs
API_TOKEN = 'your_api_token_here'
BASE_URL = 'https://fr24api.flightradar24.com/api'

# Get the flight radar service
service = get_flight_radar_client(
    base_url=BASE_URL,
    api_key=API_TOKEN,
)


### Step 1: Retrieve Flights Arriving at ESSA
# Define the query parameters
request = LiveFlightPositionRequest(
    airports=[
        AirportWithDirection(
            airport='ESSA',
            direction=Direction.INBOUND,
        )
    ]
)

# Call the API to retrieve flights
try:
    response = service.get_live_flight_positions(
        request,
    )

    # Filter out flights without an ETA
    flights_with_eta = [flight for flight in response if flight.eta is not None]
    # Order flights by ETA
    sorted_flights = sorted(flights_with_eta, key=lambda x: x.eta.timestamp() if x.eta else 0)
    print('Flights arriving at ESSA:')
    for flight in sorted_flights:
        print(flight)

except Exception as e:
    print(f'Error retrieving flights: {e}')
    exit()


# Step 2: Retrieve airport coordinates for ESSA
try:
    airport_response = service.get_airports('ESSA')
    destination_coords = (airport_response.lat, airport_response.lon)
    print('\nESSA Coordinates:')
    print(destination_coords)
except Exception as e:
    print(f'Error retrieving airport data: {e}')
    exit()


# Step 3: Calculate the Farthest Flight Using Great Circle Distance
# Function to calculate the great circle distance
def calculate_distance(coord1, coord2):
    return great_circle(coord1, coord2).kilometers


# Initialize variables to track the farthest flight
farthest_flight = None
max_distance = 0

# Calculate the distance for each flight
for flight in sorted_flights:
    origin_coords = (flight.lat, flight.lon)  # Current position of the flight
    # Calculate distance from the flight's current position to ESSA
    distance = calculate_distance(origin_coords, destination_coords)
    if distance > max_distance:
        max_distance = distance
        farthest_flight = flight

# Display the result
if farthest_flight:
    print(
        f'\nThe farthest flight is from {farthest_flight.orig_icao} to ESSA with a distance of {max_distance:.2f} km.'
    )
else:
    print('No flights found.')