Advertisement
Guest User

Untitled

a guest
Nov 15th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.49 KB | None | 0 0
  1. <?php
  2. define('DB_HOST', 'localhost');
  3. define('DB_NAME', 'trains_geo');
  4. define('DB_USER', 'root');
  5. define('DB_PASSWORD', '');
  6. define('DB_ENCODING', 'utf8');
  7. define('DB_CHARSET', 'utf8');
  8.  
  9. class DbConnect {
  10.  
  11. private $db_host = DB_HOST;
  12. private $db_name = DB_NAME;
  13. private $db_user = DB_USER;
  14. private $db_password = DB_PASSWORD;
  15. private $db_encoding = DB_ENCODING;
  16. private $db_charset = DB_CHARSET;
  17.  
  18. /**
  19. * PDO instance
  20. * @var PDO
  21. */
  22. private $pdo = null;
  23.  
  24. /**
  25. * Open the database connection
  26. */
  27. public function __construct() {
  28. // open database connection
  29. $conStr = sprintf("mysql:host=%s;dbname=%s;encoding:%s;charset=%s;", $this->db_host, $this->db_name, $this->db_encoding, $this->db_charset);
  30.  
  31. try {
  32. $this->pdo = new PDO($conStr, $this->db_user, $this->db_password);
  33. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  34. } catch (PDOException $e) {
  35. echo $e->getMessage();
  36. }
  37. }
  38.  
  39. public function getPdo() {
  40. return $this->pdo;
  41. }
  42.  
  43. /**
  44. * close the database connection
  45. */
  46. public function __destruct() {
  47. // close the database connection
  48. $this->pdo = null;
  49. }
  50.  
  51. }
  52.  
  53. class DrawMapStructure {
  54.  
  55. private $pdo = null;
  56. private $spatialDataPolylines = array();
  57. private $spatialDataStationsPoints = array();
  58. private $spatialDataCurrentConnectionLine = array();
  59. private $spatialDataCurrentConnectionSpatialLine = array();
  60. private $stationSpiderLines = array();
  61. private $distances = array();
  62. private $simplePathStations = array();
  63. private $stationLineIntersects = array();
  64. private $lastCheckedLineIds = array();
  65. private $floatPrecision = 10;
  66. private $lineId = 0;
  67. private $checkSLI = true;
  68. public $dijkstrasRouteEngine = array();
  69. private $SRP = array();
  70. public $SRPStations;
  71. public $SRPSections;
  72. public $totalCost;
  73. private $previousTimeArr = array();
  74. private $stationsIds = array();
  75.  
  76. public function __construct() {
  77. $dbConnect = new DbConnect();
  78. $this->pdo = $dbConnect->getPdo();
  79. }
  80.  
  81. public function getRailwaysPolylines() {
  82. $sql = 'SELECT `id`, ST_AsGeoJSON(`line`, :floatPrecision) AS `line` FROM `connections`';
  83. $stmt = $this->pdo->prepare($sql);
  84. $stmt->bindParam(':floatPrecision', $this->floatPrecision, PDO::PARAM_INT);
  85. $stmt->execute();
  86.  
  87. while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  88. $this->spatialDataPolylines[$row['id']] = json_decode($row['line'], true);
  89. }
  90.  
  91. return $this->spatialDataPolylines;
  92. }
  93.  
  94. public function getStationsPoints() {
  95. $sql = 'SELECT `id`, `name`, ST_AsGeoJSON(`point`, :floatPrecision) AS `point` FROM `stations`';
  96.  
  97. $stmt = $this->pdo->prepare($sql);
  98. $stmt->bindParam(':floatPrecision', $this->floatPrecision, PDO::PARAM_INT);
  99. $stmt->execute();
  100.  
  101. while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  102. $this->spatialDataStationsPoints[$row['id']] = json_decode($row['point'], true);
  103. $this->spatialDataStationsPoints[$row['id']]['name'] = $row['name'];
  104. }
  105.  
  106. return $this->spatialDataStationsPoints;
  107. }
  108.  
  109. public function getStationPoint($stationId, $stationPointJSON = false) {
  110. if($stationPointJSON) {
  111. $sql = 'SELECT ST_AsGeoJSON(`point`, :floatPrecision) AS `pointJSON` FROM `stations` WHERE `id`=:id';
  112.  
  113. $stmt = $this->pdo->prepare($sql);
  114. $stmt->bindParam(':floatPrecision', $this->floatPrecision, PDO::PARAM_INT);
  115. $stmt->bindParam(':id', $stationId, PDO::PARAM_INT);
  116. $stmt->execute();
  117.  
  118. return json_decode($stmt->fetch(PDO::FETCH_ASSOC)['pointJSON'], true);
  119. } else {
  120. $sql = 'SELECT `point` FROM `stations` WHERE `id`=:id';
  121.  
  122. $stmt = $this->pdo->prepare($sql);
  123. $stmt->bindParam(':id', $stationId, PDO::PARAM_INT);
  124. $stmt->execute();
  125.  
  126. return $stmt->fetch(PDO::FETCH_ASSOC)['point'];
  127. }
  128. }
  129.  
  130. public function getCurrentConnectionLine($beginStationId, $endStationId) {
  131. $sql = 'SELECT s.`id` AS `station_id`, s.`name`, c.`id` AS `line_id`, ST_AsGeoJSON(c.`line`, :floatPrecision) AS `currentLineSpatialJSONData`
  132. FROM `connections` c
  133. JOIN `stations` s ON (ST_Intersects(s.`point`, c.`line`) = 1)
  134. WHERE (ST_INTERSECTS(c.`line`, :station1)
  135. OR ST_INTERSECTS(c.`line`, :station2))
  136. GROUP BY c.`id`';
  137.  
  138. $stmt = $this->pdo->prepare($sql);
  139. $stmt->bindParam(':floatPrecision', $this->floatPrecision, PDO::PARAM_INT);
  140. $stmt->bindValue(':station1', $this->getStationPoint($beginStationId));
  141. $stmt->bindValue(':station2', $this->getStationPoint($endStationId));
  142. $stmt->execute();
  143.  
  144. while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  145. $this->spatialDataCurrentConnectionLine[$row['line_id']] = json_decode($row['currentLineSpatialJSONData'], true);
  146. }
  147.  
  148. return $this->spatialDataCurrentConnectionLine;
  149. }
  150.  
  151. public function getDistanceLineOfTwoStations($s1, $s2) {
  152. $sql = 'SELECT
  153. ST_Length(
  154. ST_LineStringFromWKB(
  155. LineString(
  156. (SELECT `point` FROM `stations` WHERE `id` = :s1Id),
  157. (SELECT `point` FROM `stations` WHERE `id` = :s2Id)
  158. )
  159. )
  160. ) AS `distance`';
  161.  
  162. $stmt = $this->pdo->prepare($sql);
  163. $stmt->bindValue(':s1Id', $s1, PDO::PARAM_INT);
  164. $stmt->bindValue(':s2Id', $s2, PDO::PARAM_INT);
  165. $stmt->execute();
  166.  
  167. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  168.  
  169. return $row['distance'];
  170. }
  171.  
  172. public function getStationSpiderLines($stationId, $getDistanceOnly = false) {
  173. if(count($this->simplePathStations) === 0) {
  174. $this->simplePathStations[] = $stationId;
  175. }
  176.  
  177. $sql = 'SELECT
  178. `id`,
  179. ST_AsGeoJSON(`point`, :floatPrecision) AS `secondpoint`,
  180. ST_Length(
  181. ST_LineStringFromWKB(
  182. LineString(
  183. `point`,
  184. (SELECT `point` FROM `stations` WHERE `id` = :stationId)
  185. )
  186. )
  187. )
  188. AS `distance`,
  189. (SELECT ST_AsGeoJSON(`point`, :floatPrecision) FROM `stations` WHERE `id` = :stationId) AS `firstpoint`
  190. FROM `stations`
  191. WHERE `id` <> :stationId
  192. ORDER BY `distance`';
  193.  
  194. $stmt = $this->pdo->prepare($sql);
  195. $stmt->bindParam(':floatPrecision', $this->floatPrecision, PDO::PARAM_INT);
  196. $stmt->bindValue(':stationId', $stationId, PDO::PARAM_INT);
  197. $stmt->execute();
  198.  
  199. $this->distances = [];
  200. $this->stationSpiderLines = [];
  201.  
  202. if($getDistanceOnly) {
  203. while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  204. $this->distances[$row['id']] = $row['distance'];
  205. }
  206.  
  207. return $this->distances;
  208. }
  209.  
  210. while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  211. $this->stationSpiderLines['secondpoint'][] = json_decode($row['secondpoint'], true);
  212. $this->stationSpiderLines['firstpoint'] = json_decode($row['firstpoint'], true);
  213. }
  214.  
  215. return $this->stationSpiderLines;
  216. }
  217.  
  218. public function checkStationLineIntersects($stationId) {
  219. $this->stationLineIntersects = [];
  220.  
  221. $sql = 'SELECT `id` FROM `connections` WHERE ST_INTERSECTS((SELECT `point` FROM `stations` WHERE `id` = :stationId), `line`)';
  222.  
  223. $stmt = $this->pdo->prepare($sql);
  224. $stmt->bindValue(':stationId', $stationId, PDO::PARAM_INT);
  225. $stmt->execute();
  226.  
  227. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  228. $this->stationLineIntersects[] = $row['id'];
  229. }
  230.  
  231. return $this->stationLineIntersects;
  232. }
  233.  
  234. public function getSimplePath($distances, $targetStationId) {
  235. //var_dump($distances);
  236. $this->lastCheckedLineIds = $this->checkStationLineIntersects(end($this->simplePathStations));
  237. //var_dump($this->lastCheckedLineIds);
  238.  
  239. foreach($distances as $key => $val) {
  240. // echo 'key:';
  241. // var_dump($this->checkStationLineIntersects($key));
  242. // echo 'last:';
  243. // var_dump($this->lastCheckedLineIds[0]);
  244. //if($this->checkStationLineIntersects($key)[0] != $this->lastCheckedLineIds[$this->lineId]) {
  245. /*var_dump($key.' > '.$targetStationId);
  246. var_dump($this->getDistanceLineOfTwoStations($key, $targetStationId));
  247. var_dump($distances[$targetStationId]);
  248. var_dump($distances);*/
  249.  
  250. if(!in_array($this->lastCheckedLineIds[$this->lineId], $this->checkStationLineIntersects($key)) && $this->checkSLI === true ) {
  251. echo 'stacja '.$key.' poza linią numer '.$this->lastCheckedLineIds[$this->lineId].'</br>';
  252. $this->lineId = 0;
  253. continue;
  254. } elseif(($val < $distances[$targetStationId] && $this->getDistanceLineOfTwoStations($key, $targetStationId) < $distances[$targetStationId]) || count($this->checkStationLineIntersects($key)) > 1) {
  255. if(count($this->checkStationLineIntersects($key)) > 1) {
  256. echo 'zmiana linii po dotarciu do = '.$key.'</br>';
  257. //$this->lineId++;
  258. $this->checkSLI = false;
  259. } else {
  260. $this->checkSLI = true;
  261. }
  262.  
  263. echo 'wybrana = '.$key. ' val = '.$val.'</br>';
  264. $this->simplePathStations[] = $key;
  265. $this->getSimplePath($this->getStationSpiderLines($key, true), $targetStationId);
  266. return;
  267. } elseif($key == $targetStationId) {
  268. echo 'dotarłem do ostatniej ('.$key.')</br>';
  269. $this->simplePathStations[] = $key;
  270. var_dump($this->simplePathStations);
  271. return;
  272. } else {
  273. echo 'pominięta = '.$key.'</br>';
  274. }
  275. }
  276. }
  277.  
  278. public function dijkstras($from, $to, $distancesTempArr, $unvisitedStations) {
  279. //$unvisitedStations = ['a','b','c','d','e','f','g','h','i'];
  280.  
  281. if($unvisitedStations[0] != $from) {
  282. // unset "from station" from unvisited array
  283. unset($unvisitedStations[array_search($from, $unvisitedStations)]);
  284.  
  285. // prepend "from station" to the beginning of an unvisited array
  286. array_unshift($unvisitedStations, $from);
  287. }
  288.  
  289. var_dump($unvisitedStations);
  290.  
  291. $visitedStations = [];
  292. $minCost = 0;
  293. $costs = [];
  294. // $distancesTempArr = [
  295. // ['a', 'b', 5],
  296. // ['b', 'c', 4],
  297. // ['c', 'd', 2],
  298. // ['d', 'e', 1],
  299. // ['d', 'f', 7],
  300. // ['f', 'g', 6],
  301. // ['f', 'i', 12],
  302. // ['e', 'i', 11],
  303. // ['g', 'i', 5],
  304. // ['g', 'h', 2]
  305. // ];
  306.  
  307. foreach ($unvisitedStations as $init => $station) {
  308. // find and set min cost
  309. if(count($costs) > 0) {
  310. $minCost = min($costs);
  311. var_dump($minCost);
  312. $station = array_keys($costs, min($costs))[0];
  313. }
  314.  
  315. // clean costs
  316. //var_dump($costs);
  317. $costs = [];
  318.  
  319. // push current station into visited
  320. $visitedStations[] = $station;
  321. $this->dijkstrasRouteEngine[$init]['visited'] = $visitedStations;
  322.  
  323. // unset current station from unvisited
  324. unset($unvisitedStations[array_search($station, $unvisitedStations)]);
  325. $this->dijkstrasRouteEngine[$init]['unvisited'] = $unvisitedStations;
  326.  
  327. // foreach for create analyze schema
  328. $analyze = [];
  329.  
  330. foreach ($unvisitedStations as $keyStation => $indexStation) {
  331. // foreach simple lines (A-B)
  332. foreach ($distancesTempArr as $distancesIndex => $pointsDistances) {
  333. //var_dump($distancesTempArr[$distancesIndex]['length']);
  334. // if A-B OR B-A in current then...
  335. //if(($pointsDistances['sections'][0] == $station && $pointsDistances['sections'][1] == $indexStation) || ($pointsDistances['sections'][1] == $station && $pointsDistances['sections'][0] == $indexStation)) {
  336.  
  337. if(array_key_exists($station, $pointsDistances['sections']) && array_key_exists($indexStation, $pointsDistances['sections'])) {
  338. echo '<div style="background-color:#333; padding:10px; margin:40px 0; border:1px solid crimson;"><h3 class="negative">NOWA SEKCJA '.$station. ' -> '. $indexStation.'</h3>';
  339. //var_dump('znalazłem '.$station.'-'.$indexStation.' w '.$distancesIndex.', długość to '.$distancesTempArr[$distancesIndex][2].' dodana z poprzedniej to '.$minCost);
  340. // found nearest point
  341. //$cost = $distancesTempArr[$distancesIndex]['length'] + $minCost;
  342.  
  343. foreach ($pointsDistances['sections'][$station] as $unitId => $routes) {
  344. echo '<div style="background-color:#222; padding:10px; margin:40px 0; border:1px solid yellowgreen;"><h3 class="positive">UNIT ID '.$unitId.'</h3>';
  345. $timesArr = [];
  346. $possibleTimes = [];
  347. foreach ($routes as $routeId => $time) {
  348.  
  349. //foreach ($routes as $routeId => $time) {
  350. // if routeId exist in both vertex of section
  351. if(isset($pointsDistances['sections'][$indexStation][$unitId][$routeId], $pointsDistances['sections'][$station][$unitId][$routeId])) {
  352. // if int time of destination (indexStation) vertex is greater than time of start (station) vertex
  353. if($pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'] >= $pointsDistances['sections'][$station][$unitId][$routeId]['int_time']) {
  354. var_dump($unitId.' / '.$routeId.' '.$pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'].' ('.$pointsDistances['sections'][$indexStation][$unitId][$routeId]['natural_time'].') >= '.$pointsDistances['sections'][$station][$unitId][$routeId]['int_time'].' ('.$pointsDistances['sections'][$station][$unitId][$routeId]['natural_time'].')');
  355.  
  356. if(($unitId != key($pointsDistances['sections'][$station])) || ($unitId != key($pointsDistances['sections'][$indexStation]))
  357. ) {
  358. //var_dump(key($pointsDistances['sections'][$indexStation]).' != '.key($pointsDistances['sections'][$station]));
  359. //var_dump($unitId.' != '.key($pointsDistances['sections'][$station]));
  360. var_dump($unitId.' != '.key($pointsDistances['sections'][$station]).' != '.key($pointsDistances['sections'][$indexStation]));
  361. } else {
  362. //var_dump(key($pointsDistances['sections'][$indexStation]).' == '.key($pointsDistances['sections'][$station]));
  363. //var_dump($unitId.' == '.key($pointsDistances['sections'][$station]));
  364. var_dump($unitId.' == '.key($pointsDistances['sections'][$station]).' == '.key($pointsDistances['sections'][$indexStation]));
  365. }
  366. var_dump($pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'] .'>='. $previousIntTime);
  367. // push possible times of destination station to an array
  368. if($pointsDistances['sections'][$indexStation][$unitId][$routeId]['natural_time'] > $minCost) {
  369. var_dump($pointsDistances['sections'][$indexStation][$unitId][$routeId]['natural_time'] .'>'. $minCost);
  370. $possibleTimes[$indexStation][$routeId] = [
  371. 'int_time' => $pointsDistances['sections'][$station][$unitId][$routeId]['int_time'],
  372. 'readable_time' => $pointsDistances['sections'][$station][$unitId][$routeId]['natural_time']
  373. ];
  374. }
  375. }
  376.  
  377. // $secondsDiff = $pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'] - $pointsDistances['sections'][$station][$unitId][$routeId]['int_time'];
  378. // $incrementTime = $pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'];
  379.  
  380. // if(count($this->previousTimeArr) == 0) {
  381. // //var_dump('był większy '.$incrementTime .' ('.$pointsDistances['sections'][$indexStation][$unitId][$routeId]['natural_time'].') > ' . $this->previousTimeArr[0]['seconds_diff'] .' ('.$this->previousTimeArr[0]['natural_time'].") $station<->$indexStation [ ] ");
  382. // $timesArr[] = [
  383. // //'seconds_diff' => $incrementTime,
  384. // 'seconds_diff' => $pointsDistances['sections'][$station][$unitId][$routeId]['int_time'],
  385. // // $pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'],
  386. // // $pointsDistances['sections'][$station][$unitId][$routeId]['natural_time'],
  387. // 'natural_time' => $pointsDistances['sections'][$station][$unitId][$routeId]['natural_time']
  388. // ];
  389. // }
  390. // elseif($secondsDiff > 0 && $incrementTime > $this->previousTimeArr[0]['seconds_diff']) {
  391. // //var_dump('był większy '.$incrementTime .' ('.$pointsDistances['sections'][$indexStation][$unitId][$routeId]['natural_time'].') > ' . $this->previousTimeArr[0]['seconds_diff'] .' ('.$this->previousTimeArr[0]['natural_time'].") $station<->$indexStation [ ] ");
  392. // $timesArr[] = [
  393. // //'seconds_diff' => $incrementTime,
  394. // 'seconds_diff' => $pointsDistances['sections'][$station][$unitId][$routeId]['int_time'],
  395. // // $pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'],
  396. // // $pointsDistances['sections'][$station][$unitId][$routeId]['natural_time'],
  397. // 'natural_time' => $pointsDistances['sections'][$station][$unitId][$routeId]['natural_time']
  398. // ];
  399. // } else {
  400. // //echo 'else';
  401. // }
  402. $previousIntTime = $pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'];
  403. }
  404. //}
  405. // echo '<h3>PREVIOUS</h3>';
  406. // var_dump($this->previousTimeArr);
  407. // echo '<h3>TIMEARR</h3>';
  408. // var_dump($timesArr);
  409. $this->previousTimeArr = $timesArr;
  410.  
  411. //var_dump($secondsDiff);
  412. //$cost = $secondsDiff + $minCost;
  413. }
  414.  
  415. // var_dump($possibleTimes);
  416. // var_dump($indexStation);
  417. if(count($possibleTimes) > 0) {
  418. //$cost = $possibleTimes[$indexStation];
  419. $cost = reset($possibleTimes[$indexStation])['readable_time'];
  420. $analyzeUnitId = $unitId;
  421. }
  422. // if(count($timesArr) > 0) {
  423. // $cost = $timesArr[0]['seconds_diff'];
  424. // $analyzeUnitId = $unitId;
  425. // $analyzeIntTime = $pointsDistances['sections'][$station][$unitId][$routeId]['int_time'];
  426. // $analyzeTime = $timesArr[0]['natural_time'];
  427. // }
  428. echo '</div>';
  429.  
  430. $previousUnitId = $unitId;
  431.  
  432. //if($pointsDistances['sections'][$station])
  433. //$cost = $distancesTempArr[$distancesIndex]['length'] + $minCost;
  434. }
  435. echo '</div>';
  436. //echo 'jestem w ifie stacji';
  437. if($init == 0) {
  438. $analyze[$indexStation] = ['cost' => $cost, 'station' => $station, 'init' => $init, 'section_id' => $distancesIndex, 'unit_id' => $analyzeUnitId];
  439. $costs[$indexStation] = $cost;
  440. break;
  441. }
  442. } else {
  443. // infinity cost
  444. // if($init > 0 && $this->dijkstrasRouteEngine[$init - 1]['analyze'][$indexStation]['cost'] != 'infinity') {
  445. // $cost = $this->dijkstrasRouteEngine[$init - 1]['analyze'][$indexStation]['cost'];
  446. // }
  447. // else
  448. $cost = 'infinity';
  449.  
  450. $analyze[$indexStation] = ['cost' => $cost, 'station' => $station, 'init' => $init/*, 'section_id' => $distancesIndex*/];
  451. }
  452. //var_dump($station.' | '.$indexStation.' | '.$this->dijkstrasRouteEngine[$init - 1]['analyze'][$indexStation]['cost'].' | '.$cost);
  453. // skip first cycle
  454. if($init > 0) {
  455. //$cost = $distancesTempArr[$distancesIndex][2] + $minCost;
  456. $previousCost = $this->dijkstrasRouteEngine[$init - 1]['analyze'][$indexStation]['cost'];
  457. //var_dump($init.' / '.$indexStation.' = '.$previousCost .' <> '.$station.' = '. $cost);
  458. //var_dump($previousCost.' = '.$cost);
  459. if($previousCost == 'infinity' && $cost == 'infinity') {
  460.  
  461. //$cost = $distancesTempArr[$distancesIndex][2] + $minCost;
  462. $analyze[$indexStation] = ['cost' => $cost, 'station' => $station, 'init' => $init/*, 'section_id' => $distancesIndex*/];
  463. //$costs[] = $cost;
  464. //break;
  465.  
  466. } elseif($previousCost == 'infinity' && $cost != 'infinity') {
  467.  
  468. $analyze[$indexStation] = ['cost' => $cost, 'station' => $station, 'init' => $init, 'section_id' => $distancesIndex, 'unit_id' => $analyzeUnitId];
  469. $costs[$indexStation] = $cost;
  470. break;
  471.  
  472. } elseif($previousCost != 'infinity' && $cost == 'infinity') {
  473.  
  474. $analyze[$indexStation] = $this->dijkstrasRouteEngine[$init - 1]['analyze'][$indexStation];
  475. $costs[$indexStation] = $previousCost;
  476. //break;
  477.  
  478. } elseif($previousCost != 'infinity' && $cost != 'infinity') {
  479. echo 'porównałem dwie liczby';
  480. if($cost <= $previousCost) {
  481. $analyze[$indexStation] = ['cost' => $cost, 'station' => $station, 'init' => $init, 'section_id' => $distancesIndex, 'unit_id' => $analyzeUnitId];
  482. $costs[$indexStation] = $cost;
  483. break;
  484. } else {
  485. $analyze[$indexStation] = $this->dijkstrasRouteEngine[$init - 1]['analyze'][$indexStation];
  486. $costs[$indexStation] = $previousCost;
  487. //break;
  488. }
  489. }
  490. }
  491. }
  492. }
  493.  
  494. // -- OLD COSTS PLACE --
  495.  
  496. // set current
  497. $this->dijkstrasRouteEngine[$init]['current'] = $station;
  498.  
  499. // set unvisited to analyze
  500. $this->dijkstrasRouteEngine[$init]['analyze'] = $analyze;
  501. }
  502. echo '<div style="border:1px solid #42C0FB; padding: 10px;"><h3 style="color:#42C0FB;">POSSIBLE TIMES [INDEX STATION][ROUTE ID]</h3>';
  503. var_dump($possibleTimes);
  504. echo '</div>';
  505. //var_dump($this->dijkstrasRouteEngine);
  506. //return $this->dijkstrasRouteEngine;
  507. }
  508.  
  509. public function getShortestRoutePath($from, $to, $toReference = null) {
  510. $toArray = [];
  511.  
  512. // in first method init reference "to station" is null - use "to", clear SRP
  513. if($toReference == null) {
  514. $distancesTempArr = $this->getSections($from, $to);
  515.  
  516. if($distancesTempArr === false)
  517. return false;
  518.  
  519. $unvisitedStations = $this->stationsIds;
  520. $this->dijkstras($from, $to, $distancesTempArr, $unvisitedStations);
  521. $this->SRP = [];
  522. $this->totalCost = null;
  523. $toReference = $to;
  524. }
  525.  
  526. // walk on dijkstras route engine array to determine backwards SRP step by step
  527. foreach ($this->dijkstrasRouteEngine as $key => $init) {
  528. foreach ($init['analyze'] as $station => $analyzeField) {
  529. if($station == $toReference && $analyzeField['cost'] != 'infinity')
  530. $toArray[] = $analyzeField;
  531.  
  532. if($station == $to) {
  533. $this->totalCost = $analyzeField['cost'];
  534. //var_dump($analyzeField);
  535. }
  536. }
  537. }
  538.  
  539. if(count($toArray) > 0) {
  540. var_dump($toArray);
  541. $previousStation = end($toArray)['station'];
  542.  
  543. // $this->SRP[] = [
  544. // 'path_point' => end($toArray)['station'],
  545. // 'section_id' => end($toArray)['section_id'],
  546. // 'unit_id' => end($toArray)['unit_id'],
  547. // 'natural_time' => end($toArray)['natural_time']
  548. // ];
  549. $this->SRP[] = end($toArray);
  550.  
  551. // references on station to set route backwards
  552. $this->getShortestRoutePath($from, $to, $previousStation);
  553. return $this->SRP;
  554. }
  555.  
  556. // prepend "to station" to the beginning of an SRP array
  557. array_unshift($this->SRP, ['station' => $to, 'section_id' => null]);
  558.  
  559. // reverse SRP
  560. $this->SRP = array_reverse($this->SRP);
  561.  
  562. // separate station from the sections
  563. foreach ($this->SRP as $key) {
  564. $SRPStations[] = $key['station'];
  565. $SRPSections[] = $key['section_id'];
  566. }
  567.  
  568. // set stations and sections
  569. $this->SRPStations = $SRPStations;
  570. $this->SRPSections = $SRPSections;
  571.  
  572. // final SRP return
  573. return $this->SRP;
  574. }
  575.  
  576. public function getStationsIds() {
  577. //$sql = 'SELECT `id` FROM `stations` ORDER BY `id`';
  578. $sql = 'SELECT `s`.`id`
  579. FROM `stations` `s`, `relations` `r`
  580. WHERE `s`.`id` = `r`.`station_id`
  581. GROUP BY `s`.`id`
  582. ORDER BY `s`.`id`';
  583.  
  584. $stmt = $this->pdo->query($sql);
  585. $stmt->execute();
  586.  
  587. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  588. $stationsIds[] = $row['id'];
  589. }
  590.  
  591. return $stationsIds;
  592. }
  593.  
  594. public function getSections($from, $to) {
  595. $sql = 'SELECT `s`.`id` AS `station_id`, `s`.`name`, `c`.`id` AS `line_id`, ST_Length(`c`.`line`) AS `length`, TIME_TO_SEC(`r`.`time`) AS `relation_time`, `r`.`time` AS `relation_time_natural`, `r`.`route_id`, `r`.`unit_id`, `r`.`id` AS `relation_id`
  596. FROM `connections` `c`
  597. JOIN `stations` `s` ON (ST_Intersects(`s`.`point`, `c`.`line`) = 1)
  598. JOIN `relations` `r` ON `s`.`id` = `r`.`station_id`
  599. WHERE `r`.`time` >= \'15:50:00\'
  600. ORDER BY `r`.`time`';
  601.  
  602. $stmt = $this->pdo->query($sql);
  603. $stmt->execute();
  604.  
  605. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  606. $sections[$row['line_id']]['section_id'] = $row['line_id'];
  607. $sections[$row['line_id']]['length'] = $row['length'];
  608. $sections[$row['line_id']]['sections'][$row['station_id']][$row['unit_id']][$row['route_id']] = ['int_time' => $row['relation_time'], 'natural_time' => $row['relation_time_natural']];
  609. //$sections[$row['line_id']]['sections'][$row['station_id']][] = ];
  610.  
  611. $detectedStationsIds[] = $row['station_id'];
  612. }
  613.  
  614. // remove duplicates stations ids from array
  615. $mergedDetectedStationsIds = array_unique($detectedStationsIds);
  616.  
  617. var_dump($from);
  618. var_dump($to);
  619. var_dump($mergedDetectedStationsIds);
  620.  
  621. // if there is no from or to station in possible stations return false
  622. if(!in_array($from, $mergedDetectedStationsIds) || !in_array($to, $mergedDetectedStationsIds))
  623. return false;
  624.  
  625. // set stations ids
  626. $this->stationsIds = $mergedDetectedStationsIds;
  627.  
  628. // unset index where section has only 1 vertex
  629. foreach ($sections as $key => $val) {
  630. if(count($val['sections']) < 2)
  631. unset($sections[$key]);
  632.  
  633. //var_dump(count($val['sections']));
  634. }
  635.  
  636. return $sections;
  637. }
  638. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement