123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- header('Content-Type: application/json');
- function distanceMeters($lat1, $lon1, $lat2, $lon2)
- {
- $x = deg2rad( $lon1 - $lon2 ) * cos( deg2rad( $lat1 ) );
- $y = deg2rad( $lat1 - $lat2 );
- $dist = 6371000.0 * sqrt( $x*$x + $y*$y );
-
- return $dist;
- }
- $db = file('db.txt');
- $db2 = file('db2.txt');
-
- $sum_of_lat = 0;
- $sum_of_long = 0;
- $sum_of_time = 0;
-
- $sum_of_lat2 = 0;
- $sum_of_long2 = 0;
- $sum_of_time2 = 0;
-
- $array_time = array();
- $array_time2 = array();
-
- $average = new stdClass;
- $average->latitude = 0;
- $average->longitude = 0;
- $average->inTime = 0;
- $average->parada = '';
-
- $average2 = new stdClass;
- $average2->latitude = 0;
- $average2->longitude = 0;
- $average2->inTime = 0;
- $average2->parada = '';
-
- $db_size = count($db);
- $db2_size = count($db2);
- $distances=[];
- $distances2 = [];
-
- $paradas = array(
- "Generales" => array(18.404916, -66.044828),
- "Natu1" => array(18.403663, -66.045529),
- "Natu2" => array(18.403769, -66.046572),
- "Centro" => array( 18.403898, -66.048255),
- "Biblio" => array(18.403994, -66.049781),
- "Merenderos" => array(18.406345, -66.050197),
- "leyes" => array (18.406069, -66.047733),
- "Complejo" => array(18.406503, -66.046121),
- "rotc_EntrR" => array(18.406083, -66.043605),
- "rotc_Est" => array( 18.407121, -66.042053),
- "Museo" => array(18.403917, -66.050996),
- "FacundaB" => array(18.401992, -66.050611),
- "Educacion" => array(18.402077, -66.047800),
- "MultiP" => array(18.402133, -66.045014)
- );
-
- for ($i = 0; $i < $db_size; $i++)
- {
- $decoded_location = json_decode($db[$i]);
- $sum_of_lat += $decoded_location->latitude;
- $sum_of_long += $decoded_location->longitude;
- array_push($array_time,$decoded_location->inTime);
- if($i+1 == $db_size)
- {
- $average->latitude = $sum_of_lat/$db_size;
- $average->longitude = $sum_of_long/$db_size;
- $average->inTime = date("h:i:s a",(array_sum(array_map('strtotime',$array_time)))/$db_size);
- }
- }
-
- for ($i = 0; $i < $db2_size; $i++)
- {
- $decoded_location2 = json_decode($db2[$i]);
- $sum_of_lat2 += $decoded_location2->latitude;
- $sum_of_long2 += $decoded_location2->longitude;
- array_push($array_time2,$decoded_location2->inTime);
- if($i+1 == $db2_size)
- {
- $average2->latitude = $sum_of_lat2/$db2_size;
- $average2->longitude = $sum_of_long2/$db2_size;
- $average2->inTime = date("h:i:s a",(array_sum(array_map('strtotime',$array_time2)))/$db2_size);
- }
- }
-
- foreach($paradas as &$coso)
- {
- $plo2 = distanceMeters($average2->latitude, $average2->longitude, $coso[0], $coso[1]);
- array_push($distances2,$plo2);
- //echo $distances;
-
- }
-
-
- foreach($paradas as &$thing)
- {
- $plo = distanceMeters($average->latitude, $average->longitude, $thing[0], $thing[1]);
- array_push($distances,$plo);
- //echo $distances;
-
- }
-
-
- $indice = array_search(min($distances),$distances);
- $indice2 = array_search(min($distances2),$distances2);
-
- $checking = array_keys($paradas);
-
-
- $average->parada = $checking[$indice];
- $average2->parada = $checking[$indice2];
-
- $encoded_average = json_encode($average);
- $encoded_average2 = json_encode($average2);
- // echo $encoded_average;
-
- $encoded_average = json_decode($encoded_average);
- $encoded_average2 = json_decode($encoded_average2);
-
- if (!empty($encoded_avarage2)){
- $jsoncito = lcfirst($encoded_average->parada) . "_" . ucfirst($encoded_average->parada);
- }
- elseif(!empty($encoded_average)){
- $jsoncito = lcfirst($encoded_average2->parada) . "_" . ucfirst($encoded_average2->parada);
- }
- else{
- $jsoncito = lcfirst($encoded_average->parada) . "_" . ucfirst($encoded_average2->parada);
- }
-
- $paquete -> parada = $jsoncito;
- date_default_timezone_set('America/Puerto_Rico');
-
- $currenttime = date('h:i:s');
-
- $paquete -> tiempo = $currenttime;
-
- $encoded_paquete = json_encode($paquete);
-
- echo $encoded_paquete;
-
- ?>
|