前幾天坐飛機,一覺醒來看到壯麗的雪山。拍了不少照片,但是並不清楚雪山的大概位置。想要給飛機上拍攝的照片加上地理位置,需要 gpx 文檔,然後用 digiKam 就可以標記照片的位置。怎麼取得航班的位置呢?有兩個方法。
第一個是 FlightAware。搜索到航班的歷史記錄如這個 ZH8960。頁面上有個「+ Google Earth」的圖標,點擊後會得到一個 kml 檔案。然後到 kml2gpx.com 即可將文檔轉換為所需要的 gpx 文檔。但是這個方法的問題在於 FlightAware 的數據會有一部分是直線,看起來就是沒有 GPS 數據,所以我最終使用的方法二。
第二個略複雜,是用 flightradar24 的數據。搜索結果頁也有 kml 檔案下載按鈕,但是需要成為會員才能使用。所以點擊旁邊的回放按鈕,來到回放頁面。然後找到包含 GPS 數據的網絡請求:按 F12 按鈕打開瀏覽器開發者工具 > 切換到「網絡」標籤 > 刷新頁面 > 在過濾請求的輸入框里填寫 flight-playback.json
即可看到所需要的請求 > 在請求上右鍵 > 選擇「保存回應為(Save Response As)即可獲得包含 GPS 數據的 json 文檔。然後使用我的這個 Python 腳本即可將 json 文檔轉換為 gpx 文檔。
###################### #file json2gpx.py # convert json file from www.flightradar24.com to gps file. # get the json file by open the web page: https://www.flightradar24.com/data/flights/zh8960#3a6f819a # then save the request https://api.flightradar24.com/common/v1/flight-playback.json?flightId=3a6f819a×tamp=1747838700 as flight-playback.json # run python json2gpx.py, and get output.gpx ###################### import json import xml.etree.ElementTree as ET from datetime import datetime, timezone #read json from file with open('flight-playback.json', 'r') as f: data_all = json.load(f) data = data_all['result']['response']['data']['flight']['track'] # Create GPX root gpx = ET.Element("gpx", version="1.1", creator="JSON-to-GPX Converter", xmlns="http://www.topografix.com/GPX/1/1") trk = ET.SubElement(gpx, "trk") trkseg = ET.SubElement(trk, "trkseg") # Add track points for point in data: trkpt = ET.SubElement(trkseg, "trkpt", lat=str(point["latitude"]), lon=str(point["longitude"])) ET.SubElement(trkpt, "ele").text = str(point["altitude"]["meters"]) timestamp_iso = datetime.fromtimestamp(point["timestamp"], tz=timezone.utc).isoformat().replace("+00:00", "Z") ET.SubElement(trkpt, "time").text = timestamp_iso # Write to GPX file tree = ET.ElementTree(gpx) with open("output.gpx", "wb") as f: tree.write(f, encoding="utf-8", xml_declaration=True) print("GPX file created as 'output.gpx'")