biale_plamy-find_ways.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. function pointsDistance($a, $b) {
  52. $line = new LineString(array($a, $b));
  53. return $line->length();
  54. }
  55. $ways2joins = unserialize(file_get_contents('ways2joins.txt'));
  56. $joins2ways = unserialize(file_get_contents('joins2ways.txt'));
  57. $points2joins = unserialize(file_get_contents('points2joins.txt'));
  58. $joins_asText = unserialize(file_get_contents('joins_asText.txt'));
  59. $ways_asText = unserialize(file_get_contents('ways_asText.txt'));
  60. $points_asText = unserialize(file_get_contents('points_asText.txt'));
  61. function findWay($joinA, $joinB, $path = array()) {
  62. global $ways2joins, $joins2ways, $points2joins, $ways_asText, $points_asText, $res;
  63. //echo implode('->',$path)."\n\n";
  64. if ($joinA == $joinB) return $path;
  65. else {
  66. $subPaths = array();
  67. foreach ($joins2ways[$joinA] as $way_key) {
  68. if (!in_array($way_key, $path)) {
  69. // $joins = $ways2joins[$way_key];
  70. // $back_join_subkey = array_search($join_key, $joins);
  71. // $new_join_subkey = 1 - $back_join_subkey;
  72. // $new_join = $joins[$new_join_subkey];
  73. $new_join = $ways2joins[$way_key][1 - array_search($joinA, $ways2joins[$way_key])];
  74. if ($subPath = findWay($new_join, $joinB, array_merge($path,array($way_key)))) $subPaths[] = $subPath;
  75. }
  76. }
  77. if ($subPaths) {
  78. if (count($subPaths) == 1) return $subPaths[0];
  79. else {
  80. $shortestKey = 0;
  81. foreach ($subPaths as $key => $subPath) {
  82. $distance_cur = 0;
  83. foreach ($subPath as $way_key) {
  84. $distance_cur += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->length();
  85. }
  86. if (!$shortestKey || ($shortestKey && $distance_cur < $distance)) {
  87. $shortestKey = $key;
  88. $distance = $distance_cur;
  89. }
  90. }
  91. return $subPaths[$key];
  92. }
  93. }
  94. else return null;
  95. }
  96. }
  97. $res = findWay(100,110);
  98. $distance = 0;
  99. foreach ($res as $way_key) {
  100. $distance += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->greatCircleLength();
  101. }
  102. print_r($res);
  103. echo "Distance: ".$distance."m\n";