Giter Site home page Giter Site logo

billy1125 / tra_time_space_diagram Goto Github PK

View Code? Open in Web Editor NEW
22.0 2.0 4.0 195 KB

台灣鐵路局公開資料JSON轉鐵路運行圖(SVG格式)程式

Home Page: http://tradiagram.com

License: MIT License

Python 94.92% CSS 4.58% Batchfile 0.49%
timespacediagram json svg python pandas beautifulsoup4 railway taiwanrailway railways

tra_time_space_diagram's Introduction

台灣鐵路局公開資料JSON鐵路運行圖繪製程式

項目 內容
專案名稱 TRA_time_space_diagram
Author 呂卓勳 Cho-Hsun Lu
E-mail [email protected]
版本 1.3.7

緣起

鐵道愛好者通常都會使用鐵路運行圖(Railway Time-Space Diagram),幫助研究鐵道相關事務,雖然有人會出版紙本台鐵鐵路運行圖,但是當台鐵改點,紙本的資訊參考價值就會變低。所以當我知道台鐵已經將時刻資訊以 JSON 公開於網路上之後,便試圖以 Python 開發一個繪製台灣鐵路運行圖的程式。

限制

本程式鐵路運行圖均來自於台鐵 open data 進行分析整理與繪製。然而公開資料不等於實際台鐵的運行計畫,僅是旅客所需的列車到站與離站時間資料,列車的待避或會車狀況無法在運行圖中展示,貨運、迴送等非客運車次也因台鐵並未在公開資料集提供。因此會出現運行圖與實際運行有所差異,以及無法提供非客運車次的運行線。因此實際鐵路運行情況,請以台鐵所公布資訊或鐵路運行實況為準。

程式功能

本程式用於將台鐵公開之時刻表 JSON 格式檔案 (以下稱台鐵 JSON),轉換為鐵路運行圖,運行圖格式為可縮放向量圖形(Scalable Vector Graphics, SVG)。並且提供一個批次下載台鐵 JSON 之簡易程式。

Python 版本與所需相關程式開發套件

目前本程式主要於 Python 3.9x 開發,除此之外本程式需要以下套件,包括:

  • Pandas:用於列車推算通過車站時間,本程式開發使用版本為 2.0.0
  • beautifulsoup4:用於擷取台鐵 JSON,本程式開發使用版本為 4.11.1

進度條程式碼則採用 Vladimir Ignatev 所設計的程式。svg 繪圖部分程式感謝 nedwu的啟發,並且參考其專案部分程式碼

以上套件為主要開發環境,若您採用非相同版本之 Python 與套件,執行上有任何錯誤歡迎交流。

使用方法

請將所有程式解壓縮到同一檔案夾,包括 CSV、JSON、OUTPUT 等檔案夾均需於同一檔案夾,再將所需要轉檔的台鐵 JSON 檔案,置放於 JSON 檔案夾之中,台鐵 JSON 檔案可至台鐵公開資料網站中下載。

程式以作業系統的命令行 (command-line) 模式執行,執行方式包括一般模式與參數模式,分述如下:

一般模式

命令行執行語法

$ python batch.py
************************************
台鐵JSON轉檔運行圖程式 - 版本:1.3.7
************************************

之後會顯示本程式的標題,並且顯示版本。

請選擇轉檔方式,直接轉檔請直接按Enter鍵,需要轉檔特定車次請輸入「Y」再進行選擇,離開請按「N」:Y

詢問您是否需要轉換特定車次,若您需要轉換特定車次,請按「Y」,若不需,請直接按下「ENTER」即可。

請注意!已轉檔的 JSON 檔案將移動到 JSON_BACKUP 檔案夾。 若按下「ENTER」,程式將直接就 JSON 檔案夾中所有以「JSON」附檔名之資料進行轉檔,直到所有 JSON 檔案轉換完成為止。

請問特定車次號碼?
請輸入車次號後再按Enter鍵。如果有多個車次,請依序輸入各車次,中間以半形空白鍵隔開(例如: 408 426 111),再按Enter鍵:3671

若按下「Y」,將繼續顯示以上問題。 請輸入車次號後再按Enter鍵,例如第 3671 車次普快車,請輸入「3671」再按Enter鍵,程式將尋找是否有該車次,並且直接繪製運行圖。 如果有多個車次需要繪製,請依序輸入各車次,中間以半形空白鍵隔開(例如: 408 426 111),輸入結束則直接按下「Enter」即可。

