Tracking the Space Station Live with Python

Tom Clarke
CodeX
Published in
4 min readSep 4, 2021

This is a pretty cool thing to be able to do, the coordinates for the space station are live streamed and freely available to anyone! Of course there’s apps and websites that already have a visual tracker for you, but where’s the fun in that. By coding it yourself you have the freedom to implement it in other projects how you wish, or use it to alert you when it’s visible in the sky!

So where to start with something like this, it is a bit random! Our international space station (ISS) coordinate data can be brought into python with a pull request from an API, and then we need a way to visualise it on a plot. So let’s import the packages we need to do this:

import pandas as pd
import plotly.express as px
import time
from IPython.display import clear_output

You may have noticed that I have imported an IPython package, that is mainly because animating plotly graphs without using Dash is a bit of a nightmare I’ve found, so you’ll see how the ‘clear_output’ method is used later. On that note I have coded this in jupyter notebook, and I recommend using this or google colab if you’re following along, it certainly is easier.

The URL where we can pull the json data holding the location of the ISS is: ‘http://api.open-notify.org/iss-now.json'

So let’s define that as a variable first off:

URL = 'http://api.open-notify.org/iss-now.json'

Now let’s put together a quick function that can pull the data, and also alter it so it is easier for us to use in the plotting functions.

def pullData(url):
df = pd.read_json(url)
df['Latitude'] = df.loc['latitude','iss_position']
df['Longitude'] = df.loc['longitude','iss_position']
df.reset_index(inplace=True)
df = df.drop(['index','messages'])
df = df.drop(0)
return df

So what we’ve done here is read in the json data from our API, and reduced it down to a pandas data frame that only holds the longitude and latitude at the time it’s called.

As you can see it’s very easy to get the longitude and latitude coordinates of the ISS into python. If that’s all you came to see, then there’s no need to continue reading unless you’re interested. However, if you plan to put together a visual aid that shows where the space station currently is, then you will soon see!

We are going to start using plotly now to put together a scatter graph, that only plots one point, the ISS location. Luckily plotly has a built in function that holds a map of earth, so no need to import an image and map it onto a sphere or anything like that! To begin with we will set up the initial state of the graph, and after that we will move into the real-time updates. The first thing we need to do it get a first instance of ISS data, and plot that point.

df = pullData(URL)
fig = px.scatter_geo(df,lat='Latitude',lon='Longitude',
projection='orthographic',
width=900,height=700)

So that gets us a basic graph of the earth and a static ISS point in orthographic view. This ‘projection’ parameter is something you might want to play around with for yourself. Personally I recommend leaving it out and using the default, using orthographic, or the ‘natural earth’ argument. Choose whichever suits you best!

For styling purposes there’s a couple more lines we should add before implementing the real-time updates.

fig.update_layout(margin=dict(l=0,r=0,t=0,b=0))
fig.update_geos(showcoastlines=True,coastlinecolor='white',
showland=False,
showocean=True,oceancolor='DarkBlue',
showcountries=True,countrycolor='black',
resolution=110)

By all means change the styling to how you like it, this just kept it nice and clean for me. That brings us to updating the graph. Since plotly doesn’t have an animation function like matplotlib (to my knowledge), we are just using a ‘while’ loop. In this loop we are going to pull in the latest ISS data, update some of the data of our plot, and show a new one. This is where the IPython ‘clear_output’ function comes in handy, because we can replace the old plot with the new. This is only really an option because rendering these graphs is quite light on the CPU, in other cases this is just too slow. Anyway, it should look a little something like this:

while True:
df = pullData(URL)
clear_output(wait=True)
fig.update_traces(lat=df['Latitude'],lon=df['Longitude'],
marker=dict(color='lime',size=10))
fig.update_geos(projection_rotation=dict(lon=df['Longitude'],
lat=df['Latitude']),
roll=0)
fig.show()
time.sleep(5)

Here, the ‘fig.update_traces’ method is what changes the location of the marker for the ISS. The other method we used ‘fig.update_geos’ is updating the camera perspective so that the camera is always looking over the ISS, rather than half the time it being hidden on the other side of the globe.

If you followed along you should have an output looking something like:

That’s all there is to it, but you could have a lot more fun with it. Add in a notification system when the ISS is near your house, or if you like flask web apps, add this and also embed the live stream footage that’s constantly available on youtube!

Whatever you decide, I hope you found this helpful and interesting!

--

--

Tom Clarke
CodeX
Writer for

I enjoy coming up with fun and interesting python concepts, as well as useful projects. You can also view my articles on Blogger: https://azcoding.blogspot.com/