biale_plamy.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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',
  29. 'Rozdzielcza_test_bzyk_przylacza','Rozdzielcza_test_bzyk_punkty_adresowe','Rozdzielcza_test_bzyk_ways',
  30. 'Rozdzielcza_test_bzyk_joins_HIST','Rozdzielcza_test_bzyk_przylacza_HIST',
  31. 'Rozdzielcza_test_bzyk_przylacza_HIST','Rozdzielcza_test_bzyk_punkty_adresowe_HIST','Rozdzielcza_test_bzyk_ways_HIST');
  32. //$tables = DB::getPDO()->list_tables();
  33. foreach($check_tbls as $tbl) {
  34. $query = "select ID from ".$tbl." limit 1 ";
  35. echo "<br>\nresult chk for tbl:".$tbl." ;";
  36. try{
  37. $result = DB::getPDO()->fetchall($query);
  38. } catch(Exception $exception) {
  39. // return $exception;
  40. echo "<br>\n Ajest blad z tbl:".$tbl." i get sample table structure with data from ../sql/".$tbl.".sql - it should be manually replaced by correct/newer!";
  41. $sql = file_get_contents('../sql/'.$tbl.'.sql');
  42. echo "<br> w get som content ".strlen($sql) ;
  43. unset($result);
  44. //return $exception;
  45. try{
  46. $result2 = DB::getPDO()->fetchall('select 1+1;');
  47. //mam blad dziwny na mac pro : Call to a member function fetchall() on null??? dpiero za drugim razem?
  48. } catch(Exception $exception2) {
  49. echo "<br>\n jest blad dump dla tbl:".$tbl;
  50. }
  51. }
  52. // print_r($exception);
  53. echo "<br> 64";
  54. print_r($result2);
  55. }
  56. }
  57. init_sh_check();
  58. $query = "select astext(the_geom) as multiway from Rozdzielcza_test_bzyk_drogi where numpoints(the_geom)>1";
  59. $result = DB::getPDO()->fetchall($query);
  60. $joins_asText = array();
  61. foreach ($result as $row) {
  62. $multiway = Vendor_Geophp::load($row['multiway'], 'wkt');
  63. //echo $multiway->length();
  64. for ($i = 1; $i < $n = $multiway->numPoints(); $i++) {
  65. $way = new LineString(array($multiway->pointN($i), $multiway->pointN($i+1)));
  66. $ways_asText[] = $way->asText();
  67. if (!in_array($multiway->pointN($i)->asText(), $joins_asText)) $joins_asText[] = $multiway->pointN($i)->asText();
  68. }
  69. if (!in_array($multiway->pointN($n)->asText(), $joins_asText)) $joins_asText[] = $multiway->pointN($n)->asText();
  70. }
  71. $query = "select ID, astext(the_geom) as point, astext(the_geom_nearest_way) as way, astext(the_geom_xpoint) as xpoint from Rozdzielcza_test_bzyk_punkty_adresowe limit 100";
  72. $result = DB::getPDO()->fetchall($query);
  73. $xpoints = array();
  74. foreach ($result as $row) {
  75. $points[$row['ID']] = Vendor_Geophp::load($row['point'], 'wkt');
  76. $xpoint = Vendor_Geophp::load($row['xpoint'], 'wkt');
  77. if (!($key = array_search($xpoint->asText(), $xpoints))) $key = array_push($xpoints, $xpoint->asText())-1;
  78. $xpoints2points[$key][] = $row['ID'];
  79. $points2xpoints[$row['ID']] = $key;
  80. $way = Vendor_Geophp::load($row['way'], 'wkt');
  81. if (!(in_array($xpoint->asText(), $joins_asText))) {
  82. if (@!in_array($key, $ways2cut[array_search($way->asText(), $ways_asText)])) {
  83. $ways2cut[array_search($way->asText(), $ways_asText)][] = $key;
  84. } else echo ".";
  85. };
  86. }
  87. function pointsDistance($a, $b) {
  88. $line = new LineString(array($a, $b));
  89. return $line->length();
  90. }
  91. foreach ($ways2cut as $way_key => $xpoint_keys) {
  92. $distances = array();
  93. $way = Vendor_Geophp::load($ways_asText[$way_key], 'wkt');
  94. foreach ($xpoint_keys as $xpoint_key) {
  95. $xpoint = Vendor_Geophp::load($xpoints[$xpoint_key], 'wkt');
  96. $key = array_push($joins_asText, $xpoint->asText())-1;
  97. foreach ($xpoints2points[$xpoint_key] as $point_key) {
  98. $points2joins[$point_key] = $key;
  99. }
  100. $distances[$xpoint_key] = pointsDistance($way->startPoint(), $xpoint);
  101. }
  102. asort($distances);
  103. $sub_xpoints = array();
  104. foreach (array_keys($distances) as $xpoint_key) {
  105. $sub_xpoints[] = Vendor_Geophp::load($xpoints[$xpoint_key]);
  106. }
  107. $startPoint = $way->startPoint();
  108. $endPoint = $way->endPoint();
  109. $way = new LineString(array($startPoint, $sub_xpoints[0]));
  110. $ways_asText[$way_key] = $way->asText();
  111. for ($i = 0; $i < $n = count($sub_xpoints) - 1; $i++) {
  112. $way = new LineString(array($sub_xpoints[$i], $sub_xpoints[$i+1]));
  113. $ways_asText[] = $way->asText();
  114. }
  115. $way = new LineString(array($sub_xpoints[$n], $endPoint));
  116. $ways_asText[] = $way->asText();
  117. }
  118. echo "\n";
  119. foreach (array_keys($ways_asText) as $way_key) {
  120. $way = Vendor_Geophp::load($ways_asText[$way_key], 'wkt');
  121. $ways_lenghts[$way_key] = $way->length();
  122. for ($i = 1; $i <= 2; $i++) {
  123. $join = $way->pointN($i)->asText();
  124. $join_key = array_search($join, $joins_asText);
  125. //if (!$join_key) echo ".";
  126. $ways2joins[$way_key][] = $join_key;
  127. $joins2ways[$join_key][] = $way_key;
  128. }
  129. }
  130. function findWay($join_key, $point_key, $path = array()) {
  131. global $ways2joins, $joins2ways, $points2joins, $ways_asText, $joins2ways, $points, $points2xpoints, $xpoints2joins, $ways_lengths, $res;
  132. //echo implode('->',$path)."\n\n";
  133. if ($join_key == $points2joins[$point_key]) $res[] = $path;
  134. else {
  135. foreach ($joins2ways[$join_key] as $way_key) {
  136. if (!in_array($way_key, $path)) {
  137. // $joins = $ways2joins[$way_key];
  138. // $back_join_subkey = array_search($join_key, $joins);
  139. // $new_join_subkey = 1 - $back_join_subkey;
  140. // $new_join = $joins[$new_join_subkey];
  141. $new_join = $ways2joins[$way_key][1 - array_search($join_key, $ways2joins[$way_key])];
  142. findWay($new_join, $point_key, array_merge($path,array($way_key)));
  143. }
  144. }
  145. }
  146. }
  147. //print_r($ways2joins);
  148. for ($i = 0; $i < count($points); $i++) {
  149. // $res = array();
  150. if (isset($res)) unset($res);
  151. findWay(2000, $i);
  152. print_r($res);
  153. }
  154. //print_r($ways);
  155. echo "ways count: ".count($ways_asText)."\n";
  156. /*
  157. $db = DB::getDB();
  158. $db->query("truncate table Rozdzielcza_test_bzyk_ways");
  159. $db->query("truncate table Rozdzielcza_test_bzyk_joins");
  160. foreach ($ways_asText as $way) {
  161. $query = "insert into Rozdzielcza_test_bzyk_ways (the_geom) values (linefromtext('".$way."'))";
  162. $db->query($query);
  163. //echo $query."\n";
  164. }
  165. foreach ($joins_asText as $join) {
  166. $query = "insert into Rozdzielcza_test_bzyk_joins (the_geom) values (pointfromtext('".$join."'))";
  167. $db->query($query);
  168. }
  169. */