共有 60 個JSON 檔案需要轉檔。

目前進行日期「20230623」轉檔。

[==========================================------------------] 70.7% ...目前正在轉換: 車次3671

繪製運行圖的過程中,程式將顯示進度條與正在處理的車次。

檔案轉換完成!轉換時間共 15.6 秒

如果繪製完成,將顯示以上訊息,訊息包括轉換所耗費的時間。

附註:轉換時間需視 CPU 效能,保守時間約 1 分鐘內可完成。

無法執行!沒有 JSON 檔案,請在 JSON 資料夾中置入台鐵的時刻表 JSON。

如果您在置放 JSON 的檔案夾中沒有台鐵 JSON 檔案,則會顯示以上錯誤訊息。 程式繪製完成的運行圖,將依照不同台鐵營運路線,分別置放於 OUTPUT 檔案夾之中,檔名則與轉換的 JSON 檔名相同。

參數模式

本程式支援以參數模式執行,您可以直接輸入以下參數,以半形空白隔開,直接執行無須一一回答問題。參數包括:

  • 需轉換台鐵 JSON 檔案存放資料夾位置
  • 運行圖轉換完成儲存位置
  • 特定車次之車次號,若要轉換所有車次,參數為 ALL

若您不改動 JSON 與 OUTPUT 檔案夾位置,並且指定要轉換第 3671 車次普快車,可以這樣輸入:

$ python batch.py JSON OUTPUT 3671

若您改動 JSON 與 OUTPUT 檔案夾位置,分別為 /Users/username/Desktop/JSON/Users/username/Desktop/OUTPUT,並且指定要轉換所有車次,可以這樣輸入:

$ python batch.py /Users/username/Desktop/JSON /Users/username/Desktop/OUTPUT ALL

執行後,程式將自動將所有置放於您指定的 JSON 檔案夾中的台鐵 JSON 進行轉檔,並且依照不同台鐵營運路線,置放於您指定的 OUTPUT 檔案夾之中。

擷取台鐵 JSON 程式

若您需要批次下載當日之台鐵所有 JSON 程式,請以命令行模式,執行 JSON 檔案夾中之 download_json.py 程式,執行語法為:

$ python download_json.py

該程式將直接下載台鐵公開資料網站網站中所有 JSON 檔案,並且置放於 JSON 檔案夾中。

附註:台鐵每日均提供當日至 60 天內每日之時刻表資料,以 JSON 格式提供。如果想要看相關資料定義的說明,可至政府資料開放平台參考。

閱讀運行圖之方法

本程式所轉換之運行圖,檔案副檔名為 .svg,請使用瀏覽器直接開啟檔案,請注意,請將 OUTPUT 檔案夾中的 stlye.css 樣式檔案與運行圖置放於同一檔案夾中,再以瀏覽器開啟運行圖。目前為止,本程式所轉換之運行圖於 Google Chrome、Mozilla Firefox、Opera、Apple Safari 均能正常顯示,至於其他瀏覽器尚未實地測試,若有問題也歡迎回報。

致謝

感謝以上網友對於本程式的建議與錯誤回報,您的協助能讓本程式更臻完整,感謝!

tra_time_space_diagram's People

Contributors

billy1125 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tra_time_space_diagram's Issues

有辦法繪製非台鐵的運行圖嗎?

你好,我是一名建中學生,也是一名鐵道迷,本來就有在使用您做的網頁,也覺得你的運行圖繪製的十分精美。
因為我在做鐵路時刻表相關的資訊科展,我想要fork你的repl到我專題來使用,所以我想請問如果我有一個我生出來的時刻表,有沒有辦法透過你的軟體內容轉成運行圖。或是想詢問可能可以修改哪些位置?
(主要是想詢問繪圖的核心是如何實現的)

Some runtime errors.

Hi there,
I've tried to use the code, but it will randomly produce some different errors.

root@debian-s-2vcpu-2gb-sgp1-01:~/TRA_time_space_diagram# python3 batch.py
台鐵JSON轉檔運行圖程式 - 版本:1.0b

