synthetic_avls_source.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/python3
  2. # simple test program to generate synthetic bus position
  3. # data (AVLS)
  4. import json
  5. import sys
  6. import time
  7. import math
  8. import ridelogic_avls as rl
  9. if len(sys.argv) < 3:
  10. print("provide path and stop geojson files")
  11. sys.exit(0)
  12. json_path_fn = sys.argv[1]
  13. json_stop_fn = sys.argv[2]
  14. json_path = {}
  15. json_stop = {}
  16. with open(json_path_fn) as fp:
  17. json_path = json.loads(fp.read())
  18. with open(json_stop_fn) as fp:
  19. json_stop = json.loads(fp.read())
  20. _path = json_path["features"][0]["geometry"]["coordinates"]
  21. bus_ctx = {
  22. "update_freq" : 1000,
  23. "gps_path": _path,
  24. "bus": [
  25. {
  26. "id": "bus0",
  27. "idx" : 0,
  28. "idx_dir" : 1,
  29. "bearing":0,
  30. "longitude": _path[0][0],
  31. "latitude": _path[0][1]
  32. },
  33. {
  34. "id":"bus1",
  35. "idx": len(_path)-1,
  36. "idx_dir" : -1,
  37. "bearing":0,
  38. "longitude": _path[-1][0],
  39. "latitude": _path[-1][1]
  40. }
  41. ]
  42. }
  43. while True:
  44. feed_msg = {
  45. "header" : { "gtfs_realtime_version":"2.0", "timestamp":int(time.time()) },
  46. "entity":[]
  47. }
  48. for bus in bus_ctx["bus"]:
  49. print(bus["id"], bus["longitude"], bus["latitude"], bus["bearing"])
  50. lng_prv = bus["longitude"]
  51. lat_prv = bus["latitude"]
  52. bus["longitude"] = bus_ctx["gps_path"][bus["idx"]][0]
  53. bus["latitude"] = bus_ctx["gps_path"][bus["idx"]][1]
  54. bus["idx"] += bus["idx_dir"]
  55. if bus["idx"] < 0:
  56. bus["idx"] = 0
  57. bus["idx_dir"] = 1
  58. if bus["idx"] >= len(bus_ctx["gps_path"]):
  59. bus["idx"] = len(bus_ctx["gps_path"])-1
  60. bus["idx_dir"] = -1
  61. lng_cur = bus["longitude"]
  62. lat_cur = bus["latitude"]
  63. bus["bearing"] = int(180.0 * math.atan2(lng_cur - lng_prv, lat_cur - lat_prv) / math.pi)
  64. entity = {
  65. "id" : bus["id"],
  66. "vehicle" : {
  67. "position" : {
  68. "longitude":lng_cur,
  69. "latitude":lat_cur,
  70. "bearing":bus["bearing"]
  71. }
  72. }
  73. }
  74. feed_msg["entity"].append(entity);
  75. rl.write_protobuf("test_gtfs.pb", feed_msg)
  76. time.sleep( float(bus_ctx["update_freq"])/1000.0 )