biale_plamy-generate_data.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. if($sub_xpoints[$n] and $endPoint) {
  137. $way = new LineString(array($sub_xpoints[$n], $endPoint));
  138. $ways_asText[] = $way->asText();
  139. $ways_2ID_Way[]=$ID_Way;
  140. }
  141. }
  142. foreach (array_keys($ways_asText) as $way_key) {
  143. $way = Vendor_Geophp::load($ways_asText[$way_key], 'wkt');
  144. $ways_lenghts[$way_key] = $way->length();
  145. for ($i = 1; $i <= 2; $i++) {
  146. $join = $way->pointN($i)->asText();
  147. $join_key = array_search($join, $joins_asText);
  148. //if (!$join_key) echo ".";
  149. $ways2joins[$way_key][] = $join_key;
  150. $joins2ways[$join_key][] = $way_key;
  151. }
  152. }
  153. /*
  154. // $val=array(A=>asd,B=>cos) ---
  155. // <xml>
  156. // <A>cos</A>
  157. <B>cos></B>
  158. </xml>
  159. array(0=asd,1=asd)
  160. <xml>
  161. <item id=0>asd</>
  162. <item id=2>asd</>
  163. array(A=>asd,B=>array(0=>asd,1=>asd))
  164. <xml>
  165. <A>asd</>
  166. <B><item id=0>asd</>
  167. <item id=1>asd</>
  168. // $pozA[1]=dupa
  169. // $pozA[2]=jasiu
  170. */
  171. // function defination to convert array to xml
  172. function array_to_xml( $data, &$xml_data ) {
  173. foreach( $data as $key => $value ) {
  174. $key_="";
  175. if( is_numeric($key) ){
  176. $key_ = 'item';
  177. }
  178. if( is_array($value) ) {
  179. $subnode = $xml_data->addChild($key_);
  180. $subnode->addAttribute('id', $key);
  181. array_to_xml($value, $subnode);
  182. } else {
  183. $subnode=$xml_data->addChild("$key_",htmlspecialchars("$value"));
  184. if( is_numeric($key) ){
  185. $subnode->addAttribute('id', $key);
  186. }
  187. }
  188. }
  189. }
  190. file_put_contents('points2ID_Way.txt',serialize($points2ID_Way));
  191. //for better debug to see variables in editor i make also print_r
  192. ob_start();print_r($points2ID_Way); $contents = ob_get_contents();ob_end_clean();
  193. file_put_contents('points2ID_Way_print_r.txt',$contents);
  194. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><points2ID_Way></points2ID_Way>');
  195. array_to_xml($points2ID_Way,$xml_data);
  196. print $xml_data->asXML("points2ID_Way.xml");
  197. unset($xml_data);
  198. file_put_contents('ways_2ID_Way.txt',serialize($ways_2ID_Way));
  199. //for better debug to see variables in editor i make also print_r
  200. ob_start();print_r($ways_2ID_Way); $contents = ob_get_contents();ob_end_clean();
  201. file_put_contents('ways_2ID_Way_print_r.txt',$contents);
  202. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><ways_2ID_Way></ways_2ID_Way>');
  203. array_to_xml($ways_2ID_Way,$xml_data);
  204. print $xml_data->asXML("ways_2ID_Way.xml");
  205. unset($xml_data);
  206. file_put_contents('ways2joins.txt',serialize($ways2joins));
  207. //for better debug to see variables in editor i make also print_r
  208. ob_start();print_r($ways2joins); $contents = ob_get_contents();ob_end_clean();
  209. file_put_contents('ways2joins_print_r.txt',$contents);
  210. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><ways2joins></ways2joins>');
  211. array_to_xml($ways2joins,$xml_data);
  212. print $xml_data->asXML("ways2joins.xml");
  213. unset($xml_data);
  214. file_put_contents('joins2ways.txt',serialize($joins2ways));
  215. ob_start();print_r($joins2ways); $contents = ob_get_contents();ob_end_clean();
  216. file_put_contents('joins2ways_print_r.txt',$contents);
  217. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><joins2ways></joins2ways>');
  218. array_to_xml($joins2ways,$xml_data);
  219. print $xml_data->asXML("joins2ways.xml");
  220. unset($xml_data);
  221. file_put_contents('points2joins.txt',serialize($points2joins));
  222. ob_start();print_r($points2joins); $contents = ob_get_contents();ob_end_clean();
  223. file_put_contents('points2joins_print_r.txt',$contents);
  224. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><points2joins></points2joins>');
  225. array_to_xml($points2joins,$xml_data);
  226. print $xml_data->asXML("points2joins.xml");
  227. unset($xml_data);
  228. file_put_contents('joins_asText.txt',serialize($joins_asText));
  229. ob_start();print_r($joins_asText); $contents = ob_get_contents();ob_end_clean();
  230. file_put_contents('joins_asText_print_r.txt',$contents);
  231. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><joins_asText></joins_asText>');
  232. array_to_xml($joins_asText,$xml_data);
  233. print $xml_data->asXML("joins_asText.xml");
  234. unset($xml_data);
  235. file_put_contents('ways_asText.txt',serialize($ways_asText));
  236. ob_start();print_r($ways_asText); $contents = ob_get_contents();ob_end_clean();
  237. file_put_contents('ways_asText_print_r.txt',$contents);
  238. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><ways_asText></ways_asText>');
  239. array_to_xml($ways_asText,$xml_data);
  240. print $xml_data->asXML("ways_asText.xml");
  241. unset($xml_data);
  242. file_put_contents('points_asText.txt',serialize($points_asText));
  243. ob_start();print_r($points_asText); $contents = ob_get_contents();ob_end_clean();
  244. file_put_contents('points_asText_print_r.txt',$contents);
  245. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><points_asText></points_asText>');
  246. array_to_xml($points_asText,$xml_data);
  247. print $xml_data->asXML("points_asText.xml");
  248. unset($xml_data);
  249. /*
  250. function findWay($join_key, $point_key, $path = array()) {
  251. global $ways2joins, $joins2ways, $points2joins, $ways_asText, $joins2ways, $points, $points2xpoints, $xpoints2joins, $ways_lengths, $res;
  252. //echo implode('->',$path)."\n\n";
  253. if ($join_key == $points2joins[$point_key]) $res[] = $path;
  254. else {
  255. foreach ($joins2ways[$join_key] as $way_key) {
  256. if (!in_array($way_key, $path)) {
  257. // $joins = $ways2joins[$way_key];
  258. // $back_join_subkey = array_search($join_key, $joins);
  259. // $new_join_subkey = 1 - $back_join_subkey;
  260. // $new_join = $joins[$new_join_subkey];
  261. $new_join = $ways2joins[$way_key][1 - array_search($join_key, $ways2joins[$way_key])];
  262. findWay($new_join, $point_key, array_merge($path,array($way_key)));
  263. }
  264. }
  265. }
  266. }
  267. */
  268. //print_r($ways2joins);
  269. //for ($i = 0; $i < count($points); $i++) {
  270. // if (isset($res)) unset($res);
  271. // findWay(2000, $i);
  272. // print_r($res);
  273. //}
  274. //print_r($ways);
  275. echo "ways count: ".count($ways_asText)."\n";
  276. $db = DB::getDB();
  277. $db->query("truncate table Rozdzielcza_test_bzyk_ways");
  278. $db->query("truncate table Rozdzielcza_test_bzyk_joins");
  279. foreach ($ways_asText as $way) {
  280. $query = "insert into Rozdzielcza_test_bzyk_ways (the_geom) values (linefromtext('".$way."'))";
  281. $db->query($query);
  282. //echo $query."\n";
  283. }
  284. foreach ($joins_asText as $join) {
  285. $query = "insert into Rozdzielcza_test_bzyk_joins (the_geom) values (pointfromtext('".$join."'))";
  286. $db->query($query);
  287. }