-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.html
More file actions
117 lines (100 loc) · 4.99 KB
/
predict.html
File metadata and controls
117 lines (100 loc) · 4.99 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ISS Passes Over User Location</title>
<script src="https://unpkg.com/satellite.js/dist/satellite.min.js"></script>
<!-- Include jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div>
<h2>Upcoming ISS Passes</h2>
<div id="iss-passes">Calculating...</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', async function () {
// Haversine formula to calculate distance between two points on the Earth
function haversineDistance(lat1, lon1, lat2, lon2) {
const R = 6371; // Radius of Earth in kilometers
const toRad = angle => angle * Math.PI / 180;
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a = Math.sin(dLat / 2) ** 2 +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
Math.sin(dLon / 2) ** 2;
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c; // Distance in kilometers
}
// Fetch ISS TLE Data (Two-Line Element Set)
async function fetchISSTLE() {
const response = await fetch('https://celestrak.com/NORAD/elements/stations.txt');
const tleData = await response.text();
return tleData.split('\n').slice(1, 3); // Get the two TLE lines
}
// Predict ISS pass over a given location using satellite.js
function predictISSPasses(tleLine1, tleLine2, userLat, userLon) {
const passes = [];
const iss = satellite.twoline2satrec(tleLine1, tleLine2);
const startTime = new Date(); // current time
// Loop to calculate for the next 10 orbits (~15 minutes per orbit)
for (let i = 0; i < 10; i++) {
const time = new Date(startTime.getTime() + i * 90 * 60 * 1000); // 90 min intervals
const positionAndVelocity = satellite.propagate(iss, time);
const positionEci = positionAndVelocity.position;
const gmst = satellite.gstime(time);
const positionGd = satellite.eciToGeodetic(positionEci, gmst);
const latitude = satellite.degreesLat(positionGd.latitude);
const longitude = satellite.degreesLong(positionGd.longitude);
const distance = haversineDistance(userLat, userLon, latitude, longitude);
// ISS pass if it's within 2000 km (visible range)
if (distance <= 2000) {
passes.push({ time, latitude, longitude, distance });
}
}
return passes;
}
// Fetch user location
function fetchUserLocation() {
return new Promise((resolve, reject) => {
$.getJSON('https://geolocation-db.com/json/')
.done(location => {
const userLatitude = parseFloat(location.latitude);
const userLongitude = parseFloat(location.longitude);
resolve({ latitude: userLatitude, longitude: userLongitude });
})
.fail(() => reject('Error fetching user location'));
});
}
// Calculate and display ISS passes
async function calculateISSPasses() {
const issPassesDiv = document.getElementById('iss-passes');
try {
const tleLines = await fetchISSTLE();
const userLocation = await fetchUserLocation();
const passes = predictISSPasses(
tleLines[0], tleLines[1],
userLocation.latitude, userLocation.longitude
);
// Display pass times
if (passes.length > 0) {
issPassesDiv.innerHTML = '<ul>';
passes.forEach(pass => {
const passTime = pass.time.toLocaleString();
issPassesDiv.innerHTML += `<li>Pass at ${passTime} - Distance: ${pass.distance.toFixed(2)} km</li>`;
});
issPassesDiv.innerHTML += '</ul>';
} else {
issPassesDiv.textContent = 'No upcoming passes within visible range.';
}
} catch (error) {
issPassesDiv.textContent = 'Error calculating ISS passes: ' + error;
}
}
// Start pass calculation
calculateISSPasses();
});
</script>
</body>
</html>