biale_plamy-generate_data.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. ini_set('display_errors', 'On');
  3. error_reporting(E_ALL);
  4. $_SERVER['SERVER_NAME'] = 'biuro.biall-net.pl';
  5. //when used in /Library/Server/Web/Data/Sites/bzyk-biale-plamy-php-temp/SE/dev on biuro.biall-net.pl
  6. if( file_exists ( "../../se-lib/bootstrap.php"))
  7. require("../../se-lib/bootstrap.php");
  8. //when used in in native SE
  9. else require("../../../../SE-production-git/SE/se-lib/bootstrap.php");
  10. Lib::loadClass("Vendor_Geophp");
  11. function init_sh_check() {
  12. //check if run from bash
  13. if(isset($_SERVER["argv"])) {
  14. if(@$_SERVER["argv"][1]=='--help') {
  15. echo "
  16. Options are:
  17. php ".$_SERVER["argv"][0]." --check - control if we have needed tables/data
  18. .EOF
  19. ";
  20. } else if(@$_SERVER["argv"][1]=='--check') {
  21. check();
  22. } else echo " bad option, try:
  23. php ".$_SERVER["argv"][0]." --help
  24. .EOF
  25. ";
  26. } else {
  27. echo "<br> this script could be run from bash e.g. php biale_plamy.php --help <br>" ;
  28. check();
  29. }
  30. }
  31. function check() {
  32. echo "<br>\n i run check()";
  33. //todo to set arguments to rebuild tables etc:
  34. $check_tbls=array('Rozdzielcza_test_bzyk_drogi','Rozdzielcza_test_bzyk_joins','Rozdzielcza_test_bzyk_przylacza_HIST',
  35. 'Rozdzielcza_test_bzyk_przylacza','Rozdzielcza_test_bzyk_punkty_adresowe','Rozdzielcza_test_bzyk_ways');
  36. //$tables = DB::getPDO()->list_tables();
  37. foreach($check_tbls as $tbl) {
  38. $query = "select ID from ".$tbl." limit 1 ";
  39. echo "<br>\nresult chk for tbl:".$tbl." ;";
  40. try{
  41. $result = DB::getPDO()->fetchall($query);
  42. } catch(Exception $exception) {
  43. // return $exception;
  44. 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!";
  45. $sql = file_get_contents('../sql/'.$tbl.'.sql');
  46. try{
  47. $result = DB::getPDO()->fetchall($sql);
  48. } catch(Exception $exception2) {
  49. echo "<br>\n jest blad dump dla tbl:".$tbl;
  50. }
  51. }
  52. // print_r($exception);
  53. print_r($result);
  54. }
  55. }
  56. init_sh_check();
  57. $query = "select astext(the_geom) as multiway ,ID as ID_Way from Rozdzielcza_test_bzyk_drogi where numpoints(the_geom)>1";
  58. $result = DB::getPDO()->fetchall($query);
  59. $joins_asText = array();
  60. foreach ($result as $row) {
  61. $multiway = Vendor_Geophp::load($row['multiway'], 'wkt');
  62. //echo $multiway->length();
  63. for ($i = 1; $i < $n = $multiway->numPoints(); $i++) {
  64. $way = new LineString(array($multiway->pointN($i), $multiway->pointN($i+1)));
  65. $ways_asText[] = $way->asText();
  66. $ways_2ID_Way[]=$row['ID_Way'];
  67. if (!in_array($multiway->pointN($i)->asText(), $joins_asText)) $joins_asText[] = $multiway->pointN($i)->asText();
  68. }
  69. if (!in_array($multiway->pointN($n)->asText(), $joins_asText)) $joins_asText[] = $multiway->pointN($n)->asText();
  70. }
  71. $query = "select ID, astext(the_geom) as point, astext(the_geom_nearest_way) as way, astext(the_geom_xpoint) as xpoint, ID_Way from Rozdzielcza_test_bzyk_punkty_adresowe";
  72. $result = DB::getPDO()->fetchall($query);
  73. $xpoints = array();
  74. foreach ($result as $row) {
  75. $points[$row['ID']] = Vendor_Geophp::load($row['point'], 'wkt');
  76. $points2ID_Way[$row['ID']] = $row['ID_Way'] ;
  77. $points_asText[$row['ID']] = $points[$row['ID']]->asText();
  78. $xpoint = Vendor_Geophp::load($row['xpoint'], 'wkt');
  79. //blad dziwny sqix
  80. if($xpoints) {
  81. if (!($key = array_search($xpoint->asText(), $xpoints))) $key = array_push($xpoints, $xpoint->asText())-1;
  82. $xpoints2points[$key][] = $row['ID'];
  83. $points2xpoints[$row['ID']] = $key;
  84. $way = Vendor_Geophp::load($row['way'], 'wkt');
  85. }
  86. //errory sieje z die
  87. if($xpoint) {
  88. if (!(in_array($xpoint->asText(), $joins_asText))) {
  89. if (@!in_array($key, $ways2cut[array_search($way->asText(), $ways_asText)])) {
  90. $ways2cut[array_search($way->asText(), $ways_asText)][] = array('key'=>$key,'ID_Way'=>$row['ID_Way']);
  91. } else echo ".";
  92. };
  93. }
  94. }
  95. function pointsDistance($a, $b) {
  96. $line = new LineString(array($a, $b));
  97. return $line->length();
  98. }
  99. foreach ($ways2cut as $way_key => $xpoint_keys) {
  100. $distances = array();
  101. //$xpoint_keys=$value_arr['key'];
  102. $way = Vendor_Geophp::load($ways_asText[$way_key], 'wkt');
  103. foreach ($xpoint_keys as $xpoint_key_arr) {
  104. if($joins_asText ) {
  105. $xpoint_key=$xpoint_key_arr['key'];
  106. $ID_Way=$xpoint_key_arr['ID_Way'];
  107. if($xpoints[$xpoint_key]) //error sypie
  108. $xpoint = Vendor_Geophp::load($xpoints[$xpoint_key], 'wkt');
  109. if($xpoint) { ///sypie errorem
  110. $key = array_push($joins_asText, $xpoint->asText())-1; //blad
  111. foreach ($xpoints2points[$xpoint_key] as $point_key) {
  112. $points2joins[$point_key] = $key;
  113. }
  114. }
  115. $distances[$xpoint_key] = pointsDistance($way->startPoint(), $xpoint);
  116. }
  117. }
  118. asort($distances);
  119. $sub_xpoints = array();
  120. foreach (array_keys($distances) as $xpoint_key) {
  121. if($xpoints[$xpoint_key]) // if sypie errorami
  122. $sub_xpoints[] = Vendor_Geophp::load($xpoints[$xpoint_key]);
  123. }
  124. $startPoint = $way->startPoint();
  125. $endPoint = $way->endPoint();
  126. if($sub_xpoints[0] and $startPoint) { // sypie erorrami
  127. $way = new LineString(array($startPoint, $sub_xpoints[0]));
  128. $ways_asText[$way_key] = $way->asText();
  129. $ways_2ID_Way[$way_key]=$ID_Way;
  130. }
  131. for ($i = 0; $i < $n = count($sub_xpoints) - 1; $i++) {
  132. $way = new LineString(array($sub_xpoints[$i], $sub_xpoints[$i+1]));
  133. $ways_asText[] = $way->asText();
  134. $ways_2ID_Way[]=$ID_Way;
  135. }
  136. $way = new LineString(array($sub_xpoints[$n], $endPoint));
  137. $ways_asText[] = $way->asText();
  138. $ways_2ID_Way[]=$ID_Way;
  139. }
  140. foreach (array_keys($ways_asText) as $way_key) {
  141. $way = Vendor_Geophp::load($ways_asText[$way_key], 'wkt');
  142. $ways_lenghts[$way_key] = $way->length();
  143. for ($i = 1; $i <= 2; $i++) {
  144. $join = $way->pointN($i)->asText();
  145. $join_key = array_search($join, $joins_asText);
  146. //if (!$join_key) echo ".";
  147. $ways2joins[$way_key][] = $join_key;
  148. $joins2ways[$join_key][] = $way_key;
  149. }
  150. }
  151. /*
  152. // $val=array(A=>asd,B=>cos) ---
  153. // <xml>
  154. // <A>cos</A>
  155. <B>cos></B>
  156. </xml>
  157. array(0=asd,1=asd)
  158. <xml>
  159. <item id=0>asd</>
  160. <item id=2>asd</>
  161. array(A=>asd,B=>array(0=>asd,1=>asd))
  162. <xml>
  163. <A>asd</>
  164. <B><item id=0>asd</>
  165. <item id=1>asd</>
  166. // $pozA[1]=dupa
  167. // $pozA[2]=jasiu
  168. */
  169. // function defination to convert array to xml
  170. function array_to_xml( $data, &$xml_data ) {
  171. foreach( $data as $key => $value ) {
  172. $key_="";
  173. if( is_numeric($key) ){
  174. $key_ = 'item';
  175. }
  176. if( is_array($value) ) {
  177. $subnode = $xml_data->addChild($key_);
  178. $subnode->addAttribute('id', $key);
  179. array_to_xml($value, $subnode);
  180. } else {
  181. $subnode=$xml_data->addChild("$key_",htmlspecialchars("$value"));
  182. if( is_numeric($key) ){
  183. $subnode->addAttribute('id', $key);
  184. }
  185. }
  186. }
  187. }
  188. file_put_contents('points2ID_Way.txt',serialize($points2ID_Way));
  189. //for better debug to see variables in editor i make also print_r
  190. ob_start();print_r($points2ID_Way); $contents = ob_get_contents();ob_end_clean();
  191. file_put_contents('points2ID_Way_print_r.txt',$contents);
  192. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><points2ID_Way></points2ID_Way>');
  193. array_to_xml($points2ID_Way,$xml_data);
  194. print $xml_data->asXML("points2ID_Way.xml");
  195. unset($xml_data);
  196. file_put_contents('ways_2ID_Way.txt',serialize($ways_2ID_Way));
  197. //for better debug to see variables in editor i make also print_r
  198. ob_start();print_r($ways_2ID_Way); $contents = ob_get_contents();ob_end_clean();
  199. file_put_contents('ways_2ID_Way_print_r.txt',$contents);
  200. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><ways_2ID_Way></ways_2ID_Way>');
  201. array_to_xml($ways_2ID_Way,$xml_data);
  202. print $xml_data->asXML("ways_2ID_Way.xml");
  203. unset($xml_data);
  204. file_put_contents('ways2joins.txt',serialize($ways2joins));
  205. //for better debug to see variables in editor i make also print_r
  206. ob_start();print_r($ways2joins); $contents = ob_get_contents();ob_end_clean();
  207. file_put_contents('ways2joins_print_r.txt',$contents);
  208. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><ways2joins></ways2joins>');
  209. array_to_xml($ways2joins,$xml_data);
  210. print $xml_data->asXML("ways2joins.xml");
  211. unset($xml_data);
  212. file_put_contents('joins2ways.txt',serialize($joins2ways));
  213. ob_start();print_r($joins2ways); $contents = ob_get_contents();ob_end_clean();
  214. file_put_contents('joins2ways_print_r.txt',$contents);
  215. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><joins2ways></joins2ways>');
  216. array_to_xml($joins2ways,$xml_data);
  217. print $xml_data->asXML("joins2ways.xml");
  218. unset($xml_data);
  219. file_put_contents('points2joins.txt',serialize($points2joins));
  220. ob_start();print_r($points2joins); $contents = ob_get_contents();ob_end_clean();
  221. file_put_contents('points2joins_print_r.txt',$contents);
  222. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><points2joins></points2joins>');
  223. array_to_xml($points2joins,$xml_data);
  224. print $xml_data->asXML("points2joins.xml");
  225. unset($xml_data);
  226. file_put_contents('joins_asText.txt',serialize($joins_asText));
  227. ob_start();print_r($joins_asText); $contents = ob_get_contents();ob_end_clean();
  228. file_put_contents('joins_asText_print_r.txt',$contents);
  229. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><joins_asText></joins_asText>');
  230. array_to_xml($joins_asText,$xml_data);
  231. print $xml_data->asXML("joins_asText.xml");
  232. unset($xml_data);
  233. file_put_contents('ways_asText.txt',serialize($ways_asText));
  234. ob_start();print_r($ways_asText); $contents = ob_get_contents();ob_end_clean();
  235. file_put_contents('ways_asText_print_r.txt',$contents);
  236. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><ways_asText></ways_asText>');
  237. array_to_xml($ways_asText,$xml_data);
  238. print $xml_data->asXML("ways_asText.xml");
  239. unset($xml_data);
  240. file_put_contents('points_asText.txt',serialize($points_asText));
  241. ob_start();print_r($points_asText); $contents = ob_get_contents();ob_end_clean();
  242. file_put_contents('points_asText_print_r.txt',$contents);
  243. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><points_asText></points_asText>');
  244. array_to_xml($points_asText,$xml_data);
  245. print $xml_data->asXML("points_asText.xml");
  246. unset($xml_data);
  247. /*
  248. function findWay($join_key, $point_key, $path = array()) {
  249. global $ways2joins, $joins2ways, $points2joins, $ways_asText, $joins2ways, $points, $points2xpoints, $xpoints2joins, $ways_lengths, $res;
  250. //echo implode('->',$path)."\n\n";
  251. if ($join_key == $points2joins[$point_key]) $res[] = $path;
  252. else {
  253. foreach ($joins2ways[$join_key] as $way_key) {
  254. if (!in_array($way_key, $path)) {
  255. // $joins = $ways2joins[$way_key];
  256. // $back_join_subkey = array_search($join_key, $joins);
  257. // $new_join_subkey = 1 - $back_join_subkey;
  258. // $new_join = $joins[$new_join_subkey];
  259. $new_join = $ways2joins[$way_key][1 - array_search($join_key, $ways2joins[$way_key])];
  260. findWay($new_join, $point_key, array_merge($path,array($way_key)));
  261. }
  262. }
  263. }
  264. }
  265. */
  266. //print_r($ways2joins);
  267. //for ($i = 0; $i < count($points); $i++) {
  268. // if (isset($res)) unset($res);
  269. // findWay(2000, $i);
  270. // print_r($res);
  271. //}
  272. //print_r($ways);
  273. echo "ways count: ".count($ways_asText)."\n";
  274. $db = DB::getDB();
  275. $db->query("truncate table Rozdzielcza_test_bzyk_ways");
  276. $db->query("truncate table Rozdzielcza_test_bzyk_joins");
  277. foreach ($ways_asText as $way) {
  278. $query = "insert into Rozdzielcza_test_bzyk_ways (the_geom) values (linefromtext('".$way."'))";
  279. $db->query($query);
  280. //echo $query."\n";
  281. }
  282. foreach ($joins_asText as $join) {
  283. $query = "insert into Rozdzielcza_test_bzyk_joins (the_geom) values (pointfromtext('".$join."'))";
  284. $db->query($query);
  285. }