I just now learned that there is a website that gives information about the ISS through JSON. JSON (pronounced JSON) is a programming language, like Python, which is simply Javascript in a human-readable format. The website has two sections (as far as I know), one for the International Space Station’s location and one for who is on board!
The website shows this: {"timestamp": 1524801325, "iss_position": {"latitude": "48.8868", "longitude": "113.9675"}, "message": "success"} for the location…
And this: {"people": [{"name": "Anton Shkaplerov", "craft": "ISS"}, {"name": "Scott Tingle", "craft": "ISS"}, {"name": "Norishige Kanai", "craft": "ISS"}, {"name": "Oleg Artemyev", "craft": "ISS"}, {"name": "Andrew Feustel", "craft": "ISS"}, {"name": "Richard Arnold", "craft": "ISS"}], "number": 6, "message": "success"} for the crew manifest.
Although we can read that, it still could look cleaner. So naturally, I wanted to implement this into my Python code and see what I can do (Side note: I figured out how to indent my code properly – it adds some grey highlight before and after the code but oh well!).
import json
import turtle
import urllib.request
from turtle import *
import os
os.getcwd()
#print ("Current working dir : %s" % os.getcwd())
url_astros = 'http://api.open-notify.org/astros.json'
url_iss = 'http://api.open-notify.org/iss-now.json'
responsea = urllib.request.urlopen(url_astros)
resulta = json.loads(responsea.read())
response = urllib.request.urlopen(url_iss)
result = json.loads(response.read())
print('People in Space: ', resulta['number'])
people = resulta['people']
for p in people:
print(p['name'], 'Is currently stationed in: ', p['craft'])
location = result['iss_position']
lat = float(location['latitude'])
lon = float(location['longitude'])
print('Latitude: ', lat)
print('Longitude: ', lon)
screen = turtle.Screen()
screen.setup(1200, 480)
screen.setworldcoordinates(-180, -90, 180, 90)
screen.bgpic('map.gif')
screen.register_shape('isssmall.gif')
iss = turtle.Turtle()
iss.shape('isssmall.gif')
iss.shapesize(311, 124)
iss.setheading(90)
iss.penup()
iss.goto(lon, lat)
#longitude goes first because coordinates are (x,y)
Here is what it looks like in a video.
I learned A LOT of new Python code tricks during this project – many of which would have been very helpful in the past. The biggest thing I learned was how to insert images into my Python code. I always tried in my other ones however I could never get them to load!
import json
import turtle
import urllib.request
from turtle import *
import os
os.getcwd()
You may have noticed: 1. There are more imported modules than my usual, and 2. There is a line of code that is not importing anything that I still included in this chunk. I included os.getcwd() because this simple line allowed me to get images into my code. What this (and the next line: #print ("Current working dir : %s" % os.getcwd())) did was show me where my code was happening and, more importantly, where to place my images. I always tried to place my images in the Turtle library or in the Tkinter library, this line told me to put it where I save my code and boom – it worked!
url_astros = 'http://api.open-notify.org/astros.json'
url_iss = 'http://api.open-notify.org/iss-now.json'
responsea = urllib.request.urlopen(url_astros)
resulta = json.loads(responsea.read())
response = urllib.request.urlopen(url_iss)
result = json.loads(response.read())
Now we need to define variables that are used in the “urllib.request” module – what this does is go to the website and grab the data it shows. Since we are dealing with two sites (one for location and one for crew members) we need two sets of the variables. We can’t have two variables with the same name, so, I named the first set with an “a” at the end, for “astronaut.”
The “urllib.request” module (we will call this the url module for ease) needs to know 2 things: The url it is going to, and the response it should be getting. Then we turn this into out resulta and result variables.
Now that our data is stored, we need to display it to the user:
print('People in Space: ', resulta['number'])
people = resulta['people']
for p in people:
print(p['name'], 'Is currently stationed in: ', p['craft'])
Ah yes! Another for loop – what a familiar sight! We use this for loop to display our crew data. We want to know who is on board and what spacecraft they are in (obviously it is the ISS, but if one were to do this across multiple crafts, this part would be more important). Our for loop counts everyone on board, and for every person listed on the website, puts their name and where they are stationed at.
Cool, so that was the main part because I first found out about the crew site only. Once I discovered that there were coordinates, too, I was all over that!
location = result['iss_position']
lat = float(location['latitude'])
lon = float(location['longitude'])
print('Latitude: ', lat)
print('Longitude: ', lon)
The website already called the ISS’s position ‘iss_position’, so we just grab that from the result. Then we make two more variables – latitude and longitude. Normally we see int (integer) values, but we need the numbers after the decimal places so we call them “floats” to ensure that we get everything.
screen = turtle.Screen()
screen.setup(1200, 480)
screen.setworldcoordinates(-180, -90, 180, 90)
screen.bgpic('map.gif')
Back to good ol’ Turtle, now that we can use images properly! Here we are just creating a screen as large as our map (it was 1200 pixels by 480 pixels so we make our screen fit it perfectly – *insert flat earth joke here*). Then we have to tell Turtle where the map coords are, since our coordinates go by -180,xyz’ and all that, we simply put the top values into our Python code. Then we make the background image our map – you know, so we can see it?
screen.register_shape('isssmall.gif')
iss = turtle.Turtle()
iss.shape('isssmall.gif')
iss.shapesize(311, 124)
iss.setheading(90)
Now we create a shape – since we want our ISS to look like the ISS, we grab the picture of it and choose that instead of, say, a square. iss = turtle.Turtle() tells the computer to use the built in Turtle functions – to save us time. The original image size was about 630ish by 480ish but that was way too large. So i mathematically guessed a size until it looked nice – we ended up with 311×124. Setting the heading ensure that we have our ISS oriented in the correct direction.
Finally, all we have to do now is tell our ISS to go to its true location, according to the map.
iss.penup()
iss.goto(lon, lat)
#longitude goes first because coordinates are (x,y)
Again, bringing the pen up prevents a line being drawn on our map, and then we put the pen back down to confirm the ISS’s location.
I learned SO much from this project. The bringing in of images took about an hour and a half to figure out – pretty good for when I spent longer in the past and still failed! Now I can use images in a lot more code – like the modern art code could have been real art (cheating I know)! So far, this may have been the most fun and the most beneficial project I have done so far!