日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python之深入解析如何制作国际空间站实时跟踪器

發(fā)布時(shí)間:2024/5/21 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python之深入解析如何制作国际空间站实时跟踪器 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、前言

  • Open Notify 是一個(gè)開(kāi)源項(xiàng)目,旨在為 NASA 的一些出色數(shù)據(jù)提供簡(jiǎn)單的編程接口。
  • open-notify.org 的作者做了一些工作,以獲取原始數(shù)據(jù)并將其轉(zhuǎn)換為與太空和航天器有關(guān)的 API。
  • 現(xiàn)在將通過(guò)這個(gè)接口,獲取得到國(guó)際空間站的位置,并實(shí)時(shí)地繪制到地圖上。
  • 為了實(shí)現(xiàn)該目標(biāo),得先安裝 ISS_Info:
pip install ISS-Info

二、地圖初始化

  • 為了實(shí)時(shí)展示國(guó)際空間站的路徑,需要使用 turtle 繪制曲線(xiàn),因此可以創(chuàng)建一個(gè) turtle 畫(huà)布,將背景設(shè)為地球:
import ISS_Info import turtle import time import json import urllib.request screen = turtle.Screen() screen.setup(720, 360) screen.setworldcoordinates(-180, -90, 180,90) screen.bgpic("map.png") screen.bgcolor("black") screen.register_shape("isss.gif") screen.title("Real time ISS tracker") iss = turtle.Turtle() iss.shape("isss.gif")
  • 效果如下:

三、獲取空間站的人數(shù)

  • 如果能知道空間站上的宇航員人數(shù),就能更加準(zhǔn)確的跟蹤國(guó)際空間站。幸運(yùn)的是 open-notify 確實(shí)提供了這樣的接口。
  • 為了獲取人數(shù)信息,必須向下列接口請(qǐng)求拿到數(shù)據(jù),并將相應(yīng)的宇航員名字寫(xiě)在左上角:
http://api.open-notify.org/astros.json
  • 實(shí)現(xiàn)代碼:
astronauts = turtle.Turtle() astronauts.penup() astronauts.color('black') astronauts.goto(-178,86) astronauts.hideturtle() url = "http://api.open-notify.org/astros.json" response = urllib.request.urlopen(url) result = json.loads(response read()) print("There are currently " + str(result ["number"]) + " astronauts in space:") print("") astronauts.write("People in space: " + str(result["number"]), font=style) astronauts.sety(astronauts.ycor() - 5)people = result["people"] for p in people:print(p["name"] + " on: " + p["craft"])astronauts.write(p["name" ] + "on:" + p["craft"], font=style)astronauts.sety(astronauts.ycor() - 5)
  • 效果如下:

四、繪制空間站位置

  • 為了能夠繪制空間站的實(shí)時(shí)位置,需要請(qǐng)求拿到空間站的位置信息。請(qǐng)求的接口是:
http://api.open-notify.org/iss-now.json
  • 不過(guò)作者將其封裝成了一個(gè)函數(shù),我們直接調(diào)用 iss_current_loc 即可,循環(huán)獲取國(guó)際空間站位置:
while True :location = ISS_Info.iss_current_loc()lat = location['iss_ position']['latitude']lon = location['iss_ position']['longitude']print("Position: \n latitude: {}, longitude: {}" .format(lat, lon))pos = iss.pos()posx = iss.xcor()if iss.xcor() >= (179.1): ### Stop drawing at the right edge ofiss.penup() ### the screen to avoid aiss.goto(float(lon), float(lat)) ### horizontal wrap round line time.sleep(5)else:iss.goto(float(lon), float(lat))iss.pendown()time.sleep(5)
  • 我們還可以標(biāo)出自己目前所處的位置,以查看和國(guó)際空間站的距離及空間站經(jīng)過(guò)你上空的時(shí)間點(diǎn)(UTC)。
# 深圳 lat = 112.5118928 lon = 23.8534489 prediction = turtle.Turtle() prediction.penup() prediction.color('yellow') prediction.goto(lat, lon) prediction.dot(5) prediction.hideturtle() url = 'http://api.open-notify.org/iss-pass.json?lat=' + str(lat-90) + '&lon=' + str(lon) response = urllib.request.urlopen(url) result = json.loads(response.read()) over = result ['response'][1]['risetime'] prediction.write(time.ctime(over), font=style)
  • 不過(guò)這里值得注意的是,iss-pass.json 這個(gè)接口的緯度計(jì)算必須在 -90 到 90 之內(nèi),因此深圳的緯度需要減去 90。
  • 最終效果如下:

總結(jié)

以上是生活随笔為你收集整理的Python之深入解析如何制作国际空间站实时跟踪器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。