Setting Up The World Map
Use turtle.screen() function to get access to the screen, then use screen.setup() to set the size and position of the output window. Use screen.setworldcoordinates() function to set the coordinates of all 4 corners on x, y-axis so that when iss reach out from reaches they appear again from another edge.
Python3
screen = turtle.Screen() screen.setup( 1280 , 720 ) screen.setworldcoordinates( - 180 , - 90 , 180 , 90 ) |
Set map as background pic using screen.bgpic() function and set iss image as turtle shape using screen.register_shape() function. Use it as an object and assign it as a shape using iss.shape() function, then set the angle of shape using iss.setheading() function. iss.penup() function indicates that their drawings. Thus, the turtle stops.
The file can be downloaded:
- map.gif
- iss.gif
Code:
Python3
# load the world map image screen.bgpic( "images\map.gif" ) screen.register_shape( "images\iss.gif" ) iss = turtle.Turtle() iss.shape( "images\iss.gif" ) iss.setheading( 45 ) iss.penup() |
Access the current status of ISS using the API below:
url = "http://api.open-notify.org/iss-now.json"
Extract the current location of ISS in terms of latitude and longitude from the above API. This script below runs inside the while loop so you can see the updated position and movement of the ISS until you stop the program.
Python3
# load the current status of the ISS in real-time url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) result = json.loads(response.read()) # Extract the ISS location location = result[ "iss_position" ] lat = location[ 'latitude' ] lon = location[ 'longitude' ] # Output lon and lat to the terminal in the # float format lat = float (lat) lon = float (lon) print ( "\nLatitude: " + str (lat)) print ( "\nLongitude: " + str (lon)) |
Update the position of ISS every 5 seconds by refreshing the latitude and longitude value from API.
Python3
# Update the ISS location on the map iss.goto(lon, lat) # Refresh each 5 seconds time.sleep( 5 ) |
Below is the full implementation.
Python3
# json convert the python dictionary # above into a json import json import turtle # urllib.request fetch URLs using # a variety of different protocols import urllib.request import time # webbrowser provides a high-level interface # to allow displaying Web-based documents # to users import webbrowser # geocoder takes the data and locate these # locations in the map import geocoder url = "http://api.open-notify.org/astros.json" response = urllib.request.urlopen(url) result = json.loads(response.read()) file = open ( "iss.txt" , "w" ) file .write( "There are currently " + # prints number of astronauts str (result[ "number" ]) + " astronauts on the ISS: \n\n" ) people = result[ "people" ] # prints names of crew for p in people: file .write(p[ 'name' ] + " - on board" + "\n" ) # print long and lat g = geocoder.ip( 'me' ) file .write( "\nYour current lat / long is: " + str (g.latlng)) file .close() webbrowser. open ( "iss.txt" ) # Setup the world map in turtle module screen = turtle.Screen() screen.setup( 1280 , 720 ) screen.setworldcoordinates( - 180 , - 90 , 180 , 90 ) # load the world map image screen.bgpic( "images/map.gif" ) screen.register_shape( "images\iss.gif" ) iss = turtle.Turtle() iss.shape( "images\iss.gif" ) iss.setheading( 45 ) iss.penup() while True : # load the current status of the ISS in real-time url = "http://api.open-notify.org/iss-now.json" response = urllib.request.urlopen(url) result = json.loads(response.read()) # Extract the ISS location location = result[ "iss_position" ] lat = location[ 'latitude' ] lon = location[ 'longitude' ] # Ouput lon and lat to the terminal lat = float (lat) lon = float (lon) print ( "\nLatitude: " + str (lat)) print ( "\nLongitude: " + str (lon)) # Update the ISS location on the map iss.goto(lon, lat) # Refresh each 5 seconds time.sleep( 5 ) |
Output:
Crew Information: Here is info on the onboarded crew members along with their names.
ISS Location: Here is a screenshot of the moving ISS i.e orbiting around the earth. You can see it by zooming in on the screenshot.
ISS Moving look: Here you can see the ISS moving every 5 seconds.
How To Track ISS (International Space Station) Using Python?
In this article, we will discuss how to track the current location of ISS(International Space Station) and then maps the location. We will write a script that will display the current location of ISS along with onboarded crew names. It works on API, it takes the current location of ISS in the form of latitude and longitude and then locates that value onto the map. It takes the value from the website at every 5 sec and then updates the value of latitude and longitude and thus also moves the ISS icon on the world map. The movement visible is very little but you can notice that movement in the gif below. This is possible by using some of the modules of python like JSON, urllib.requests, Webbrowser, Geocoder etc. Numerous functions are used to create this script.