How to Track an Airplane with Python
--
With all the hype around Jack Sweeny tracking the private jet of billionaire Elon Musk, along with a whole host of other billionaires and celebrities including Bill Gates, Mark Zuckerburg and others. You may very well be wondering how this is possible, well today we will be creating a very simple flight tracker using Python and The Opensky-Network API.
Getting Started
To get started, first you will need a plane to track. In order to do this you will need to obtain the callsign of the aircraft. A callsign is a unique identifier given to each plane that allow us to uniquely identify it. For the sake of this tutorial I will be using the callsign of Elon Musk’s private jet, which we know to be N628TS. Now that we have this, I will input it into the Opensky Network Aircraft Database this will allow us to obtain the Model S hex code of the plane. This is what we will use to get the longitude and latitude of the plane. In this case the hex code is: a835af.
Making requests to the Opensky-Network API
Now that we have the hex code needed, we can start making requests to the Opensky-Nework API. We can do this with the help of the requests module in Python.
import requests
data = requests.get("https://opensky-network.org/api/states/all?time=0&icao24=a835af")
print(data)
Most likely you will get a response that looks something like this
{'time': 1644596111, 'states': None}
This means that the jet is grounded, if the jet were to be flying the response you get would look something that looks similar to this:
{'time': 1644596206, 'states': [['399184', 'VLJ945S ', 'France', 1644596206, 1644596206, 0.7225, 49.7943, 8839.2, False, 251.49, 139.73, 0, None, 8846.82, '6310', False, 0, 0]]}
Most of the response you can ignore; for this tutorial the part we are interested in is the 0.7225 and the 49.7943 as these represent the longitude and latitude of the plane. Lets expand the script:
import requestsdata = requests.get("https://opensky-network.org/api/states/all?time=0&icao24=40097f").json()if data["states"] != None: print(f"The longitude of the plane is: {data['states'][0][5]}") print(f"The latitude of the plane is: {data['states'][0][6]}")else: print("The plane is currently on the ground!")print("The plane is currently on the ground!")
Great! we can now get the longitude and latitude of the plane; as well as a way to detect if the plane is grounded or in the air. The next step is to display the planes route in some meaningful way. For this I will make use of folium to map the longitude and latitude onto a world map:
import requestsimport timeimport folium
def generate_map(points):
map = folium.Map(location=[0,0], zoom_start=2)
folium.PolyLine(points, color='black').add_to(map)
map.save("map.html")points = []while True:
data = requests.get("https://opensky-network.org/api/states /all?time=0&icao24=HEX_OF_PLANE_YOU_WANT_TO_TRACK").json() if data["states"] != None:
points.append((data['states'][0][6], data['states'][0][5])) generate_map(points) else:
print("The plane is currently on the ground!") time.sleep(60)
This code will update the line every 1 minute with the planes new location!
Congrats! You have just build a simple flight tracker with Python!