|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import requests |
| 4 | +import json |
| 5 | +import threading |
| 6 | +import datetime |
| 7 | +import tkinter |
| 8 | +import urllib.request |
| 9 | +from PIL import ImageTk, Image |
| 10 | +import math |
| 11 | + |
| 12 | +prev_lat, prev_long, prev_time, prev_speed = 0, 0, 0, 0 |
| 13 | + |
| 14 | +def distance_on_unit_sphere(lat1, long1, lat2, long2): |
| 15 | + |
| 16 | + # Convert latitude and longitude to |
| 17 | + # spherical coordinates in radians. |
| 18 | + degrees_to_radians = math.pi/180.0 |
| 19 | + |
| 20 | + # phi = 90 - latitude |
| 21 | + phi1 = (90.0 - lat1)*degrees_to_radians |
| 22 | + phi2 = (90.0 - lat2)*degrees_to_radians |
| 23 | + |
| 24 | + # theta = longitude |
| 25 | + theta1 = long1*degrees_to_radians |
| 26 | + theta2 = long2*degrees_to_radians |
| 27 | + |
| 28 | + # Compute spherical distance from spherical coordinates. |
| 29 | + |
| 30 | + # For two locations in spherical coordinates |
| 31 | + # (1, theta, phi) and (1, theta', phi') |
| 32 | + # cosine( arc length ) = |
| 33 | + # sin phi sin phi' cos(theta-theta') + cos phi cos phi' |
| 34 | + # distance = rho * arc length |
| 35 | + |
| 36 | + cos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) + |
| 37 | + math.cos(phi1)*math.cos(phi2)) |
| 38 | + arc = math.acos( cos ) |
| 39 | + |
| 40 | + # Remember to multiply arc by the radius of the earth |
| 41 | + # in your favorite set of units to get length. |
| 42 | + return arc |
| 43 | + |
| 44 | + |
| 45 | +def start(): |
| 46 | + top = tkinter.Tk() |
| 47 | + top.title("ISS Tracker") |
| 48 | + |
| 49 | + def counter_label(label): |
| 50 | + |
| 51 | + def count(): |
| 52 | + global prev_lat |
| 53 | + global prev_long |
| 54 | + global prev_time |
| 55 | + global prev_speed |
| 56 | + req = requests.get("http://api.open-notify.org/iss-now.json") |
| 57 | + |
| 58 | + obj = req.json() |
| 59 | + lat = float(obj['iss_position']['latitude']) |
| 60 | + long = float(obj['iss_position']['longitude']) |
| 61 | + timestamp = int(obj['timestamp']) |
| 62 | + time_change = timestamp - prev_time |
| 63 | + distance_moved = distance_on_unit_sphere(prev_lat, prev_long, |
| 64 | + lat, long) * 4164 |
| 65 | + speed = distance_moved * 3600 |
| 66 | + |
| 67 | + if time_change != 1 or distance_moved > 5: |
| 68 | + label.config(text="Estimated speed: {} mph\nLatitude: {}\nLongitude: {}".format(prev_speed, lat, long)) |
| 69 | + else: |
| 70 | + label.config(text="Estimated speed: {} mph\nLatitude: {}\nLongitude: {}".format(speed, lat, long)) |
| 71 | + prev_speed = speed |
| 72 | + prev_lat, prev_long, prev_time = lat, long, timestamp |
| 73 | + label.after(1000, count) |
| 74 | + count() |
| 75 | + title = tkinter.Label(top, height=1, width=50, |
| 76 | + font=("Arial", 25, "bold"), text="ISS Tracker") |
| 77 | + title.pack() |
| 78 | + label = tkinter.Label(top, height=3, width=50, font=("Arial", 20)) |
| 79 | + label.pack() |
| 80 | + counter_label(label) |
| 81 | + top.mainloop() |
0 commit comments