Lets make our own GTFS-RT VehiclePosition feed

In [1]:
from google.transit import gtfs_realtime_pb2
from google.protobuf.json_format import MessageToDict
from google.protobuf.json_format import MessageToJson
from google.protobuf.json_format import ParseDict
import requests
import json
import pandas as pd
from collections import OrderedDict
import datetime
In [2]:
newdict = {
    'header': {'gtfsRealtimeVersion': '2.0',
        'incrementality': 'FULL_DATASET',
        'timestamp': '1531806015'
    },
    'entity': [{
        'id': 'y1897',
        'vehicle': {
            'trip': {
                'tripId': '37696116',
                'startDate': '20180717',
                'scheduleRelationship': 'UNSCHEDULED',
                'routeId': '47',
                'directionId': 1
            },
            'position': {
                'latitude': 42.33042907714844,
                'longitude': -71.08306884765625,
                'bearing': 0.0
            },
            'timestamp': '1531805943',
            'vehicle': {'id': 'y1897', 'label': '1897'}
        }
    }]
}
In [3]:
feed3 = gtfs_realtime_pb2.FeedMessage()
In [4]:
# now, use the ParseDict function to check if the data has been encoded properly
ParseDict(newdict, feed3)
Out[4]:
header {
  gtfs_realtime_version: "2.0"
  incrementality: FULL_DATASET
  timestamp: 1531806015
}
entity {
  id: "y1897"
  vehicle {
    trip {
      trip_id: "37696116"
      start_date: "20180717"
      schedule_relationship: UNSCHEDULED
      route_id: "47"
      direction_id: 1
    }
    position {
      latitude: 42.33042907714844
      longitude: -71.08306884765625
      bearing: 0.0
    }
    timestamp: 1531805943
    vehicle {
      id: "y1897"
      label: "1897"
    }
  }
}
In [13]:
# write it out to disk
with open('VehiclePositions.pb','wb') as f:
	f.write(feed3.SerializeToString())

VehiclePositions.pb of GTFS-Realtime thus created from json data.

https://developers.google.com/transit/gtfs-realtime/guides/vehicle-positions