Advertisement
Guest User

Untitled

a guest
Nov 14th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.24 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. $station = array_keys($costs, min($costs))[0];
  312. }
  313.  
  314. // clean costs
  315. //var_dump($costs);
  316. $costs = [];
  317.  
  318. // push current station into visited
  319. $visitedStations[] = $station;
  320. $this->dijkstrasRouteEngine[$init]['visited'] = $visitedStations;
  321.  
  322. // unset current station from unvisited
  323. unset($unvisitedStations[array_search($station, $unvisitedStations)]);
  324. $this->dijkstrasRouteEngine[$init]['unvisited'] = $unvisitedStations;
  325.  
  326. // foreach for create analyze schema
  327. $analyze = [];
  328.  
  329. foreach ($unvisitedStations as $keyStation => $indexStation) {
  330. // foreach simple lines (A-B)
  331. foreach ($distancesTempArr as $distancesIndex => $pointsDistances) {
  332. //var_dump($distancesTempArr[$distancesIndex]['length']);
  333. // if A-B OR B-A in current then...
  334. //if(($pointsDistances['sections'][0] == $station && $pointsDistances['sections'][1] == $indexStation) || ($pointsDistances['sections'][1] == $station && $pointsDistances['sections'][0] == $indexStation)) {
  335.  
  336. if(array_key_exists($station, $pointsDistances['sections']) && array_key_exists($indexStation, $pointsDistances['sections'])) {
  337. echo '<div style="background-color:#333; padding:10px; margin:40px 0; border:1px solid crimson;"><h3 class="negative">NOWA SEKCJA '.$station. ' -> '. $indexStation.'</h3>';
  338. //var_dump('znalazłem '.$station.'-'.$indexStation.' w '.$distancesIndex.', długość to '.$distancesTempArr[$distancesIndex][2].' dodana z poprzedniej to '.$minCost);
  339. // found nearest point
  340. //$cost = $distancesTempArr[$distancesIndex]['length'] + $minCost;
  341.  
  342. foreach ($pointsDistances['sections'][$station] as $unitId => $routes) {
  343. echo '<div style="background-color:#222; padding:10px; margin:40px 0; border:1px solid yellowgreen;"><h3 class="positive">UNIT ID '.$unitId.'</h3>';
  344. $timesArr = [];
  345. $possibleTimes = [];
  346. foreach ($routes as $routeId => $time) {
  347.  
  348. //foreach ($routes as $routeId => $time) {
  349. // if routeId exist in both vertex of section
  350. if(isset($pointsDistances['sections'][$indexStation][$unitId][$routeId], $pointsDistances['sections'][$station][$unitId][$routeId])) {
  351. // if int time of destination (indexStation) vertex is greater than time of start (station) vertex
  352. if($pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'] >= $pointsDistances['sections'][$station][$unitId][$routeId]['int_time']) {
  353. 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'].')');
  354.  
  355. if(($unitId != key($pointsDistances['sections'][$station])) || ($unitId != key($pointsDistances['sections'][$indexStation]))
  356. ) {
  357. //var_dump(key($pointsDistances['sections'][$indexStation]).' != '.key($pointsDistances['sections'][$station]));
  358. //var_dump($unitId.' != '.key($pointsDistances['sections'][$station]));
  359. var_dump($unitId.' != '.key($pointsDistances['sections'][$station]).' != '.key($pointsDistances['sections'][$indexStation]));
  360. } else {
  361. //var_dump(key($pointsDistances['sections'][$indexStation]).' == '.key($pointsDistances['sections'][$station]));
  362. //var_dump($unitId.' == '.key($pointsDistances['sections'][$station]));
  363. var_dump($unitId.' == '.key($pointsDistances['sections'][$station]).' == '.key($pointsDistances['sections'][$indexStation]));
  364. }
  365. var_dump($pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'] .'>='. $previousIntTime);
  366. // push possible times of destination station to an array
  367. $possibleTimes[$indexStation][$routeId] = [
  368. 'int_time' => $pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'],
  369. 'readable_time' => $pointsDistances['sections'][$indexStation][$unitId][$routeId]['natural_time']
  370. ];
  371. }
  372.  
  373. // $secondsDiff = $pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'] - $pointsDistances['sections'][$station][$unitId][$routeId]['int_time'];
  374. // $incrementTime = $pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'];
  375.  
  376. // if(count($this->previousTimeArr) == 0) {
  377. // //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 [ ] ");
  378. // $timesArr[] = [
  379. // //'seconds_diff' => $incrementTime,
  380. // 'seconds_diff' => $pointsDistances['sections'][$station][$unitId][$routeId]['int_time'],
  381. // // $pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'],
  382. // // $pointsDistances['sections'][$station][$unitId][$routeId]['natural_time'],
  383. // 'natural_time' => $pointsDistances['sections'][$station][$unitId][$routeId]['natural_time']
  384. // ];
  385. // }
  386. // elseif($secondsDiff > 0 && $incrementTime > $this->previousTimeArr[0]['seconds_diff']) {
  387. // //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 [ ] ");
  388. // $timesArr[] = [
  389. // //'seconds_diff' => $incrementTime,
  390. // 'seconds_diff' => $pointsDistances['sections'][$station][$unitId][$routeId]['int_time'],
  391. // // $pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'],
  392. // // $pointsDistances['sections'][$station][$unitId][$routeId]['natural_time'],
  393. // 'natural_time' => $pointsDistances['sections'][$station][$unitId][$routeId]['natural_time']
  394. // ];
  395. // } else {
  396. // //echo 'else';
  397. // }
  398. $previousIntTime = $pointsDistances['sections'][$indexStation][$unitId][$routeId]['int_time'];
  399. }
  400. //}
  401. // echo '<h3>PREVIOUS</h3>';
  402. // var_dump($this->previousTimeArr);
  403. // echo '<h3>TIMEARR</h3>';
  404. // var_dump($timesArr);
  405. $this->previousTimeArr = $timesArr;
  406.  
  407. //var_dump($secondsDiff);
  408. //$cost = $secondsDiff + $minCost;
  409. }
  410.  
  411. // var_dump($possibleTimes);
  412. // var_dump($indexStation);
  413. if(count($possibleTimes) > 0) {
  414. //$cost = $possibleTimes[$indexStation];
  415. $cost = reset($possibleTimes[$indexStation])['readable_time'];
  416. $analyzeUnitId = $unitId;
  417. }
  418. // if(count($timesArr) > 0) {
  419. // $cost = $timesArr[0]['seconds_diff'];
  420. // $analyzeUnitId = $unitId;
  421. // $analyzeIntTime = $pointsDistances['sections'][$station][$unitId][$routeId]['int_time'];
  422. // $analyzeTime = $timesArr[0]['natural_time'];
  423. // }
  424. echo '</div>';
  425.  
  426. $previousUnitId = $unitId;
  427.  
  428. //if($pointsDistances['sections'][$station])
  429. //$cost = $distancesTempArr[$distancesIndex]['length'] + $minCost;
  430. }
  431. echo '</div>';
  432. //echo 'jestem w ifie stacji';
  433. if($init == 0) {
  434. $analyze[$indexStation] = ['cost' => $cost, 'station' => $station, 'init' => $init, 'section_id' => $distancesIndex, 'unit_id' => $analyzeUnitId];
  435. $costs[$indexStation] = $cost;
  436. break;
  437. }
  438. } else {
  439. // infinity cost
  440. // if($init > 0 && $this->dijkstrasRouteEngine[$init - 1]['analyze'][$indexStation]['cost'] != 'infinity') {
  441. // $cost = $this->dijkstrasRouteEngine[$init - 1]['analyze'][$indexStation]['cost'];
  442. // }
  443. // else
  444. $cost = 'infinity';
  445.  
  446. $analyze[$indexStation] = ['cost' => $cost, 'station' => $station, 'init' => $init/*, 'section_id' => $distancesIndex*/];
  447. }
  448. //var_dump($station.' | '.$indexStation.' | '.$this->dijkstrasRouteEngine[$init - 1]['analyze'][$indexStation]['cost'].' | '.$cost);
  449. // skip first cycle
  450. if($init > 0) {
  451. //$cost = $distancesTempArr[$distancesIndex][2] + $minCost;
  452. $previousCost = $this->dijkstrasRouteEngine[$init - 1]['analyze'][$indexStation]['cost'];
  453. //var_dump($init.' / '.$indexStation.' = '.$previousCost .' <> '.$station.' = '. $cost);
  454. //var_dump($previousCost.' = '.$cost);
  455. if($previousCost == 'infinity' && $cost == 'infinity') {
  456.  
  457. //$cost = $distancesTempArr[$distancesIndex][2] + $minCost;
  458. $analyze[$indexStation] = ['cost' => $cost, 'station' => $station, 'init' => $init/*, 'section_id' => $distancesIndex*/];
  459. //$costs[] = $cost;
  460. //break;
  461.  
  462. } elseif($previousCost == 'infinity' && $cost != 'infinity') {
  463.  
  464. $analyze[$indexStation] = ['cost' => $cost, 'station' => $station, 'init' => $init, 'section_id' => $distancesIndex, 'unit_id' => $analyzeUnitId];
  465. $costs[$indexStation] = $cost;
  466. break;
  467.  
  468. } elseif($previousCost != 'infinity' && $cost == 'infinity') {
  469.  
  470. $analyze[$indexStation] = $this->dijkstrasRouteEngine[$init - 1]['analyze'][$indexStation];
  471. $costs[$indexStation] = $previousCost;
  472. //break;
  473.  
  474. } elseif($previousCost != 'infinity' && $cost != 'infinity') {
  475. echo 'porównałem dwie liczby';
  476. if($cost <= $previousCost) {
  477. $analyze[$indexStation] = ['cost' => $cost, 'station' => $station, 'init' => $init, 'section_id' => $distancesIndex, 'unit_id' => $analyzeUnitId];
  478. $costs[$indexStation] = $cost;
  479. break;
  480. } else {
  481. $analyze[$indexStation] = $this->dijkstrasRouteEngine[$init - 1]['analyze'][$indexStation];
  482. $costs[$indexStation] = $previousCost;
  483. //break;
  484. }
  485. }
  486. }
  487. }
  488. }
  489.  
  490. // -- OLD COSTS PLACE --
  491.  
  492. // set current
  493. $this->dijkstrasRouteEngine[$init]['current'] = $station;
  494.  
  495. // set unvisited to analyze
  496. $this->dijkstrasRouteEngine[$init]['analyze'] = $analyze;
  497. }
  498. echo '<div style="border:1px solid #42C0FB; padding: 10px;"><h3 style="color:#42C0FB;">POSSIBLE TIMES [INDEX STATION][ROUTE ID]</h3>';
  499. var_dump($possibleTimes);
  500. echo '</div>';
  501. //var_dump($this->dijkstrasRouteEngine);
  502. //return $this->dijkstrasRouteEngine;
  503. }
  504.  
  505. public function getShortestRoutePath($from, $to, $toReference = null) {
  506. $toArray = [];
  507.  
  508. // in first method init reference "to station" is null - use "to", clear SRP
  509. if($toReference == null) {
  510. $distancesTempArr = $this->getSections($from, $to);
  511.  
  512. if($distancesTempArr === false)
  513. return false;
  514.  
  515. $unvisitedStations = $this->stationsIds;
  516. $this->dijkstras($from, $to, $distancesTempArr, $unvisitedStations);
  517. $this->SRP = [];
  518. $this->totalCost = null;
  519. $toReference = $to;
  520. }
  521.  
  522. // walk on dijkstras route engine array to determine backwards SRP step by step
  523. foreach ($this->dijkstrasRouteEngine as $key => $init) {
  524. foreach ($init['analyze'] as $station => $analyzeField) {
  525. if($station == $toReference && $analyzeField['cost'] != 'infinity')
  526. $toArray[] = $analyzeField;
  527.  
  528. if($station == $to) {
  529. $this->totalCost = $analyzeField['cost'];
  530. //var_dump($analyzeField);
  531. }
  532. }
  533. }
  534.  
  535. if(count($toArray) > 0) {
  536. var_dump($toArray);
  537. $previousStation = end($toArray)['station'];
  538.  
  539. // $this->SRP[] = [
  540. // 'path_point' => end($toArray)['station'],
  541. // 'section_id' => end($toArray)['section_id'],
  542. // 'unit_id' => end($toArray)['unit_id'],
  543. // 'natural_time' => end($toArray)['natural_time']
  544. // ];
  545. $this->SRP[] = end($toArray);
  546.  
  547. // references on station to set route backwards
  548. $this->getShortestRoutePath($from, $to, $previousStation);
  549. return $this->SRP;
  550. }
  551.  
  552. // prepend "to station" to the beginning of an SRP array
  553. array_unshift($this->SRP, ['station' => $to, 'section_id' => null]);
  554.  
  555. // reverse SRP
  556. $this->SRP = array_reverse($this->SRP);
  557.  
  558. // separate station from the sections
  559. foreach ($this->SRP as $key) {
  560. $SRPStations[] = $key['station'];
  561. $SRPSections[] = $key['section_id'];
  562. }
  563.  
  564. // set stations and sections
  565. $this->SRPStations = $SRPStations;
  566. $this->SRPSections = $SRPSections;
  567.  
  568. // final SRP return
  569. return $this->SRP;
  570. }
  571.  
  572. public function getStationsIds() {
  573. //$sql = 'SELECT `id` FROM `stations` ORDER BY `id`';
  574. $sql = 'SELECT `s`.`id`
  575. FROM `stations` `s`, `relations` `r`
  576. WHERE `s`.`id` = `r`.`station_id`
  577. GROUP BY `s`.`id`
  578. ORDER BY `s`.`id`';
  579.  
  580. $stmt = $this->pdo->query($sql);
  581. $stmt->execute();
  582.  
  583. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  584. $stationsIds[] = $row['id'];
  585. }
  586.  
  587. return $stationsIds;
  588. }
  589.  
  590. public function getSections($from, $to) {
  591. $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`
  592. FROM `connections` `c`
  593. JOIN `stations` `s` ON (ST_Intersects(`s`.`point`, `c`.`line`) = 1)
  594. JOIN `relations` `r` ON `s`.`id` = `r`.`station_id`
  595. WHERE `r`.`time` >= \'15:50:00\'
  596. ORDER BY `r`.`time`';
  597.  
  598. $stmt = $this->pdo->query($sql);
  599. $stmt->execute();
  600.  
  601. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  602. $sections[$row['line_id']]['section_id'] = $row['line_id'];
  603. $sections[$row['line_id']]['length'] = $row['length'];
  604. $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']];
  605. //$sections[$row['line_id']]['sections'][$row['station_id']][] = ];
  606.  
  607. $detectedStationsIds[] = $row['station_id'];
  608. }
  609.  
  610. // remove duplicates stations ids from array
  611. $mergedDetectedStationsIds = array_unique($detectedStationsIds);
  612.  
  613. var_dump($from);
  614. var_dump($to);
  615. var_dump($mergedDetectedStationsIds);
  616.  
  617. // if there is no from or to station in possible stations return false
  618. if(!in_array($from, $mergedDetectedStationsIds) || !in_array($to, $mergedDetectedStationsIds))
  619. return false;
  620.  
  621. // set stations ids
  622. $this->stationsIds = $mergedDetectedStationsIds;
  623.  
  624. // unset index where section has only 1 vertex
  625. foreach ($sections as $key => $val) {
  626. if(count($val['sections']) < 2)
  627. unset($sections[$key]);
  628.  
  629. //var_dump(count($val['sections']));
  630. }
  631.  
  632. return $sections;
  633. }
  634. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement