gtfs_example_write.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/python3
  2. #
  3. from google.transit import gtfs_realtime_pb2 as gtfspb
  4. import urllib.request
  5. import sys
  6. def gtfs_vehicle_position(data, vp = None):
  7. if vp is None:
  8. vp = gtfspb.VehiclePosition()
  9. if "trip" in data:
  10. trip = data["trip"]
  11. if "trip_id" in trip: vp.trip.trip_id = trip["trip_id"]
  12. if "route_id" in trip: vp.trip.route_id = trip["route_id"]
  13. if "direction_id" in trip: vp.trip.direction_id = trip["direction_id"]
  14. if "start_time" in trip: vp.trip.start_time = trip["start_time"]
  15. if "start_date" in trip: vp.trip.start_date = trip["start_date"]
  16. if "schedule_relationship" in trip: vp.trip.schedule_relationship = trip["schedule_relationship"]
  17. #vp.trip.schedule_relationship = gtfspb.TripDescriptor.ScheduleRelationship.SCHEDULED
  18. if "stop_id" in data: vp.stop_id = data["stop_id"]
  19. if "current_stop_sequence" in data: vp.current_stop_sequence = data["current_stop_sequence"]
  20. if "current_state" in data: vp.current_status = data["current_state"]
  21. #vp.current_status = gtfspb.VehiclePosition.VehicleStopStatus.INCOMING_AT
  22. if "timestamp" in data: vp.timestamp = data["timestamp"]
  23. if "congestion_level" in data: vp.congestion_level = data["congestion_level"]
  24. if "occupancy_status" in data: vp.occupancy_status = data["occupancy_status"]
  25. #vp.congestion_level = gtfspb.VehiclePosition.CongestionLevel.UNKNOWN_CONGESTION_LEVEL
  26. #vp.occupancy_status = gtfspb.VehiclePosition.OccupancyStatus.NO_DATA_AVAILABLE
  27. if "occupancy_percentage" in data: vp.occupancy_percentage = data["occupancy_percentage"]
  28. if "vehicle_id" in data: vp.vehicle.id = data["vehicle_id"]
  29. if "label" in data: vp.vehicle.label = data["label"]
  30. if "license_plate" in data: vp.vehicle.license_plate = data["license_plate"]
  31. if "position" in data:
  32. p = data["position"]
  33. if "latitude" in p: vp.position.latitude = p["latitude"]
  34. if "longitude" in p: vp.position.longitude = p["longitude"]
  35. if "bearing" in p: vp.position.bearing = p["bearing"]
  36. if "odometer" in p: vp.position.odometer = p["odometer"]
  37. if "speed" in p: vp.position.speed = p["speed"]
  38. return vp
  39. def gtfs_alert():
  40. a = gtfspb.Alert()
  41. return a
  42. def gtfs_trip_update():
  43. u = gtfspb.TripUpdate()
  44. return u
  45. def gtfs_feed_entity(data, f = None):
  46. if f is None:
  47. f = gtfspb.FeedEntity()
  48. if "id" in data: f.id = data["id"]
  49. #if not ("#type" in data): return f
  50. #if data["#type"] == "VehiclePosition":
  51. if "vehicle" in data:
  52. gtfs_vehicle_position(data["vehicle"], f.vehicle)
  53. return f
  54. def gtfs_feed_message(data, fm = None):
  55. if fm is None:
  56. fm = gtfspb.FeedMessage()
  57. if "header" in data:
  58. h = data["header"]
  59. if "gtfs_realtime_version" in h: fm.header.gtfs_realtime_version = h["gtfs_realtime_version"]
  60. if "timestamp" in h: fm.header.timestamp = h["timestamp"]
  61. if "entity" in data:
  62. for ent in data["entity"]:
  63. gtfs_feed_entity(ent, fm.entity.add())
  64. return fm
  65. ###
  66. ###
  67. ###
  68. vehicle_position_data = {
  69. "trip" : {
  70. "trip_id": "testid4",
  71. "route_id": "testroute3",
  72. "direction_id": 0,
  73. "start_time": "teststartime...",
  74. "start_date": "teststartdate...",
  75. "schedule_relationship": gtfspb.TripDescriptor.ScheduleRelationship.SCHEDULED
  76. },
  77. "stop_id" : "teststop",
  78. "curent_stop_sequence": 3,
  79. "current_state" : gtfspb.VehiclePosition.VehicleStopStatus.INCOMING_AT,
  80. "timestamp" : 123,
  81. "congestion_level" : gtfspb.VehiclePosition.CongestionLevel.UNKNOWN_CONGESTION_LEVEL,
  82. "occupancy_status" : gtfspb.VehiclePosition.OccupancyStatus.NO_DATA_AVAILABLE,
  83. "occupancy_percentage" : 1,
  84. "vehicle_id" : "testvehicleid0",
  85. "label" : "testlabel",
  86. "license_plate" : "testlicenseplate",
  87. "position" : {
  88. "latitude" : 42.9,
  89. "longitude" : -78.1,
  90. "bearing" : 90.0,
  91. "odometer" : 1.0,
  92. "speed" : 0.1
  93. }
  94. }
  95. feed_entity_data = {
  96. "id" : "testfeedid",
  97. "#type": "VehiclePosition",
  98. "vehicle": vehicle_position_data
  99. }
  100. feed_message_data = {
  101. "header" : {
  102. "gtfs_realtime_version" : "2.0",
  103. "timestamp" : 0
  104. },
  105. "entity" : [
  106. feed_entity_data,
  107. feed_entity_data
  108. ]
  109. }
  110. ###
  111. ###
  112. ###
  113. vp = gtfs_vehicle_position(vehicle_position_data)
  114. vp.stop_id = "ok"
  115. print(">>>\n", vp)
  116. fe = gtfs_feed_entity(feed_entity_data)
  117. print("feed entity>>>\n", fe)
  118. feedmsg = gtfs_feed_message(feed_message_data)
  119. print("feed message>>>\n", feedmsg)
  120. fp = open("outpy.pb", "wb")
  121. fp.write(feedmsg.SerializeToString())
  122. fp.close()