Real-Time Tracking International Space Station (ISS) with Python

Real-Time Tracking International Space Station (ISS) with Python

??When people ask me what I like most about Python ??, it’s very easy to answer: Even Super Interesting Solutions become Super Simple.

?? In today’s article, I'm excited to show you how to use Python to effortlessly track the International Space Station (ISS) in real-time. ??

When I was teaching at ByLearn , I always looked for curious topics and attractive codes to encourage my students to get more involved with Python.

Sometimes, even I was surprised by what could be done, like when I discovered how to track a space station with just a few lines of code! ???

This quickly became my favorite video among the more than 1000 videos recorded. However, since it is only in Portuguese, I decided to write an English version summarizing how you can achieve the same result.

This is what we will be tracking

Let's Get Started: Tracking the ISS with Python

First of all, it’s important to note that, unfortunately, it’s not possible to perform magic with Python (yet ????), so we need a real source to collect the position of the ISS.

In this example, we will use the "Open Notify" API: https://api.open-notify.org/

?? Also, I'll use the requests library to make it easier to use that API, so be sure to install it first:

pip install requests        

Here’s the Basic Code to Get Started:

import requests

iss_position = "https://api.open-notify.org/iss-now.json"

response = requests.get(iss_position)
data = response.json()        

??In this snippet, we import the requests package and send a GET request to Open Notify to get the ISS's location. The response is stored in the "data" variable.

Printing it we will have something like:

{
	'iss_position': {
		'longitude': '-68.1355', 
		'latitude': '49.8846'
	}, 
	'message': 'success', 
	'timestamp': 1716162051
}        
How Python is making me feel right now

As you can see, we can use the key "iss_position" to access the "longitude" and "latitude" values. ??

Here’s how to extract and format these values in Python:

latitude_value = float(data["iss_position"]["latitude"])
longitude_value = float(data["iss_position"]["longitude"])        

?? Note: We convert the values to float because the API returns them as strings.

Understanding Coordinates

Let's explain that compass

??You might notice that the API can return negative values for latitude and longitude. This is because:

  • Negative latitudes indicate positions SOUTH of the equator.
  • Positive latitudes indicate positions NORTH of the equator.
  • Negative longitudes indicate positions WEST of the Prime Meridian.
  • Positive longitudes indicate positions EAST of the Prime Meridia

We often represent coordinates with degrees, two decimal places, and directional symbols (N/S and W/E) ???.

Here’s how to format them in Python:

latitude Symbol = "N" if latitude_value > 0 else "S"
longitude_symbol = "E" if longitude_value > 0 else "W"

latitude = f"{abs(latitude_value):.2f}o{latitude_symbol}"
longitude = f"{abs(longitude_value):.2f}o{longitude Symbol}"        

Putting It All Together

Believe or not, that’s all we need to do to get the current position of the ISS. Test it with:

 print(f"Current ISS position: {latitude}, {longitude}")        

You should see something like:

 >>> Current ISS position: 22.07oN, 18.22oE        
I found you, ISS!

Making It Even Better

?? It's working, but we can make it BETTER with a few simple steps:

  1. Calculate the new position every second
  2. Update the output line with the new values.

The first one can be made with a while loop and the second one using the "end='\r'" property at the print, which moves the cursor to the beginning of the line, making it possible to "update" the line without creating a new one.

Check the full code:

import time
import requests

while True:
  iss_position = "https://api.open-notify.org/iss-now.json"

  response = requests.get(iss_position)
  data = response.json()

  latitude_value = float(data["iss_position"]["latitude"])
  longitude_value = float(data["iss_position"]["longitude"])

  latitude_symbol = "N" if latitude_value > 0 else "S"
  longitude_symbol = "E" if longitude_value > 0 else "W"

  latitude = f"{abs(latitude_value):.2f}o{latitude_symbol}"
  longitude = f"{abs(longitude_value):.2f}o{longitude_symbol}"

  print(f"Current ISS position: {latitude}, {longitude}", end='\r')

  time. Sleep(1)        
Result of our code

??? PS: If you want to check the results and compare if your code is really working, you can use: https://astroviewer.net/iss.

Key Takeaways

?? From this small piece of code, we learn that:

  1. Python is a powerful tool for turning ideas into reality and automating processes.
  2. Many libraries (like requests) are freely available and easy to use.
  3. Leveraging APIs and third-party solutions (like Open Notify) can save you time and effort.

If you enjoyed this article and want to explore more about the wonders of Python, follow me on LinkedIn! And if you really want to connect, visit my profile and Add me as a Connection! ????

#Python #Coding #Space #InternationalSpaceStation #APIs #Tech #Programming #RealTimeData


Ramiro Polverini

Software Engineer Upwardli - Desarrollador, Socio Fundador en Bantics Coop

10 个月

Hey Felipe, is great. I think for phase 2 can integrate the result with Googlemap or OpenStreetMap

要查看或添加评论,请登录

社区洞察

其他会员也浏览了