GTFS RealtimeをPythonで読み込む
GTFS Realtimeはバイナリデータです。Pythonであればgtfs-realtime-bindings
というライブラリを使ってデータを展開することができます。
gtfs-realtime-bindingsのインストール
次のコマンドでgtfs-realtime-bindings
をインストールします。
bash
$ pip install --upgrade gtfs-realtime-bindings
データの展開
次の広島県のGTFS Realtimeデータを展開してみます。
次のコードでデータを展開できます。
python
from google.transit import gtfs_realtime_pb2
import urllib.request, urllib.error
feed = gtfs_realtime_pb2.FeedMessage()
url = "https://ajt-mobusta-gtfs.mcapps.jp/realtime/11/vehicle_position.bin" # GTFS Realtime url
with urllib.request.urlopen(url) as res:
feed.ParseFromString(res.read())
print(feed)
header {
gtfs_realtime_version: "1.0"
incrementality: FULL_DATASET
timestamp: 1694429284
}
entity {
id: "ac2686d0-aad6-4e99-acce-4c9ecdceaf97"
is_deleted: false
vehicle {
trip {
trip_id: "5ee8068b-63a2-4f33-9b70-260ce9343bed"
schedule_relationship: SCHEDULED
route_id: "3093754835"
}
position {
latitude: 34.4449348449707
longitude: 132.6889190673828
bearing: 179.0
speed: 9.0
}
timestamp: 1694429269
vehicle {
id: "3152"
}
}
}
.
.
.
header
やentity
は次のようにして取得できます。
python
from google.transit import gtfs_realtime_pb2
import urllib.request, urllib.error
feed = gtfs_realtime_pb2.FeedMessage()
url = "https://ajt-mobusta-gtfs.mcapps.jp/realtime/11/vehicle_position.bin"
with urllib.request.urlopen(url) as res:
feed.ParseFromString(res.read())
print('*****header*****')
print(feed.header)
print('*****entity*****')
print(feed.entity)
*****header*****
gtfs_realtime_version: "1.0"
incrementality: FULL_DATASET
timestamp: 1694429406
*****entity*****
[id: "ac2686d0-aad6-4e99-acce-4c9ecdceaf97"
is_deleted: false
vehicle {
trip {
trip_id: "5ee8068b-63a2-4f33-9b70-260ce9343bed"
schedule_relationship: SCHEDULED
route_id: "3093754835"
}
position {
latitude: 34.4449348449707
longitude: 132.6889190673828
bearing: 179.0
speed: 9.0
}
timestamp: 1694429269
vehicle {
id: "3152"
}
}
, id: "e79e318b-1875-4100-8f61-973077830d5a"
is_deleted: false
.
.
.
]
entity
はlist型であることがわかります。
参考
AlloyDB
Amazon Cognito
Amazon EC2
Amazon ECS
Amazon QuickSight
Amazon RDS
Amazon Redshift
Amazon S3
API
Autonomous Vehicle
AWS
AWS API Gateway
AWS Chalice
AWS Control Tower
AWS IAM
AWS Lambda
AWS VPC
BERT
BigQuery
Causal Inference
ChatGPT
Chrome Extension
CircleCI
Classification
Cloud Functions
Cloud IAM
Cloud Run
Cloud Storage
Clustering
CSS
Data Engineering
Data Modeling
Database
dbt
Decision Tree
Deep Learning
Descriptive Statistics
Differential Equation
Dimensionality Reduction
Discrete Choice Model
Docker
Economics
FastAPI
Firebase
GIS
git
GitHub
GitHub Actions
Google
Google Cloud
Google Search Console
Hugging Face
Hypothesis Testing
Inferential Statistics
Interval Estimation
JavaScript
Jinja
Kedro
Kubernetes
LightGBM
Linux
LLM
Mac
Machine Learning
Macroeconomics
Marketing
Mathematical Model
Meltano
MLflow
MLOps
MySQL
NextJS
NLP
Nodejs
NoSQL
ONNX
OpenAI
Optimization Problem
Optuna
Pandas
Pinecone
PostGIS
PostgreSQL
Probability Distribution
Product
Project
Psychology
Python
PyTorch
QGIS
R
ReactJS
Regression
Rideshare
SEO
Singer
sklearn
Slack
Snowflake
Software Development
SQL
Statistical Model
Statistics
Streamlit
Tabular
Tailwind CSS
TensorFlow
Terraform
Transportation
TypeScript
Urban Planning
Vector Database
Vertex AI
VSCode
XGBoost