Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How To Track ISS (International Space Station) Using Python?
Tracking the International Space Station (ISS) in real-time is an exciting project that combines space exploration with Python programming. This article demonstrates how to fetch ISS location data using APIs and visualize it on an interactive map using the folium library.
Installing Required Libraries
Before we start tracking the ISS, we need to install two essential libraries: requests for making API calls and folium for creating interactive maps.
pip install requests folium
Fetching ISS Location Data
The Open Notify API provides real-time ISS location data in JSON format. Let's create a function to retrieve the current latitude and longitude coordinates ?
import requests
def get_iss_location():
response = requests.get("http://api.open-notify.org/iss-now.json")
data = response.json()
latitude = float(data["iss_position"]["latitude"])
longitude = float(data["iss_position"]["longitude"])
return latitude, longitude
# Get current ISS location
iss_lat, iss_lng = get_iss_location()
print(f"ISS Current Location: Latitude: {iss_lat}, Longitude: {iss_lng}")
ISS Current Location: Latitude: 47.3335, Longitude: 49.9148
Creating an Interactive Map
Now let's visualize the ISS location on an interactive world map using folium. This creates an HTML file that can be opened in any web browser ?
import folium
def create_iss_map(latitude, longitude):
# Create map centered on ISS location
iss_map = folium.Map(location=[latitude, longitude], zoom_start=4)
# Add marker for ISS position
folium.Marker(
[latitude, longitude],
tooltip="ISS Location",
popup="International Space Station",
icon=folium.Icon(color="red", icon="info-sign")
).add_to(iss_map)
return iss_map
# Create and save the map
iss_map = create_iss_map(iss_lat, iss_lng)
iss_map.save("iss_location.html")
print("Map saved as iss_location.html")
Map saved as iss_location.html
Real-Time ISS Tracking
For continuous tracking, we can create a loop that updates the ISS location every minute. This provides near real-time tracking ?
import time
def track_iss_realtime():
try:
while True:
# Get current ISS location
iss_lat, iss_lng = get_iss_location()
print(f"ISS Location: Lat: {iss_lat}, Lng: {iss_lng}")
# Update map
iss_map = create_iss_map(iss_lat, iss_lng)
iss_map.save("iss_location.html")
# Wait 60 seconds before next update
time.sleep(60)
except KeyboardInterrupt:
print("\nISS tracking stopped.")
# Uncomment to run real-time tracking
# track_iss_realtime()
Complete ISS Tracker
Here's the complete code that combines all functionality ?
import requests
import folium
from datetime import datetime
def get_iss_location():
"""Fetch current ISS coordinates from Open Notify API"""
try:
response = requests.get("http://api.open-notify.org/iss-now.json", timeout=10)
data = response.json()
latitude = float(data["iss_position"]["latitude"])
longitude = float(data["iss_position"]["longitude"])
timestamp = data["timestamp"]
return latitude, longitude, timestamp
except Exception as e:
print(f"Error fetching ISS data: {e}")
return None, None, None
def create_iss_map(latitude, longitude):
"""Create interactive map with ISS location"""
iss_map = folium.Map(location=[latitude, longitude], zoom_start=4)
# Add ISS marker
folium.Marker(
[latitude, longitude],
popup=f"ISS Position<br>Lat: {latitude}<br>Lng: {longitude}",
tooltip="International Space Station",
icon=folium.Icon(color="red", icon="rocket", prefix="fa")
).add_to(iss_map)
return iss_map
# Track ISS once
lat, lng, timestamp = get_iss_location()
if lat and lng:
current_time = datetime.fromtimestamp(timestamp)
print(f"ISS Location at {current_time}: {lat}, {lng}")
iss_map = create_iss_map(lat, lng)
iss_map.save("iss_tracker.html")
print("Open iss_tracker.html in your browser to view the map!")
ISS Location at 2024-01-15 14:30:25: -51.4142, -179.8206 Open iss_tracker.html in your browser to view the map!
Key Features
| Feature | Description | Benefit |
|---|---|---|
| Real-time Data | Open Notify API | Current ISS position |
| Interactive Map | Folium library | Visual representation |
| Auto-refresh | Time-based loop | Continuous tracking |
Conclusion
This ISS tracker demonstrates how Python can retrieve real-time space data and create interactive visualizations. The combination of APIs and mapping libraries makes it easy to track the International Space Station's orbital path around Earth.
