biale_plamy-find_ways.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. $_SERVER['SERVER_NAME'] = 'biuro.biall-net.pl';
  3. require("../../se-lib/bootstrap.php");
  4. Lib::loadClass("Vendor_Geophp");
  5. function init_sh_check() {
  6. //check if run from bash
  7. if(isset($_SERVER["argv"])) {
  8. if(@$_SERVER["argv"][1]=='--help') {
  9. echo "
  10. Options are:
  11. php ".$_SERVER["argv"][0]." --check - control if we have needed tables/data
  12. .EOF
  13. ";
  14. } else if(@$_SERVER["argv"][1]=='--check') {
  15. check();
  16. } else echo " bad option, try:
  17. php ".$_SERVER["argv"][0]." --help
  18. .EOF
  19. ";
  20. } else {
  21. echo "<br> this script could be run from bash e.g. php biale_plamy.php --help <br>" ;
  22. check();
  23. }
  24. }
  25. function check() {
  26. echo "<br>\n i run check()";
  27. //todo to set arguments to rebuild tables etc:
  28. $check_tbls=array('Rozdzielcza_test_bzyk_drogi','Rozdzielcza_test_bzyk_joins','Rozdzielcza_test_bzyk_przylacza_HIST',
  29. 'Rozdzielcza_test_bzyk_przylacza','Rozdzielcza_test_bzyk_punkty_adresowe','Rozdzielcza_test_bzyk_ways');
  30. //$tables = DB::getPDO()->list_tables();
  31. foreach($check_tbls as $tbl) {
  32. $query = "select ID from ".$tbl." limit 1 ";
  33. echo "<br>\nresult chk for tbl:".$tbl." ;";
  34. try{
  35. $result = DB::getPDO()->fetchall($query);
  36. } catch(Exception $exception) {
  37. // return $exception;
  38. echo "<br>\n jest blad z tbl:".$tbl." i get sample table structure with data from ../sql/".$tbl.".sql - it should be manually replaced by correct/newer!";
  39. $sql = file_get_contents('../sql/'.$tbl.'.sql');
  40. try{
  41. $result = DB::getPDO()->fetchall($sql);
  42. } catch(Exception $exception2) {
  43. echo "<br>\n jest blad dump dla tbl:".$tbl;
  44. }
  45. }
  46. // print_r($exception);
  47. print_r($result);
  48. }
  49. }
  50. init_sh_check();
  51. $ways2joins = unserialize(file_get_contents('ways2joins.txt'));
  52. $joins2ways = unserialize(file_get_contents('joins2ways.txt'));
  53. $points2joins = unserialize(file_get_contents('points2joins.txt'));
  54. $joins_asText = unserialize(file_get_contents('joins_asText.txt'));
  55. $ways_asText = unserialize(file_get_contents('ways_asText.txt'));
  56. $points_asText = unserialize(file_get_contents('points_asText.txt'));
  57. function joins2lineString($joins_keys) {
  58. global $joins_asText;
  59. if ($joins_keys) {
  60. foreach ($joins_keys as $join_key) {
  61. $points[] = Vendor_Geophp::load($joins_asText[$join_key], 'wkt');
  62. }
  63. return new LineString($points);
  64. } else return null;
  65. }
  66. function findWay($joinA, $joinB, $path = array()) {
  67. global $ways2joins, $joins2ways, $points2joins, $ways_asText, $points_asText;
  68. if ($joinA == $joinB) {
  69. $joinsPath = array($joinB);
  70. foreach (array_reverse($path) as $way_key) {
  71. array_unshift($joinsPath, $ways2joins[$way_key][1 - array_search($joinsPath[0], $ways2joins[$way_key])]);
  72. }
  73. return $joinsPath;
  74. } else {
  75. $subPaths = array();
  76. foreach ($joins2ways[$joinA] as $way_key) {
  77. if (!in_array($way_key, $path)) {
  78. $new_join = $ways2joins[$way_key][1 - array_search($joinA, $ways2joins[$way_key])];
  79. if ($subPath = findWay($new_join, $joinB, array_merge($path,array($way_key)))) $subPaths[] = $subPath;
  80. }
  81. }
  82. if ($subPaths) {
  83. if (count($subPaths) == 1) return $subPaths[0];
  84. else {
  85. $shortestKey = 0;
  86. foreach ($subPaths as $key => $subPath) {
  87. $distance_cur = 0;
  88. foreach ($subPath as $way_key) {
  89. $distance_cur += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->length();
  90. }
  91. if (!$shortestKey || ($shortestKey && $distance_cur < $distance)) {
  92. $shortestKey = $key;
  93. $distance = $distance_cur;
  94. }
  95. }
  96. return $subPaths[$key];
  97. }
  98. }
  99. else return null;
  100. }
  101. }
  102. #echo joins2lineString($res = findWay(100,110))->asText();
  103. #echo joins2lineString($res = findWay(25690,20134))->asText();
  104. $timeStart = microtime();
  105. $path = joins2lineString(findWay(25690,23039));
  106. $timeEnd = microtime();
  107. echo $path->asText()."\n";
  108. echo "Dlugosc: ".$path->greatCircleLength()."\n";
  109. list($usecStart, $secStart) = explode(" ", $timeStart);
  110. list($usecEnd, $secEnd) = explode(" ", $timeEnd);
  111. $diff = $secEnd - $secStart + $usecEnd - $usecStart;
  112. echo "\n\nCzas liczenia: ".$diff." s\n";
  113. //$distance = 0;
  114. //foreach ($res as $way_key) {
  115. // $distance += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->greatCircleLength();
  116. //}
  117. //print_r($res);
  118. //echo "Distance: ".$distance."m\n";