JeanExtreme002 / FlightRadarAPI

:airplane: Unofficial SDK for FlightRadar24 for Python 3 and NodeJS
https://pypi.org/project/FlightRadarAPI/
MIT License
296 stars 60 forks source link

Get the nearest airport to a location or to a city. #56

Closed ptesny closed 10 months ago

ptesny commented 10 months ago

Is your feature request related to a problem? Please describe. Get the nearest airport to a location or to a city. Similar to how we can get flights in a given radius of for a given location.

Is not your feature request related to a problem? Please describe

Additional context

JeanExtreme002 commented 10 months ago

Hi @ptesny ! To get the nearest airport (or even the nearest flight) from a location, you just have to create an Entity and order the list of airports by using the method get_distance_from, this way:

from FlightRadar24 import Entity, FlightRadar24API

location_coords = (34.0937508, -118.3264781)  # Hollywood coords
location = Entity(location_coords[0], location_coords[1])

fr_api = FlightRadar24API()
airports = fr_api.get_airports()

airports.sort(key=lambda airport: airport.get_distance_from(location))

print(airports[0])  # <(KBUR) Burbank Bob Hope Airport - Altitude: 778 - Latitude: 34.200661 - Longitude: -118.358002>
ptesny commented 10 months ago

Thank you @JeanExtreme002 ; I did not realize the Entity object is also exported from the package... How this would be in nodejs? Likely almost the same ? const { FlightRadar24API, Entity } = require("flightradarapi"); and then ...?

          latitude = urlParams.has('lat') ? urlParams.get('lat') : 34.0937508;
          longitude = urlParams.has('lon') ? urlParams.get('lon') : -118.3264781;

          const location = new Entity(latitude, longitude)

          const airports = await frApi.getAirports();

          const _ = require("lodash");

          try { 
            const airports = await frApi.getAirports();

            const nearest_airports = _.sortBy(airports, [function (o) { return o.getDistanceFrom(location); }]);

            return  JSON.stringify(nearest_airports,0,2);
          }
          catch (error) { console.log(error); return error.message; }

and the above code yields seemingly same result:

[
  {
    "latitude": 34.200661,
    "longitude": -118.358002,
    "altitude": 778,
    "name": "Burbank Bob Hope Airport",
    "icao": "KBUR",
    "iata": "BUR",
    "country": "United States"
  },
  {
    "latitude": 34.017735,
    "longitude": -118.449646,
    "altitude": 175,
    "name": "Santa Monica Airport",
    "icao": "KSMO",
    "iata": "SMO",
    "country": "United States"
  },
  {
    "latitude": 33.94252,
    "longitude": -118.406998,
    "altitude": 125,
    "name": "Los Angeles International Airport",
    "icao": "KLAX",
    "iata": "LAX",
    "country": "United States"
  },

]

PS. It could be nice to add it as getting-nearest-airports-for-cities-and-locations similar to the following: https://github.com/JeanExtreme002/FlightRadarAPI/blob/main/python/README.md#getting-the-distance-between-flights-and-airports

JeanExtreme002 commented 10 months ago

Yeah, it seems to be a good idea. Thanks for your report. I'll add it to the readme at the next update. 😊

I'm closing this issue for now.