我想使用以下代码提取卫星(ISS(的十进制坐标:
from skyfield.api import EarthSatellite, Topos, load
import time
line1 = '1 25544U 98067A 14020.93268519 .00009878 00000-0 18200-3 0 5082'
line2 = '2 25544 51.6498 109.4756 0003572 55.9686 274.8005 15.49815350868473'
satellite = EarthSatellite(line1, line2, name='ISS (ZARYA)')
while True:
ts = load.timescale()
t = ts.now()
geometry = satellite.at(t)
subpoint = geometry.subpoint()
print(subpoint.latitude)
print('n')
print(subpoint.longitude)
time.sleep(1)
输出是一个字符串:-45deg 44' 13.5"
。
将其转换为类似-77.0089°
的内容最简单的方法是什么?
令人高兴的是,对象latitude
和longitude
不是简单的字符串,而是花哨的角度对象,它们只是将自己打印为三部分字符串,以便于在屏幕上阅读。您可以通过向Python索取它们的文档来了解更多关于它们的信息。在循环结束时,尝试添加:
help(subpoint.latitude)
Angle
类的文档将会出现。你也可以在网上找到它:
https://rhodesmill.org/skyfield/api-units.html#skyfield.units.Angle
您将需要使用属性degrees
,该属性将角度表示为浮点数。将程序中的打印调用更改为:
print(subpoint.latitude.degrees)
print('n')
print(subpoint.longitude.degrees)
试试这个
from skyfield.api import EarthSatellite, Topos, load
import time
line1 = '1 25544U 98067A 14020.93268519 .00009878 00000-0 18200-3 0 5082'
line2 = '2 25544 51.6498 109.4756 0003572 55.9686 274.8005 15.49815350868473'
satellite = EarthSatellite(line1, line2, name='ISS (ZARYA)')
def convert(deg):
d, m, s = str(deg).replace('deg', '').split(" ")
ans = float(d) + (float(m.strip("'")) / 60) + (float(s.strip('"')) / 3600)
return str(ans) + chr(176)
while True:
ts = load.timescale()
t = ts.now()
geometry = satellite.at(t)
subpoint = geometry.subpoint()
lat = convert(subpoint.latitude)
lng = convert(subpoint.longitude)
print(lat, lng)
time.sleep(1)
输出:
48.522305555555555° 133.80061111111112°
48.49586111111111° 133.89988888888888°
48.46933333333334° 133.99902777777777°
48.44269444444444° 134.09808333333334°
48.416° 134.19702777777778°