-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
100 lines (79 loc) · 3.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
import turtle
import urllib.request
from math import radians, cos, sin, asin, sqrt
from geopy.geocoders import Nominatim
import time as time_module
def setup():
turtle.title('ISS Tracker')
# Setup the world map
screen = turtle.Screen()
screen.setup(1189, 848)
screen.setworldcoordinates(-180,-90,180,90)
screen.bgpic("map.gif")
# Setup ISS object
screen.register_shape("iss.gif")
iss = turtle.Turtle()
iss.shape("iss.gif")
iss.penup()
return iss # return ISS object
def astronaut_details():
# load the current status of astronauts on ISS in real-time
response = urllib.request.urlopen("http://api.open-notify.org/astros.json")
result = json.loads(response.read())
# Extract and print the astronaut's details
print(f"There are currently {result['number']} astronauts on the ISS: ")
for p in result["people"]:
print(p['name'])
def get_coordinates():
# load the current status of the ISS in real-time
response = urllib.request.urlopen("http://api.open-notify.org/iss-now.json")
result = json.loads(response.read())
# Extract the ISS location and time
location = result["iss_position"]
lat = float(location['latitude'])
lon = float(location['longitude'])
time = result["timestamp"]
return lon, lat, time
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance in kilometers between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371 # Radius of earth in kilometers. Use 3956 for miles. Determines return value units.
return c * r
def calspeed(lon1, lat1, time1, lon2, lat2, time2):
d = haversine(lon1, lat1, lon2, lat2)
t = time2-time1 or 1 # incase of 0 time difference change to 1 to handle ZeroDivisionError
return round((d/t)*3600, 2) # returns speed in kilometers per hour rounded upto 2nd decimal place
def getloc(lon, lat):
geolocator = Nominatim(user_agent="http") # initialize Nominatim API
location = geolocator.reverse(f'{lat},{lon}')
if location==None:
return 'Ocean'
return location.raw['address'].get('country', '')
def main(trail=True):
iss = setup()
astronaut_details()
prevlon, prevlan, prevtime = get_coordinates() # initialize reference values
while True:
lon, lat, time = get_coordinates()
speed = calspeed(prevlon, prevlan, prevtime, lon, lat, time)
# Update the ISS location on the map
iss.goto(lon, lat)
if trail==True:
iss.dot(size=2) # Plot trail dots
# Output speed and country to terminal
print(f'Speed: {speed} km/hr')
print(f'Above {getloc(lon, lat)}')
prevlon, prevlan, prevtime = lon, lat, time # update reference values
time_module.sleep(5) # Referesh each 5 seconds
if __name__=='__main__':
main(trail=False)