biale_plamy.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. $query = "select astext(the_geom) as multiway from Rozdzielcza_test_bzyk_drogi where numpoints(the_geom)>1";
  52. $result = DB::getPDO()->fetchall($query);
  53. $joins = array();
  54. foreach ($result as $row) {
  55. $multiway = Vendor_Geophp::load($row['multiway'], 'wkt');
  56. //echo $multiway->length();
  57. for ($i = 1; $i < $n = $multiway->numPoints(); $i++) {
  58. $way = new LineString(array($multiway->pointN($i), $multiway->pointN($i+1)));
  59. $ways[] = $way->asText();
  60. if (!in_array($multiway->pointN($i)->asText(), $joins)) $joins[] = $multiway->pointN($i)->asText();
  61. }
  62. if (!in_array($multiway->pointN($n)->asText(), $joins)) $joins[] = $multiway->pointN($n)->asText();
  63. }
  64. //$db = DB::getDB();
  65. //$db->query("truncate table Rozdzielcza_test_bzyk_ways");
  66. //foreach ($ways as $way) {
  67. // $query = "insert into Rozdzielcza_test_bzyk_ways (the_geom) values (linefromtext('".$way->asText()."'))";
  68. // $db->query($query);
  69. //}
  70. //die();
  71. // todo relacje ways <-> joins
  72. $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";
  73. $result = DB::getPDO()->fetchall($query);
  74. $xpoints = array();
  75. foreach ($result as $row) {
  76. $points[$row['ID']] = Vendor_Geophp::load($row['point'], 'wkt');
  77. $xpoint = Vendor_Geophp::load($row['xpoint'], 'wkt');
  78. if (!($key = array_search($xpoint->asText(), $xpoints))) $key = array_push($xpoints, $xpoint->asText())-1;
  79. $xpoints2points[$key][] = $row['ID'];
  80. $points2xpoints[$row['ID']] = $key;
  81. $way = Vendor_Geophp::load($row['way'], 'wkt');
  82. if (!(in_array($xpoint->asText(), $joins))) {
  83. if (@!in_array($key, $ways2cut[array_search($way->asText(), $ways)])) {
  84. $ways2cut[array_search($way->asText(), $ways)][] = $key;
  85. } else echo ".";
  86. };
  87. }
  88. function pointsDistance($a, $b) {
  89. $line = new LineString(array($a, $b));
  90. return $line->length();
  91. }
  92. foreach ($ways2cut as $way_key => $xpoint_keys) {
  93. $distances = array();
  94. $way = Vendor_Geophp::load($ways[$way_key], 'wkt');
  95. foreach ($xpoint_keys as $xpoint_key) {
  96. $xpoint = Vendor_Geophp::load($xpoints[$xpoint_key], 'wkt');
  97. $joins[] = $xpoint->asText();
  98. $distances[$xpoint_key] = pointsDistance($way->startPoint(), $xpoint);
  99. }
  100. asort($distances);
  101. $sub_xpoints = array();
  102. foreach (array_keys($distances) as $xpoint_key) {
  103. $sub_xpoints[] = Vendor_Geophp::load($xpoints[$xpoint_key]);
  104. }
  105. $startPoint = $way->startPoint();
  106. $endPoint = $way->endPoint();
  107. $way = new LineString(array($startPoint, $sub_xpoints[0]));
  108. $ways[$way_key] = $way->asText();
  109. for ($i = 0; $i < $n = count($sub_xpoints) - 1; $i++) {
  110. $way = new LineString(array($sub_xpoints[$i], $sub_xpoints[$i+1]));
  111. $ways[] = $way->asText();
  112. }
  113. $way = new LineString(array($sub_xpoints[$n], $endPoint));
  114. $ways[] = $way->asText();
  115. }
  116. //print_r($ways);
  117. echo "ways count: ".count($ways)."\n";
  118. /*
  119. $db = DB::getDB();
  120. $db->query("truncate table Rozdzielcza_test_bzyk_ways");
  121. $db->query("truncate table Rozdzielcza_test_bzyk_joins");
  122. foreach ($ways as $way) {
  123. $query = "insert into Rozdzielcza_test_bzyk_ways (the_geom) values (linefromtext('".$way."'))";
  124. $db->query($query);
  125. //echo $query."\n";
  126. }
  127. foreach ($joins as $join) {
  128. $query = "insert into Rozdzielcza_test_bzyk_joins (the_geom) values (pointfromtext('".$join."'))";
  129. $db->query($query);
  130. }
  131. */