biale_plamy.php_ 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. $_SERVER['SERVER_NAME'] = 'prezes.procesy5.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 { echo "<br> this script could be run from bash e.g. php biale_plamy.php --help <br>" ;
  21. check();
  22. }
  23. }
  24. function check() {
  25. echo "<br>\n i run check()";
  26. //todo to set arguments to rebuild tables etc:
  27. $check_tbls=array('Rozdzielcza_test_bzyk_drogi','Rozdzielcza_test_bzyk_joins','Rozdzielcza_test_bzyk_przylacza_HIST',
  28. 'Rozdzielcza_test_bzyk_przylacza','Rozdzielcza_test_bzyk_punkty_adresowe','Rozdzielcza_test_bzyk_ways');
  29. //$tables = DB::getPDO()->list_tables();
  30. foreach($check_tbls as $tbl) {
  31. $query = "select ID from ".$tbl." limit 1 ";
  32. echo "<br>\nresult chk for tbl:".$tbl." ;";
  33. try{
  34. $result = DB::getPDO()->fetchall($query);
  35. }
  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. }
  43. catch(Exception $exception2){
  44. echo "<br>\n jest blad dump dla tbl:".$tbl;
  45. }
  46. }
  47. // print_r($exception);
  48. print_r($result);
  49. }
  50. //
  51. //phpinfo();
  52. }
  53. init_sh_check();
  54. die();
  55. $query = "select astext(the_geom) as multiway from Rozdzielcza_test_bzyk_drogi where numpoints(the_geom)>1";
  56. $result = DB::getPDO()->fetchall($query);
  57. $joins = array();
  58. foreach ($result as $row) {
  59. $multiway = Vendor_Geophp::load($row['multiway'], 'wkt');
  60. //echo $multiway->length();
  61. for ($i = 1; $i < $n = $multiway->numPoints(); $i++) {
  62. $ways[] = new LineString(array($multiway->pointN($i), $multiway->pointN($i+1)));
  63. if (!in_array($multiway->pointN($i)->asText(), $joins)) $joins[] = $multiway->pointN($i)->asText();
  64. }
  65. if (!in_array($multiway->pointN($n)->asText(), $joins)) $joins[] = $multiway->pointN($n)->asText();
  66. }
  67. // todo relacje ways <-> joins
  68. $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";
  69. $result = DB::getPDO()->fetchall($query);
  70. $xpoints = array();
  71. foreach ($result as $row) {
  72. $points[$row['ID']] = Vendor_Geophp::load($row['point'], 'wkt');
  73. $xpoint = Vendor_Geophp::load($row['xpoint'], 'wkt');
  74. if (!($key = array_search($xpoint->asText(), $xpoints))) $key = array_push($xpoints, $xpoint->asText())-1;
  75. $xpoints2points[$key][] = $row['ID'];
  76. $points2xpoints[$row['ID']] = $key;
  77. $way = Vendor_Geophp::load($row['way'], 'wkt');
  78. if (!(in_array($xpoint->asText(), $joins))) {
  79. if (@!in_array($key, $ways2cut[array_search($way, $ways)])) {
  80. $ways2cut[array_search($way, $ways)][] = $key;
  81. }
  82. };
  83. }
  84. function pointsDistance($a, $b) {
  85. $line = new LineString(array($a, $b));
  86. return $line->length();
  87. }
  88. foreach ($ways2cut as $way_key => $xpoint_keys) {
  89. $distances = array();
  90. foreach ($xpoint_keys as $xpoint_key) {
  91. $xpoint = Vendor_Geophp::load($xpoints[$xpoint_key], 'wkt');
  92. $joins[] = $xpoint;
  93. $distances[$xpoint_key] = pointsDistance($ways[$way_key]->startPoint(), $xpoint);
  94. }
  95. asort($distances);
  96. $sub_xpoints = array();
  97. foreach (array_keys($distances) as $xpoint_key) {
  98. $sub_xpoints[] = Vendor_Geophp::load($xpoints[$xpoint_key]);
  99. }
  100. $startPoint = $ways[$way_key]->startPoint();
  101. $endPoint = $ways[$way_key]->endPoint();
  102. $ways[$way_key] = new LineString(array($startPoint, $sub_xpoints[0]));
  103. for ($i = 0; $i < $n = count($sub_xpoints) - 1; $i++) {
  104. $ways[] = new LineString(array($sub_xpoints[$i], $sub_xpoints[$i+1]));
  105. }
  106. $ways[] = new LineString(array($sub_xpoints[$n], $endPoint));
  107. }
  108. //print_r($ways);
  109. /*
  110. $db = DB::getDB();
  111. $db->query("truncate table Rozdzielcza_test_bzyk_ways");
  112. $db->query("truncate table Rozdzielcza_test_bzyk_joins");
  113. foreach ($ways as $way) {
  114. $query = "insert into Rozdzielcza_test_bzyk_ways (the_geom) values (linefromtext('".$way->asText()."'))";
  115. $db->query($query);
  116. }
  117. foreach ($xpoints as $xpoint) {
  118. $xpoint2 = Vendor_Geophp::load($xpoint, 'wkt');
  119. $query = "insert into Rozdzielcza_test_bzyk_joins (the_geom) values (pointfromtext('".$xpoint2->asText()."'))";
  120. $db->query($query);
  121. }
  122. */