export_bus_files.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/php
  2. <?php
  3. //echo getcwd() . "\n";
  4. //exit;
  5. include 'connect.php';
  6. //----------------------------extract the drivers
  7. $res=mysqli_query($sql,'SELECT id, pin, name FROM drivers');
  8. $f=fopen("drivers.txt","wb");
  9. fwrite($f,"#driver\tpin\tname\n");
  10. while($row=mysqli_fetch_assoc($res))
  11. {
  12. fwrite($f,sprintf("%d\t%s\t%s\n",$row['id'], $row['pin'], $row['name']));
  13. }
  14. fclose($f);
  15. //----------------------------extract the paddles
  16. $res=mysqli_query($sql,'SELECT live_paddles.*, live_stops.latitude, live_stops.longitude, live_stops.name FROM live_paddles,live_stops WHERE live_paddles.stopid=live_stops.id ORDER BY live_paddles.id,slot;');
  17. $paddle=array();
  18. while($row=mysqli_fetch_assoc($res))
  19. {
  20. $paddle[]=$row;
  21. }
  22. for($i=0;$i < count($paddle); $i++)
  23. {
  24. if(strlen($paddle[$i]['arrival']) == 0)
  25. {
  26. for($j=$i;$j < count($paddle); $j++)
  27. {
  28. if(strlen($paddle[$j]['arrival'] > 0))
  29. {
  30. $firsttime=array();
  31. preg_match('/([\d]+):([\d]+)/',$lastarr,$firsttime);
  32. $lasttime=array();
  33. preg_match('/([\d]+):([\d]+)/',$paddle[$j]['arrival'],$lasttime);
  34. $ft_min=$firsttime[2] + ($firsttime[1] * 60);
  35. $lt_min=$lasttime[2] + ($lasttime[1] * 60);
  36. if($lt_min < $ft_min)
  37. $lt_min += (60 * 24);
  38. $timedist=$lt_min - $ft_min;
  39. $stopdist=1 + ($j - $i);
  40. $rate=floatval($timedist) / floatval($stopdist);
  41. $t=$i-1;
  42. echo "Interpolation plan: From $ft_min to $lt_min in $stopdist stops ( $i >= k < $j ) at $rate/stop\n";
  43. for($k=0; $k < $stopdist; $k++)
  44. {
  45. $temp=$ft_min + ($rate * ($k+1));
  46. $hours=(intval($temp) / 60) % 24;
  47. $mins=intval($temp) % 60;
  48. $arr=sprintf("%02d:%02d\n",$hours,$mins);
  49. $paddle[$i + $k]['arrival']=$arr;
  50. }
  51. break;
  52. }
  53. }
  54. }
  55. else
  56. {
  57. $lastarr=$paddle[$i]['arrival'];
  58. }
  59. }
  60. $current=false;
  61. $f=false;
  62. foreach($paddle as $row)
  63. {
  64. if($current != $row['id'])
  65. {
  66. if($f)
  67. fclose($f);
  68. $current=$row['id'];
  69. echo "Constructing paddle $current.paddle:\n";
  70. $f=fopen($current . ".paddle","wb");
  71. fwrite($f,sprintf("#Paddle %d generated %s\n",$current,strftime("%D %T")));
  72. fwrite($f,"#scheduled arrival | location | stop data\n");
  73. fwrite($f,"#hour mintute latitude longitude route trip stop name\n");
  74. }
  75. if(!$f)
  76. {
  77. die("Cannot open file $current.paddle!\n");
  78. }
  79. $tm=array();
  80. preg_match('/([\d]+):([\d]+)/',$row['arrival'],$tm);
  81. if(!isset($tm[1]))
  82. {
  83. $tm[1]='0';
  84. $tm[2]='0';
  85. }
  86. fwrite($f,sprintf("%d\t%d\t%f\t%f\t%d\t%d\t%d\t%s\n",
  87. $tm[1],$tm[2],$row['latitude'],$row['longitude'],$row['route'],$row['trip'],$row['stop'],$row['name']));
  88. }
  89. if($f)
  90. fclose($f);
  91. ?>