biale_plamy-find_ways.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. $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. $edge_joins=simplexml_load_file("edge_joins.xml");
  62. $edge_joins->registerXPathNamespace('bp', 'https://procesy5.pl/biale_plamy-schema.xsd');
  63. //print_r($edge_joins);
  64. foreach($edge_joins->children() as $child) {
  65. // echo "\n<br>";
  66. //$attrs=$child->attributes("ID_Way");
  67. foreach($child->attributes() as $a=>$b) {
  68. // echo "#87Child";print_r($b[0]);
  69. $ID_Way=$b[0];
  70. }
  71. //echo $child->getName() . ": " . " ID_Way: ".$ID_Way. " : ";
  72. $id_j=0;
  73. settype($ID_Way, "integer");
  74. //$ID_Way=var_dump($ID_Way);
  75. // echo "#88Child:"; print_r($ID_Way);
  76. //$edge_joins_path['ID_Way'][$attrs['ID_Way']]=array();
  77. foreach($child->children('https://procesy5.pl/biale_plamy-schema.xsd') as $edge_joins_xml) {
  78. $id_j++;
  79. $attrs_join=$edge_joins_xml->attributes();
  80. //echo " #93 ID_Join ".$attrs_join["Joins"]." ";
  81. $Join=$attrs_join["Joins"][0];
  82. settype($Join, "integer");
  83. $edge_joins_path[$ID_Way][$id_j]=$attrs_join["Joins"][0];
  84. }
  85. }
  86. //print_r($edge_joins_path);
  87. function joins2lineString($joins_keys) {
  88. global $joins_asText;
  89. if ($joins_keys) {
  90. foreach ($joins_keys as $join_key) {
  91. $points[] = Vendor_Geophp::load($joins_asText[$join_key], 'wkt');
  92. }
  93. return new LineString($points);
  94. } else return null;
  95. }
  96. function findWay($joinA, $joinB, $path = array()) {
  97. global $ways2joins, $joins2ways, $points2joins, $ways_asText, $points_asText;
  98. if ($joinA == $joinB) {
  99. $joinsPath = array($joinB);
  100. foreach (array_reverse($path) as $way_key) {
  101. array_unshift($joinsPath, $ways2joins[$way_key][1 - array_search($joinsPath[0], $ways2joins[$way_key])]);
  102. }
  103. return $joinsPath;
  104. } else {
  105. $subPaths = array();
  106. foreach ($joins2ways[$joinA] as $way_key) {
  107. if (!in_array($way_key, $path)) {
  108. $new_join = $ways2joins[$way_key][1 - array_search($joinA, $ways2joins[$way_key])];
  109. if ($subPath = findWay($new_join, $joinB, array_merge($path,array($way_key)))) $subPaths[] = $subPath;
  110. }
  111. }
  112. if ($subPaths) {
  113. if (count($subPaths) == 1) return $subPaths[0];
  114. else {
  115. $shortestKey = 0;
  116. foreach ($subPaths as $key => $subPath) {
  117. $distance_cur = 0;
  118. foreach ($subPath as $way_key) {
  119. $distance_cur += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->length();
  120. }
  121. if (!$shortestKey || ($shortestKey && $distance_cur < $distance)) {
  122. $shortestKey = $key;
  123. $distance = $distance_cur;
  124. }
  125. }
  126. return $subPaths[$key];
  127. }
  128. }
  129. else {
  130. return null;
  131. }
  132. }
  133. }
  134. function findJoinInDB($id) {
  135. global $joins_asText;
  136. $join = DB::getPDO()->fetchValue("select astext(the_geom) from Rozdzielcza_test_bzyk_joins where ID='{$id}'");
  137. if ($join) {
  138. $join = Vendor_Geophp::load($join, 'wkt')->asText();
  139. $key = array_search($join, $joins_asText);
  140. if ($key) return $key;
  141. else return null;
  142. } else return null;
  143. }
  144. function array_to_xml( $data, &$xml_data ) {
  145. foreach( $data as $key => $value ) {
  146. $key_="";
  147. if( is_numeric($key) ){
  148. $key_ = 'item';
  149. }
  150. if( is_array($value) ) {
  151. $subnode = $xml_data->addChild($key_);
  152. $subnode->addAttribute('id', $key);
  153. array_to_xml($value, $subnode);
  154. } else {
  155. // $test_expl=explode(" ", $value);
  156. //preg_match('/[a-zA-Z]/', $value[0])
  157. //if($key_=="" and !isset($test_expl[1]) ) $key_=htmlspecialchars("$value");
  158. //czy nie jest jeden element array
  159. if($key_=="" and count($data[$key]==1)) {
  160. $subnode=$xml_data->addChild("$key",htmlspecialchars("$value"));
  161. } else
  162. $subnode=$xml_data->addChild("$key_",htmlspecialchars("$value"));
  163. if( is_numeric($key) ){
  164. $subnode->addAttribute('id', $key);
  165. }
  166. }
  167. }
  168. }
  169. #echo joins2lineString($res = findWay(100,110))->asText();
  170. #echo joins2lineString($res = findWay(25690,20134))->asText();
  171. #$path = joins2lineString(findWay(25690,23039));
  172. //$path = joins2lineString(findWay(findJoinInDB(49),findJoinInDB(50)));
  173. //generujemy path do $edge_joins_filled_paths
  174. DB::getPDO()->query("truncate table Rozdzielcza_test_bzyk_paths");
  175. unset($ID_Way);
  176. $edge_joins_filled_paths=array();
  177. foreach($edge_joins_path as $ID_Way=>$Joins_arr) {
  178. settype($ID_Way, "integer");
  179. $edge_joins_filled_paths[$ID_Way]['tried']=1;
  180. if(isset($Joins_arr[1]) and isset($Joins_arr[2])) {
  181. $pkt_A=$Joins_arr[1];$pkt_B=$Joins_arr[2];
  182. settype($pkt_A, "integer"); settype($pkt_B, "integer");
  183. echo "\n<br> Generuje dla ID_Way:".$ID_Way." i joins ".$pkt_A." , ".$pkt_B." ";
  184. unset($res);unset($path);
  185. $res = findWay($pkt_A,$pkt_B);
  186. $path = joins2lineString($res)->asText();
  187. // joins2lineString(findWay(389,404));
  188. settype($path, "string");
  189. $edge_joins_filled_paths[$ID_Way]['asText'] = $path;
  190. $distance = 0;
  191. foreach ($res as $way_key) {
  192. if(isset($ways_asText[$way_key]))
  193. $distance += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->greatCircleLength();
  194. }
  195. $edge_joins_filled_paths[$ID_Way]['distance'] = $distance;
  196. //joins2lineString(findWay($Joins_arr[1],$Joins_arr[2]));
  197. //joins2lineString($res = findWay($joinAB['A'],$joinAB['B']))->asText();
  198. $sql_insert_path="insert into Rozdzielcza_test_bzyk_paths (the_geom,ID_Way)
  199. values (ST_GeomFromText('".$edge_joins_filled_paths[$ID_Way]['asText']."'),'".$ID_Way."' ) ";
  200. echo "#216Query path ins:: ".$sql_insert_path." , distance:".$distance." ";
  201. if(strlen($edge_joins_filled_paths[$ID_Way]['asText'])>2)
  202. DB::getPDO()->query($sql_insert_path);
  203. else echo "#296 path nie wygenerowalo sie !! dla ".$ID_Way." i pkt ".$pkt_A." , ".$pkt_B." \n";
  204. } else echo "\n#220 droga nie ma koncow ID_Way::".$ID_Way;
  205. }
  206. echo "<br>\n edge_joins_filled_paths:";
  207. print_r($edge_joins_filled_paths);
  208. //save
  209. unset($xml_data);
  210. file_put_contents('edge_joins_filled_paths.txt',serialize($edge_joins_filled_paths));
  211. ob_start();print_r($edge_joins_filled_paths); $contents = ob_get_contents();ob_end_clean();
  212. file_put_contents('edge_joins_filled_paths_print_r.txt',$contents);
  213. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><edge_joins_filled_paths></edge_joins_filled_paths>');
  214. array_to_xml($edge_joins_filled_paths,$xml_data);
  215. print $xml_data->asXML("edge_joins_filled_paths.xml");
  216. unset($xml_data);
  217. //test
  218. $timeStart = microtime();
  219. $res = findWay(389,404);
  220. $path=joins2lineString($res)->asText();
  221. $timeEnd = microtime();
  222. if ($path) {
  223. echo $path."\n";
  224. } else echo "Brak drogi";
  225. list($usecStart, $secStart) = explode(" ", $timeStart);
  226. list($usecEnd, $secEnd) = explode(" ", $timeEnd);
  227. $diff = $secEnd - $secStart + $usecEnd - $usecStart;
  228. echo "\n\nCzas liczenia: ".$diff." s\n";
  229. $distance = 0;
  230. foreach ($res as $way_key) {
  231. $distance += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->greatCircleLength();
  232. }
  233. print_r($res);
  234. echo "Distance: ".$distance."m\n";
  235. //eof test
  236. echo "\n<br>test2";
  237. //test
  238. $timeStart = microtime();
  239. $res = findWay(389,404);
  240. $path=joins2lineString($res)->asText();
  241. $timeEnd = microtime();
  242. if ($path) {
  243. echo $path."\n";
  244. } else echo "Brak drogi";
  245. list($usecStart, $secStart) = explode(" ", $timeStart);
  246. list($usecEnd, $secEnd) = explode(" ", $timeEnd);
  247. $diff = $secEnd - $secStart + $usecEnd - $usecStart;
  248. echo "\n\nCzas liczenia: ".$diff." s\n";
  249. $distance = 0;
  250. foreach ($res as $way_key) {
  251. $distance += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->greatCircleLength();
  252. }
  253. print_r($res);
  254. echo "Distance: ".$distance."m\n";
  255. //eof test
  256. //$distance = 0;
  257. //foreach ($res as $way_key) {
  258. // $distance += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->greatCircleLength();
  259. //}
  260. //print_r($res);
  261. //echo "Distance: ".$distance."m\n";