ridelogic_avls.py 4.6 KB

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