biale_plamy-generate_data.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. $_SERVER['SERVER_NAME'] = 'biuro.biall-net.pl';
  3. //when used in /Library/Server/Web/Data/Sites/bzyk-biale-plamy-php-temp/SE/dev on biuro.biall-net.pl
  4. if( file_exists ( "../../se-lib/bootstrap.php"))
  5. require("../../se-lib/bootstrap.php");
  6. //when used in in native SE
  7. else require("../../../../SE-production-git/SE/se-lib/bootstrap.php");
  8. Lib::loadClass("Vendor_Geophp");
  9. function init_sh_check() {
  10. //check if run from bash
  11. if(isset($_SERVER["argv"])) {
  12. if(@$_SERVER["argv"][1]=='--help') {
  13. echo "
  14. Options are:
  15. php ".$_SERVER["argv"][0]." --check - control if we have needed tables/data
  16. .EOF
  17. ";
  18. } else if(@$_SERVER["argv"][1]=='--check') {
  19. check();
  20. } else echo " bad option, try:
  21. php ".$_SERVER["argv"][0]." --help
  22. .EOF
  23. ";
  24. } else {
  25. echo "<br> this script could be run from bash e.g. php biale_plamy.php --help <br>" ;
  26. check();
  27. }
  28. }
  29. function check() {
  30. echo "<br>\n i run check()";
  31. //todo to set arguments to rebuild tables etc:
  32. $check_tbls=array('Rozdzielcza_test_bzyk_drogi','Rozdzielcza_test_bzyk_joins','Rozdzielcza_test_bzyk_przylacza_HIST',
  33. 'Rozdzielcza_test_bzyk_przylacza','Rozdzielcza_test_bzyk_punkty_adresowe','Rozdzielcza_test_bzyk_ways');
  34. //$tables = DB::getPDO()->list_tables();
  35. foreach($check_tbls as $tbl) {
  36. $query = "select ID from ".$tbl." limit 1 ";
  37. echo "<br>\nresult chk for tbl:".$tbl." ;";
  38. try{
  39. $result = DB::getPDO()->fetchall($query);
  40. } catch(Exception $exception) {
  41. // return $exception;
  42. 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!";
  43. $sql = file_get_contents('../sql/'.$tbl.'.sql');
  44. try{
  45. $result = DB::getPDO()->fetchall($sql);
  46. } catch(Exception $exception2) {
  47. echo "<br>\n jest blad dump dla tbl:".$tbl;
  48. }
  49. }
  50. // print_r($exception);
  51. print_r($result);
  52. }
  53. }
  54. init_sh_check();
  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_asText = 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. $way = new LineString(array($multiway->pointN($i), $multiway->pointN($i+1)));
  63. $ways_asText[] = $way->asText();
  64. if (!in_array($multiway->pointN($i)->asText(), $joins_asText)) $joins_asText[] = $multiway->pointN($i)->asText();
  65. }
  66. if (!in_array($multiway->pointN($n)->asText(), $joins_asText)) $joins_asText[] = $multiway->pointN($n)->asText();
  67. }
  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. $points_asText[$row['ID']] = $points[$row['ID']]->asText();
  74. $xpoint = Vendor_Geophp::load($row['xpoint'], 'wkt');
  75. if (!($key = array_search($xpoint->asText(), $xpoints))) $key = array_push($xpoints, $xpoint->asText())-1;
  76. $xpoints2points[$key][] = $row['ID'];
  77. $points2xpoints[$row['ID']] = $key;
  78. $way = Vendor_Geophp::load($row['way'], 'wkt');
  79. if (!(in_array($xpoint->asText(), $joins_asText))) {
  80. if (@!in_array($key, $ways2cut[array_search($way->asText(), $ways_asText)])) {
  81. $ways2cut[array_search($way->asText(), $ways_asText)][] = $key;
  82. } else echo ".";
  83. };
  84. }
  85. function pointsDistance($a, $b) {
  86. $line = new LineString(array($a, $b));
  87. return $line->length();
  88. }
  89. foreach ($ways2cut as $way_key => $xpoint_keys) {
  90. $distances = array();
  91. $way = Vendor_Geophp::load($ways_asText[$way_key], 'wkt');
  92. foreach ($xpoint_keys as $xpoint_key) {
  93. $xpoint = Vendor_Geophp::load($xpoints[$xpoint_key], 'wkt');
  94. $key = array_push($joins_asText, $xpoint->asText())-1;
  95. foreach ($xpoints2points[$xpoint_key] as $point_key) {
  96. $points2joins[$point_key] = $key;
  97. }
  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_asText[$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_asText[] = $way->asText();
  112. }
  113. $way = new LineString(array($sub_xpoints[$n], $endPoint));
  114. $ways_asText[] = $way->asText();
  115. }
  116. foreach (array_keys($ways_asText) as $way_key) {
  117. $way = Vendor_Geophp::load($ways_asText[$way_key], 'wkt');
  118. $ways_lenghts[$way_key] = $way->length();
  119. for ($i = 1; $i <= 2; $i++) {
  120. $join = $way->pointN($i)->asText();
  121. $join_key = array_search($join, $joins_asText);
  122. //if (!$join_key) echo ".";
  123. $ways2joins[$way_key][] = $join_key;
  124. $joins2ways[$join_key][] = $way_key;
  125. }
  126. }
  127. file_put_contents('ways2joins.txt',serialize($ways2joins));
  128. //for better debug to see variables in editor i make also print_r
  129. file_put_contents('ways2joins_print_r.txt',print_r($ways2joins));
  130. file_put_contents('joins2ways.txt',serialize($joins2ways));
  131. file_put_contents('joins2ways_print_r.txt',print_r($joins2ways));
  132. file_put_contents('points2joins.txt',serialize($points2joins));
  133. file_put_contents('points2joins_print_r.txt',print_r($points2joins));
  134. file_put_contents('joins_asText.txt',serialize($joins_asText));
  135. file_put_contents('joins_asText_print_r.txt',print_r($joins_asText));
  136. file_put_contents('ways_asText.txt',serialize($ways_asText));
  137. file_put_contents('ways_asText_print_r.txt',print_r($ways_asText));
  138. file_put_contents('points_asText.txt',serialize($points_asText));
  139. file_put_contents('points_asText_print_r.txt',print_r($points_asText));
  140. /*
  141. function findWay($join_key, $point_key, $path = array()) {
  142. global $ways2joins, $joins2ways, $points2joins, $ways_asText, $joins2ways, $points, $points2xpoints, $xpoints2joins, $ways_lengths, $res;
  143. //echo implode('->',$path)."\n\n";
  144. if ($join_key == $points2joins[$point_key]) $res[] = $path;
  145. else {
  146. foreach ($joins2ways[$join_key] as $way_key) {
  147. if (!in_array($way_key, $path)) {
  148. // $joins = $ways2joins[$way_key];
  149. // $back_join_subkey = array_search($join_key, $joins);
  150. // $new_join_subkey = 1 - $back_join_subkey;
  151. // $new_join = $joins[$new_join_subkey];
  152. $new_join = $ways2joins[$way_key][1 - array_search($join_key, $ways2joins[$way_key])];
  153. findWay($new_join, $point_key, array_merge($path,array($way_key)));
  154. }
  155. }
  156. }
  157. }
  158. */
  159. //print_r($ways2joins);
  160. //for ($i = 0; $i < count($points); $i++) {
  161. // if (isset($res)) unset($res);
  162. // findWay(2000, $i);
  163. // print_r($res);
  164. //}
  165. //print_r($ways);
  166. echo "ways count: ".count($ways_asText)."\n";
  167. $db = DB::getDB();
  168. $db->query("truncate table Rozdzielcza_test_bzyk_ways");
  169. $db->query("truncate table Rozdzielcza_test_bzyk_joins");
  170. foreach ($ways_asText as $way) {
  171. $query = "insert into Rozdzielcza_test_bzyk_ways (the_geom) values (linefromtext('".$way."'))";
  172. $db->query($query);
  173. //echo $query."\n";
  174. }
  175. foreach ($joins_asText as $join) {
  176. $query = "insert into Rozdzielcza_test_bzyk_joins (the_geom) values (pointfromtext('".$join."'))";
  177. $db->query($query);
  178. }