台鐵JSON時刻表檔案日期:20180725
Traceback (most recent call last):---------------------------] 0.7% ...
  File "batch.py", line 157, in <module>
    main(argv_json_location, argv_website_svg_location, argv_train_no)
  File "batch.py", line 73, in main
    list_passing_stations = dg.find_stations(list_start_end_station, train_no['Line'], train_no['LineDir']) # 查詢特定車次所有停靠與通過車站
  File "/root/TRA_time_space_diagram/diagram.py", line 100, in find_stations
    temp.append([stations[station][0], stations[station][1], stations[station][3], km])
KeyError: ''
root@debian-s-2vcpu-2gb-sgp1-01:~/TRA_time_space_diagram# python3 batch.py
台鐵JSON轉檔運行圖程式 - 版本:1.0b

台鐵JSON時刻表檔案日期:20180725
Traceback (most recent call last):---------------------------] 1.5% ...
  File "batch.py", line 157, in <module>
    main(argv_json_location, argv_website_svg_location, argv_train_no)
  File "batch.py", line 73, in main
    list_passing_stations = dg.find_stations(list_start_end_station, train_no['Line'], train_no['LineDir']) # 查詢特定車次所有停靠與通過車站
  File "/root/TRA_time_space_diagram/diagram.py", line 100, in find_stations
    temp.append([stations[station][0], stations[station][1], stations[station][3], km])
KeyError: ''
root@debian-s-2vcpu-2gb-sgp1-01:~/TRA_time_space_diagram# python3 batch.py
台鐵JSON轉檔運行圖程式 - 版本:1.0b

台鐵JSON時刻表檔案日期:20180725
201----------------------------------------------------------] 1.2% ...
201----------------------------------------------------------] 1.4% ...
201----------------------------------------------------------] 1.6% ...
Traceback (most recent call last):---------------------------] 4.4% ...
  File "batch.py", line 157, in <module>
    main(argv_json_location, argv_website_svg_location, argv_train_no)
  File "batch.py", line 80, in main
    midnight_km = dg.midnight_train(list_start_end_station, list_passing_stations, over_night_stn) # 跨夜車次處理
  File "/root/TRA_time_space_diagram/diagram.py", line 303, in midnight_train
    item = list_passing_stations[i]
IndexError: list index out of range
root@debian-s-2vcpu-2gb-sgp1-01:~/TRA_time_space_diagram# python3 batch.py
台鐵JSON轉檔運行圖程式 - 版本:1.0b

台鐵JSON時刻表檔案日期:20180725
Traceback (most recent call last):---------------------------] 6.3% ...
  File "batch.py", line 157, in <module>
    main(argv_json_location, argv_website_svg_location, argv_train_no)
  File "batch.py", line 73, in main
    list_passing_stations = dg.find_stations(list_start_end_station, train_no['Line'], train_no['LineDir']) # 查詢特定車次所有停靠與通過車站
  File "/root/TRA_time_space_diagram/diagram.py", line 137, in find_stations
    km += float(stations[station][10])
ValueError: could not convert string to float:
root@debian-s-2vcpu-2gb-sgp1-01:~/TRA_time_space_diagram# python3 batch.py
台鐵JSON轉檔運行圖程式 - 版本:1.0b

台鐵JSON時刻表檔案日期:20180725
201----------------------------------------------------------] 1.4% ...
201----------------------------------------------------------] 1.5% ...
201----------------------------------------------------------] 1.6% ...
201----------------------------------------------------------] 1.8% ...
Traceback (most recent call last):---------------------------] 2.7% ...
  File "batch.py", line 157, in <module>
    main(argv_json_location, argv_website_svg_location, argv_train_no)
  File "batch.py", line 73, in main
    list_passing_stations = dg.find_stations(list_start_end_station, train_no['Line'], train_no['LineDir']) # 查詢特定車次所有停靠與通過車站
  File "/root/TRA_time_space_diagram/diagram.py", line 194, in find_stations
    km += float(stations[station][15])
ValueError: could not convert string to float:

I've run through the codes but found nothing unusual, can you please advice me if I miss something. thanks.

廢站的資料來源與保留廢站的目的

因為已經廢站了,好像不好確認資料是否正常,畢竟台鐵不提供了。還是我可以從哪裡看到或看出來?
另外,做為時刻表,不知道廢站留著有何打算?是拍照會用到嗎?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.