var satmarker; let maidenhead; let leafletMap; let saticon = L.divIcon({ html: '', className: '', // Prevents default Leaflet styles iconSize: [30, 30], iconAnchor: [15, 15] // Center the icon }); let homeicon = L.icon({ iconUrl: icon_home_url, iconSize: [15, 15] }); let observerGd = { longitude: satellite.degreesToRadians(homelon), latitude: satellite.degreesToRadians(homelat), height: 0.370 }; let sats = (function (L, d3, satelliteJs) { let RADIANS = Math.PI / 180; let DEGREES = 180 / Math.PI; let R_EARTH = 6378.137; // equatorial radius (km) /* =============================================== */ /* =============== CLOCK ========================= */ /* =============================================== */ /** * Factory function for keeping track of elapsed time and rates. */ function Clock() { this._rate = 60; // 1ms elapsed : 60sec simulated this._date = d3.now(); this._elapsed = 0; }; Clock.prototype.date = function (timeInMs) { if (!arguments.length) return this._date + (this._elapsed * this._rate); this._date = timeInMs; return this; }; Clock.prototype.elapsed = function (ms) { if (!arguments.length) return this._date - d3.now(); // calculates elapsed this._elapsed = ms; return this; }; Clock.prototype.rate = function (secondsPerMsElapsed) { if (!arguments.length) return this._rate; this._rate = secondsPerMsElapsed; return this; }; /* ==================================================== */ /* =============== CONVERSION ========================= */ /* ==================================================== */ function satrecToFeature(satrec, date, props) { // DJ7NT: This is never called let properties = props || {}; let positionAndVelocity = satelliteJs.propagate(satrec, date); let gmst = satelliteJs.gstime(date); let positionGd = satelliteJs.eciToGeodetic(positionAndVelocity.position, gmst); properties.height = positionGd.height; return { type: "FeatureCollection", "features": [ { type: 'Feature', properties: properties, geometry: { type: 'Point', coordinates: [ positionGd.longitude * DEGREES, positionGd.latitude * DEGREES ] } }, { type: 'Feature', properties: {infoText: 'blabla'}, geometry: { type: 'Point', coordinates: [ positionGd.longitude * DEGREES, positionGd.latitude * DEGREES ] } }] }; }; /* ==================================================== */ /* =============== TLE ================================ */ /* ==================================================== */ /** * Factory function for working with TLE. */ function TLE() { this._properties; this._date; }; TLE.prototype._lines = function (arry) { return arry.slice(0, 2); }; TLE.prototype.satrecs = function (tles) { return tles.map(function (d) { return satelliteJs.twoline2satrec.apply(null, this._lines(d)); }); }; TLE.prototype.features = function (tles) { let date = this._date || d3.now(); return tles.map(function (d) { let satrec = satelliteJs.twoline2satrec.apply(null, this._lines(d)); return satrecToFeature(satrec, date, this._properties(d)); }); }; TLE.prototype.lines = function (func) { if (!arguments.length) return this._lines; this._lines = func; return this; }; TLE.prototype.properties = function (func) { if (!arguments.length) return this._properties; this._properties = func; return this; }; TLE.prototype.date = function (ms) { if (!arguments.length) return this._date; this._date = ms; return this; }; /* ==================================================== */ /* =============== PARSE ============================== */ /* ==================================================== */ /** * Parses text file string of tle into groups. * @return {string[][]} Like [['tle line 1', 'tle line 2'], ...] */ function parseTle(tleString) { // remove last newline so that we can properly split all the lines let lines = tleString.replace(/\r?\n$/g, '').split(/\r?\n/); return lines.reduce(function (acc, cur, index) { if (index % 2 === 0) acc.push([]); acc[acc.length - 1].push(cur); return acc; }, []); }; /* ==================================================== */ /* =============== SATELLITE ========================== */ /* ==================================================== */ /** * Satellite factory function that wraps satellitejs functionality * and can compute footprints based on TLE and date * * @param {string[][]} tle two-line element * @param {Date} date date to propagate with TLE */ function Satellite(tle, date) { this._satrec = satelliteJs.twoline2satrec(tle[0], tle[1]); this._satNum = this._satrec.satnum; // NORAD Catalog Number this._altitude; // km this._position = { lat: null, lng: null }; this._halfAngle; // degrees this._date; this._gmst; this.setDate(date); this.update(); this._orbitType = this.orbitTypeFromAlt(this._altitude); // LEO, MEO, or GEO }; /** * Updates satellite position and altitude based on current TLE and date */ Satellite.prototype.update = function () { try { let positionAndVelocity = satelliteJs.propagate(this._satrec, this._date); let positionGd = satelliteJs.eciToGeodetic(positionAndVelocity.position, this._gmst); let positionEcf = satelliteJs.eciToEcf(positionAndVelocity.position, this._gmst); let lA = satelliteJs.ecfToLookAngles(observerGd, positionEcf); this._lookAngles = { azimuth: lA.azimuth * DEGREES, elevation: lA.elevation * DEGREES, rangeSat: lA.rangeSat }; this._position = { lat: positionGd.latitude * DEGREES, lng: positionGd.longitude * DEGREES }; this._altitude = positionGd.height; satmarker.setLatLng(this._position); } catch (e) { // Malicious // non-calcable SAT Found } finally { return this; } }; /** * @returns {GeoJSON.Polygon} GeoJSON describing the satellite's current footprint on the Earth */ Satellite.prototype.getFootprint = function () { let theta = this._halfAngle * RADIANS; coreAngle = this._coreAngle(theta, this._altitude, R_EARTH) * DEGREES; return d3.geoCircle() .center([this._position.lng, this._position.lat]) .radius(coreAngle)(); }; Satellite.prototype.getLocation = function () { return d3.geoCircle() .center([this._position.lng, this._position.lat]) .radius(1)(); }; /** * A conical satellite with half angle casts a circle on the Earth. Find the angle * from the center of the earth to the radius of this circle * @param {number} theta: Satellite half angle in radians * @param {number} altitude Satellite altitude * @param {number} r Earth radius * @returns {number} core angle in radians */ Satellite.prototype._coreAngle = function (theta, altitude, r) { // if FOV is larger than Earth, assume it goes to the tangential point // if (Math.sin(theta) != r / (altitude + r)) { return Math.acos(r / (r + altitude)); // } // return Math.abs(Math.asin((r + altitude) * Math.sin(theta) / r)) - theta; }; Satellite.prototype.halfAngle = function (halfAngle) { if (!arguments.length) return this._halfAngle; this._halfAngle = halfAngle; return this; }; Satellite.prototype.satNum = function (satNum) { if (!arguments.length) return this._satNum; this._satNum = satNum; return this; }; Satellite.prototype.altitude = function (altitude) { if (!arguments.length) return this._altitude; this._altitude = altitude; return this; }; Satellite.prototype.position = function (position) { if (!arguments.length) return this._position; this._position = position; return this; }; Satellite.prototype.getOrbitType = function () { return this._orbitType; }; /** * sets both the date and the Greenwich Mean Sidereal Time * @param {Date} date */ Satellite.prototype.setDate = function (date) { this._date = date; this._gmst = satelliteJs.gstime(date); return this; }; /** * Maps an altitude to a type of satellite * @param {number} altitude (in KM) * @returns {'LEO' | 'MEO' | 'GEO'} */ Satellite.prototype.orbitTypeFromAlt = function (altitude) { this._altitude = altitude || this._altitude; return this._altitude < 2000 ? 'LEO' : this._altitude > 22000 ? 'GEO' : 'MEO'; }; /* =============================================== */ /* =============== LEAFLET MAP =================== */ /* =============================================== */ // Approximate date the tle data was aquired from https://www.space-track.org/#recent // let TLE_DATA_DATE = new Date(2024, 04, 18).getTime(); let TLE_DATA_DATE = Date.now(); let attributionControl; let activeClock; let sats; let svgLayer; function projectPointCurry(map) { return function (x, y) { const point = map.latLngToLayerPoint(L.latLng(y, x)); this.stream.point(point.x, point.y); } }; function init(satellite) { svgLayer = L.svg(); leafletMap = L.map('sat_map', { zoom: 3, center: [20, 0], // attributionControl: false, layers: [ L.tileLayer(tileUrl, { // noWrap: false, }), svgLayer ] }); satmarker = L.marker( [0, 0], { icon: saticon, title: satellite, zIndex: 1000, } ).addTo(leafletMap).on('click', displayUpComingPasses); // Add an always-visible label (tooltip) satmarker.bindTooltip(satellite, { permanent: true, // Always visible direction: "top", // Position label above the marker offset: [0, -20], // Adjust position className: "satellite-label" // Optional: Custom CSS // className: "leaflet-popup-content-wrapper" // Optional: Custom CSS }); L.marker( [homelat, homelon], { icon: homeicon, title: 'Home', zIndex: 1000, } ).addTo(leafletMap); /*Legend specific*/ let legend = L.control({ position: "topright" }); legend.onAdd = function(map) { let div = L.DomUtil.create("div", "legend"); let html = "
| Satellite | |
| Orbit | |
| Altitude | |
| Azimuth | |
| Elevation | |
| Gridsquare | |
| ' + lang_gen_hamradio_gridsquares + ' |