biale_plamy-generate_data.php 12 KB

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