biale_plamy-generate_data.php 11 KB

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