1, Foreword
- Open Notify is an open source project designed to provide a simple programming interface for some of NASA's outstanding data.
- The authors of open-notify.org did some work to obtain raw data and convert it into API s related to space and spacecraft.
- Now through this interface, we will get the location of the international space station and draw it on the map in real time.
- In order to achieve this goal, ISS must be installed first_ Info:
pip install ISS-Info
2, Map initialization
- In order to show the path of the international space station in real time, a curve needs to be drawn by using turtle, so you can create a turtle canvas and set the background as earth:
import ISS_Info import turtle import time import json import urllib.request screen = turtle.Screen() screen.setup(720, 360) screen.setworldcoordinates(-180, -90, 180,90) screen.bgpic("map.png") screen.bgcolor("black") screen.register_shape("isss.gif") screen.title("Real time ISS tracker") iss = turtle.Turtle() iss.shape("isss.gif")
- The effects are as follows:
3, Get the number of people on the space station
- If we can know the number of astronauts on the space station, we can track the international space station more accurately. Fortunately, open notify does provide such an interface.
- In order to obtain the number of people information, you must request the data from the following interfaces and write the corresponding astronaut's name in the upper left corner:
http://api.open-notify.org/astros.json
- Implementation code:
astronauts = turtle.Turtle() astronauts.penup() astronauts.color('black') astronauts.goto(-178,86) astronauts.hideturtle() url = "http://api.open-notify.org/astros.json" response = urllib.request.urlopen(url) result = json.loads(response read()) print("There are currently " + str(result ["number"]) + " astronauts in space:") print("") astronauts.write("People in space: " + str(result["number"]), font=style) astronauts.sety(astronauts.ycor() - 5) people = result["people"] for p in people: print(p["name"] + " on: " + p["craft"]) astronauts.write(p["name" ] + "on:" + p["craft"], font=style) astronauts.sety(astronauts.ycor() - 5)
- The effects are as follows:
4, Draw station location
- In order to draw the real-time position of the space station, it is necessary to request the position information of the space station. The requested interface is:
http://api.open-notify.org/iss-now.json
- However, the author encapsulates it into a function, and we call ISS directly_ current_ LOC to obtain the location of the international space station:
while True : location = ISS_Info.iss_current_loc() lat = location['iss_ position']['latitude'] lon = location['iss_ position']['longitude'] print("Position: \n latitude: {}, longitude: {}" .format(lat, lon)) pos = iss.pos() posx = iss.xcor() if iss.xcor() >= (179.1): ### Stop drawing at the right edge of iss.penup() ### the screen to avoid a iss.goto(float(lon), float(lat)) ### horizontal wrap round line time.sleep(5) else: iss.goto(float(lon), float(lat)) iss.pendown() time.sleep(5)
- We can also mark our current position to see the distance from the international space station and the point in time (UTC) when the space station passes over you.
# Shenzhen lat = 112.5118928 lon = 23.8534489 prediction = turtle.Turtle() prediction.penup() prediction.color('yellow') prediction.goto(lat, lon) prediction.dot(5) prediction.hideturtle() url = 'http://api.open-notify.org/iss-pass.json?lat=' + str(lat-90) + '&lon=' + str(lon) response = urllib.request.urlopen(url) result = json.loads(response.read()) over = result ['response'][1]['risetime'] prediction.write(time.ctime(over), font=style)
- However, it is worth noting that the latitude calculation of iss-pass.json interface must be within - 90 to 90, so the latitude of Shenzhen needs to be subtracted by 90.
- The final effect is as follows: