biale_plamy-find_ways.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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('/tmp/save_ways2joins.txt'));
  56. $joins2ways = unserialize(file_get_contents('/tmp/save_joins2ways.txt'));
  57. $points2joins = unserialize(file_get_contents('/tmp/save_points2joins.txt'));
  58. $joins_asText = unserialize(file_get_contents('/tmp/save_joins_asText.txt'));
  59. $ways_asText = unserialize(file_get_contents('/tmp/save_ways_asText.txt'));
  60. $points_asText = unserialize(file_get_contents('/tmp/save_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. $way = Vendor_Geophp::load($ways_asText[$way_key], 'wkt');
  120. if ($way) $distance_cur += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->length();
  121. else $distance_cur += 999;
  122. }
  123. if (!$shortestKey || ($shortestKey && $distance_cur < $distance)) {
  124. $shortestKey = $key;
  125. $distance = $distance_cur;
  126. }
  127. }
  128. return $subPaths[$key];
  129. }
  130. }
  131. else {
  132. return null;
  133. }
  134. }
  135. }
  136. function findJoinInDB($id) {
  137. global $joins_asText;
  138. $join = DB::getPDO()->fetchValue("select astext(the_geom) from Rozdzielcza_test_bzyk_joins where ID='{$id}'");
  139. if ($join) {
  140. $join = Vendor_Geophp::load($join, 'wkt')->asText();
  141. $key = array_search($join, $joins_asText);
  142. if ($key) return $key;
  143. else return null;
  144. } else return null;
  145. }
  146. function array_to_xml( $data, &$xml_data ) {
  147. foreach( $data as $key => $value ) {
  148. $key_="";
  149. if( is_numeric($key) ){
  150. $key_ = 'item';
  151. }
  152. if( is_array($value) ) {
  153. $subnode = $xml_data->addChild($key_);
  154. $subnode->addAttribute('id', $key);
  155. array_to_xml($value, $subnode);
  156. } else {
  157. // $test_expl=explode(" ", $value);
  158. //preg_match('/[a-zA-Z]/', $value[0])
  159. //if($key_=="" and !isset($test_expl[1]) ) $key_=htmlspecialchars("$value");
  160. //czy nie jest jeden element array
  161. if($key_=="" and count($data[$key]==1)) {
  162. $subnode=$xml_data->addChild("$key",htmlspecialchars("$value"));
  163. } else
  164. $subnode=$xml_data->addChild("$key_",htmlspecialchars("$value"));
  165. if( is_numeric($key) ){
  166. $subnode->addAttribute('id', $key);
  167. }
  168. }
  169. }
  170. }
  171. #echo joins2lineString($res = findWay(100,110))->asText();
  172. #echo joins2lineString($res = findWay(25690,20134))->asText();
  173. #$path = joins2lineString(findWay(25690,23039));
  174. //$path = joins2lineString(findWay(findJoinInDB(49),findJoinInDB(50)));
  175. //generujemy path do $edge_joins_filled_paths
  176. DB::getPDO()->query("truncate table Rozdzielcza_test_bzyk_paths");
  177. unset($ID_Way);
  178. $edge_joins_filled_paths=array();
  179. foreach($edge_joins_path as $ID_Way=>$Joins_arr) {
  180. settype($ID_Way, "integer");
  181. $edge_joins_filled_paths[$ID_Way]['tried']=1;
  182. if(isset($Joins_arr[1]) and isset($Joins_arr[2])) {
  183. $pkt_A=$Joins_arr[1];$pkt_B=$Joins_arr[2];
  184. settype($pkt_A, "integer"); settype($pkt_B, "integer");
  185. echo "\n<br> Generuje dla ID_Way:".$ID_Way." i joins ".$pkt_A." , ".$pkt_B." ";
  186. unset($res);unset($path);
  187. $res = findWay($pkt_A,$pkt_B);
  188. $path = joins2lineString($res)->asText();
  189. // joins2lineString(findWay(389,404));
  190. settype($path, "string");
  191. $edge_joins_filled_paths[$ID_Way]['asText'] = $path;
  192. $distance = 0;
  193. foreach ($res as $way_key) {
  194. if(isset($ways_asText[$way_key]))
  195. $distance += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->greatCircleLength();
  196. }
  197. $edge_joins_filled_paths[$ID_Way]['distance'] = $distance;
  198. //joins2lineString(findWay($Joins_arr[1],$Joins_arr[2]));
  199. //joins2lineString($res = findWay($joinAB['A'],$joinAB['B']))->asText();
  200. $sql_insert_path="insert into Rozdzielcza_test_bzyk_paths (the_geom,ID_Way, A_RECORD_CREATE_DATE )
  201. values (ST_GeomFromText('".$edge_joins_filled_paths[$ID_Way]['asText']."'),'".$ID_Way."' , now() ) ";
  202. echo "#216Query path ins:: ".$sql_insert_path." , distance:".$distance." ";
  203. if(strlen($edge_joins_filled_paths[$ID_Way]['asText'])>2)
  204. DB::getPDO()->query($sql_insert_path);
  205. else echo "#296 path nie wygenerowalo sie !! dla ".$ID_Way." i pkt ".$pkt_A." , ".$pkt_B." \n";
  206. } else echo "\n#220 droga nie ma koncow ID_Way::".$ID_Way;
  207. }
  208. echo "<br>\n edge_joins_filled_paths:";
  209. print_r($edge_joins_filled_paths);
  210. //save
  211. unset($xml_data);
  212. file_put_contents('edge_joins_filled_paths.txt',serialize($edge_joins_filled_paths));
  213. ob_start();print_r($edge_joins_filled_paths); $contents = ob_get_contents();ob_end_clean();
  214. file_put_contents('edge_joins_filled_paths_print_r.txt',$contents);
  215. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><edge_joins_filled_paths></edge_joins_filled_paths>');
  216. array_to_xml($edge_joins_filled_paths,$xml_data);
  217. print $xml_data->asXML("edge_joins_filled_paths.xml");
  218. unset($xml_data);
  219. //test
  220. $timeStart = microtime();
  221. $res = findWay(389,404);
  222. $path=joins2lineString($res)->asText();
  223. $timeEnd = microtime();
  224. if ($path) {
  225. echo $path."\n";
  226. } else echo "Brak drogi";
  227. list($usecStart, $secStart) = explode(" ", $timeStart);
  228. list($usecEnd, $secEnd) = explode(" ", $timeEnd);
  229. $diff = $secEnd - $secStart + $usecEnd - $usecStart;
  230. echo "\n\nCzas liczenia: ".$diff." s\n";
  231. $distance = 0;
  232. foreach ($res as $way_key) {
  233. $distance += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->greatCircleLength();
  234. }
  235. print_r($res);
  236. echo "Distance: ".$distance."m\n";
  237. //eof test
  238. echo "\n<br>test2";
  239. //test
  240. $timeStart = microtime();
  241. $res = findWay(389,404);
  242. $path=joins2lineString($res)->asText();
  243. $timeEnd = microtime();
  244. if ($path) {
  245. echo $path."\n";
  246. } else echo "Brak drogi";
  247. list($usecStart, $secStart) = explode(" ", $timeStart);
  248. list($usecEnd, $secEnd) = explode(" ", $timeEnd);
  249. $diff = $secEnd - $secStart + $usecEnd - $usecStart;
  250. echo "\n\nCzas liczenia: ".$diff." s\n";
  251. $distance = 0;
  252. foreach ($res as $way_key) {
  253. $distance += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->greatCircleLength();
  254. }
  255. print_r($res);
  256. echo "Distance: ".$distance."m\n";
  257. //eof test
  258. //$distance = 0;
  259. //foreach ($res as $way_key) {
  260. // $distance += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->greatCircleLength();
  261. //}
  262. //print_r($res);
  263. //echo "Distance: ".$distance."m\n";