Now that I know how to use images in Python I’ve tackled the “Where is the space station?” lesson of Code Club projects as a) I will be teaching the lesson at some point and its always good to practise the lesson before teaching it and b) I want to make it work using Python 3 on my Mac and not via Trinket (not there is is anything wrong with Trinket, it is a cool website!).
Code Club do offer a version of the files for offline use, but I prefer trying to figure out what needs changing/getting it to work myself instead of relying on others work as I think I learn better that way.
I’ve modified the Python so that it shows the current location of the International Space Station (ISS) and when it will pass over Manchester, United Kingdom:
———
#geektechstuff
#!/bin/python3
#Original material at https://codeclubprojects.org/en-GB/python/iss/
#import required libraries
import json,turtle,urllib.request,time
#opens JSON file containing ISS crew information
url = ‘http://api.open-notify.org/astros.json’
response = urllib.request.urlopen(url)
result = json.loads(response.read())
#opens JSON file containing ISS location information
url2 = ‘http://api.open-notify.org/iss-now.json’
response2 = urllib.request.urlopen(url2)
result2 = json.loads(response2.read())
#prints information on who is on the ISS
print(‘People in space:’, result[‘number’])
people = result[‘people’]
for p in people:
print(p[‘name’],”in”,p[‘craft’])
#collects information from location JSON
location = result2[‘iss_position’]
lat = location[‘latitude’]
lat = float(lat)
lon = location[‘longitude’]
lon = float(lon)
print(‘ISS current location:’)
print(‘Latitude:’,lat)
print(‘Longitude:’,lon)
#shows a map of planet Earth
screen = turtle.Screen()
screen.setup(720, 360)
screen.setworldcoordinates(-180, -90, 180, 90)
screen.bgpic(‘map.gif’)
#creates a Turtle for ISS
screen.register_shape(‘iss.gif’)
iss = turtle.Turtle()
iss.shape(‘iss.gif’)
iss.setheading(90)
#set location for ISS turtle
iss.penup()
iss.goto(lon, lat)
#set marker location for Manchester, UK
lat_man = 53.483959
lon_man = -2.244644
location_man = turtle.Turtle()
location_man.penup()
location_man.color(‘yellow’)
location_man.goto(lon_man,lat_man)
location_man.dot(5)
location_man.hideturtle()
#when will ISS pass over Manchester, UK
url_man = ‘http://api.open-notify.org/iss-pass.json?lat=53.48&lon=-2.244’
response_man = urllib.request.urlopen(url_man)
result_man = json.loads(response_man.read())
over_man = result_man[‘response’][1][‘risetime’]
#print on map when ISS will pass over Manchester, UK
style = (‘Arial’,6,’bold’)
location_man.write(time.ctime(over_man),font=style)
———
Original Code Club project:
https://codeclubprojects.org/en-GB/python/iss/
My GitHub containing the relevant files:
https://github.com/geektechdude/Python_ISS_Location
Trinket: