Repositorio del curso CCOM4030 el semestre B91 del proyecto Trolley

sender.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. header('Content-Type: application/json');
  3. function distanceMeters($lat1, $lon1, $lat2, $lon2)
  4. {
  5. $x = deg2rad( $lon1 - $lon2 ) * cos( deg2rad( $lat1 ) );
  6. $y = deg2rad( $lat1 - $lat2 );
  7. $dist = 6371000.0 * sqrt( $x*$x + $y*$y );
  8. return $dist;
  9. }
  10. $db = file('db.txt');
  11. $db2 = file('db2.txt');
  12. $sum_of_lat = 0;
  13. $sum_of_long = 0;
  14. $sum_of_time = 0;
  15. $sum_of_lat2 = 0;
  16. $sum_of_long2 = 0;
  17. $sum_of_time2 = 0;
  18. $array_time = array();
  19. $array_time2 = array();
  20. $average = new stdClass;
  21. $average->latitude = 0;
  22. $average->longitude = 0;
  23. $average->inTime = 0;
  24. $average->parada = '';
  25. $average2 = new stdClass;
  26. $average2->latitude = 0;
  27. $average2->longitude = 0;
  28. $average2->inTime = 0;
  29. $average2->parada = '';
  30. $db_size = count($db);
  31. $db2_size = count($db2);
  32. $distances=[];
  33. $distances2 = [];
  34. $paradas = array(
  35. "Generales" => array(18.404916, -66.044828),
  36. "Natu1" => array(18.403663, -66.045529),
  37. "Natu2" => array(18.403769, -66.046572),
  38. "Centro" => array( 18.403898, -66.048255),
  39. "Biblio" => array(18.403994, -66.049781),
  40. "Merenderos" => array(18.406345, -66.050197),
  41. "leyes" => array (18.406069, -66.047733),
  42. "Complejo" => array(18.406503, -66.046121),
  43. "rotc_EntrR" => array(18.406083, -66.043605),
  44. "rotc_Est" => array( 18.407121, -66.042053),
  45. "Museo" => array(18.403917, -66.050996),
  46. "FacundaB" => array(18.401992, -66.050611),
  47. "Educacion" => array(18.402077, -66.047800),
  48. "MultiP" => array(18.402133, -66.045014)
  49. );
  50. for ($i = 0; $i < $db_size; $i++)
  51. {
  52. $decoded_location = json_decode($db[$i]);
  53. $sum_of_lat += $decoded_location->latitude;
  54. $sum_of_long += $decoded_location->longitude;
  55. array_push($array_time,$decoded_location->inTime);
  56. if($i+1 == $db_size)
  57. {
  58. $average->latitude = $sum_of_lat/$db_size;
  59. $average->longitude = $sum_of_long/$db_size;
  60. $average->inTime = date("h:i:s a",(array_sum(array_map('strtotime',$array_time)))/$db_size);
  61. }
  62. }
  63. for ($i = 0; $i < $db2_size; $i++)
  64. {
  65. $decoded_location2 = json_decode($db2[$i]);
  66. $sum_of_lat2 += $decoded_location2->latitude;
  67. $sum_of_long2 += $decoded_location2->longitude;
  68. array_push($array_time2,$decoded_location2->inTime);
  69. if($i+1 == $db2_size)
  70. {
  71. $average2->latitude = $sum_of_lat2/$db2_size;
  72. $average2->longitude = $sum_of_long2/$db2_size;
  73. $average2->inTime = date("h:i:s a",(array_sum(array_map('strtotime',$array_time2)))/$db2_size);
  74. }
  75. }
  76. foreach($paradas as &$coso)
  77. {
  78. $plo2 = distanceMeters($average2->latitude, $average2->longitude, $coso[0], $coso[1]);
  79. array_push($distances2,$plo2);
  80. //echo $distances;
  81. }
  82. foreach($paradas as &$thing)
  83. {
  84. $plo = distanceMeters($average->latitude, $average->longitude, $thing[0], $thing[1]);
  85. array_push($distances,$plo);
  86. //echo $distances;
  87. }
  88. $indice = array_search(min($distances),$distances);
  89. $indice2 = array_search(min($distances2),$distances2);
  90. $checking = array_keys($paradas);
  91. $average->parada = $checking[$indice];
  92. $average2->parada = $checking[$indice2];
  93. $encoded_average = json_encode($average);
  94. $encoded_average2 = json_encode($average2);
  95. // echo $encoded_average;
  96. $encoded_average = json_decode($encoded_average);
  97. $encoded_average2 = json_decode($encoded_average2);
  98. if (!empty($encoded_avarage2)){
  99. $jsoncito = lcfirst($encoded_average->parada) . "_" . ucfirst($encoded_average->parada);
  100. }
  101. elseif(!empty($encoded_average)){
  102. $jsoncito = lcfirst($encoded_average2->parada) . "_" . ucfirst($encoded_average2->parada);
  103. }
  104. else{
  105. $jsoncito = lcfirst($encoded_average->parada) . "_" . ucfirst($encoded_average2->parada);
  106. }
  107. $paquete -> parada = $jsoncito;
  108. date_default_timezone_set('America/Puerto_Rico');
  109. $currenttime = date('h:i:s');
  110. $paquete -> tiempo = $currenttime;
  111. $encoded_paquete = json_encode($paquete);
  112. echo $encoded_paquete;
  113. ?>