4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GTFS Realtime やーる(TripUpdate)

Last updated at Posted at 2021-12-17

はじめに

熊本のGTFSリアルタイムが公開されたので、Pythonで取得してみましょう。
こういう便利なものが作れちゃいます。

開発環境

  • Windows PC
  • Python 3.6
  • gtfs-realtime-bindings

実装

1.gtfs-realtime-bindingsをインストールします

pip install gtfs-realtime-bindings

2.熊本の産交バスのTripUpdate(ルートの最新情報)を取得してみましょう

gtfs-realtime-sample.py
from google.transit import gtfs_realtime_pb2
import requests

feed = gtfs_realtime_pb2.FeedMessage()
response = requests.get('https://km.bus-vision.jp/realtime/sankobus_trip_update.bin')
feed.ParseFromString(response.content)
for entity in feed.entity:
  if entity.HasField('trip_update'):
    print(entity.trip_update)

3.次のような結果が返ってきます

stop_time_update {
  stop_sequence: 36
  arrival {
    delay: 429
    time: 1639723749
    uncertainty: 300
  }
  departure {
    delay: 429
    time: 1639723749
    uncertainty: 300
  }
  stop_id: "100441_1"
}
vehicle {
  id: "347"
  label: "1201"
}

4.ダウンロードしたTripUpdateファイル(.bin)から読み込む場合

gtfs-realtime-sample.py
from google.transit import gtfs_realtime_pb2
import requests

feed = gtfs_realtime_pb2.FeedMessage()
with open("sankobus_trip_update.bin", 'rb') as f:
    content = f.read()
    
feed.ParseFromString(content)
for entity in feed.entity:
  if entity.HasField('trip_update'):
    print(entity.trip_update)
stop_time_update {
  stop_sequence: 36
  arrival {
    delay: 369
    time: 1639723689
    uncertainty: 300
  }
  departure {
    delay: 369
    time: 1639723689
    uncertainty: 300
  }
  stop_id: "100441_1"
}
vehicle {
  id: "347"
  label: "1201"
}

お疲れ様でした。

GTFSリアルタイムを公開している都道府県リスト

熊本のGTFS Realtime

産交バス(TripUpdate) https://km.bus-vision.jp/realtime/sankobus_trip_update.bin
産交バス(VehiclePosition) https://km.bus-vision.jp/realtime/sankobus_vpos_update.bin
熊本電鉄バス(TripUpdate) https://km.bus-vision.jp/realtime/dentetsu_trip_update.bin
熊本電鉄バス(VehiclePosition) https://km.bus-vision.jp/realtime/dentetsu_vpos_update.bin
熊本バス(TripUpdate) https://km.bus-vision.jp/realtime/kumabus_trip_update.bin
熊本バス(VehiclePosition) https://km.bus-vision.jp/realtime/kumabus_vpos_update.bin
熊本都市バス(TripUpdate) https://km.bus-vision.jp/realtime/toshibus_trip_update.bin
熊本都市バス(VehiclePosition) https://km.bus-vision.jp/realtime/toshibus_vpos_update.bin

※15秒おきに更新されますが、過度なアクセスは行わないようにしてください。

参考文献

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?