biale_plamy_class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. class showProgress {
  10. private $startTime;
  11. private $i = 0, $l = 0, $left = 0, $diff = 0, $n, $msg;
  12. private $strlen = 40;
  13. public function __construct($msg, $n = null) {
  14. $this->msg = str_pad($msg, $this->strlen);
  15. echo "{$this->msg}: 0%";
  16. $this->n = $n;
  17. list($usec, $sec) = explode(" ", microtime());
  18. $this->startTime = $sec + $usec;
  19. }
  20. public function step($int = 1) {
  21. if ($this->n) {
  22. $this->i += $int;
  23. $p = floor(100 * $this->i / $this->n);
  24. list($usec, $sec) = explode(" ", microtime());
  25. $curTime = $sec + $usec;
  26. $diff = $curTime - $this->startTime;
  27. $left = floor((($this->n / $this->i) - 1) * $diff);
  28. $diff = floor($diff);
  29. if ($p > $this->l || $left != $this->left || $diff != $this->diff) {
  30. $this->l = $p;
  31. $this->diff = $diff;
  32. $this->left = $left;
  33. if ($left > 59) $left = floor($left / 60) . "m " . ($left % 60);
  34. if ($diff > 59) $diff = floor($diff / 60) . "m " . ($diff % 60);
  35. echo "\r".str_pad("", $this->strlen+60);
  36. echo "\r{$this->msg}: {$p}% (elapsed {$diff}s, estimated remaining {$left}s)";
  37. }
  38. }
  39. }
  40. public function stop() {
  41. list($usec, $sec) = explode(" ", microtime());
  42. $curTime = $sec + $usec;
  43. $diff = $curTime - $this->startTime;
  44. $diff = floor($diff);
  45. if ($diff > 59) $diff = floor($diff / 60) . "m " . ($diff % 60);
  46. echo "\r".str_pad("", $this->strlen+60);
  47. echo "\r{$this->msg}: OK (done in {$diff}s)\n";
  48. }
  49. }
  50. class bialePlamy {
  51. private $showProgress = false;
  52. private $getSourceData = false, $createTemporaryWaysTable = false, $createTemporaryJoinsTable = false, $createTemporaryPrzylaczaTable = false;
  53. private $punktyAdresoweTable = null, $drogiTable = null;
  54. private $joins_asText = array(), $ways_asText = array(), $xpoints_asText = array(), $points_asText = array(), $przylacza_asText = array();
  55. private $ways2drogi = array(), $ways2joins = array(), $joins2ways = array(), $points2xpoints = array(), $xpoints2points = array(), $przylacza2joins = array(), $przylacza2points = array();
  56. public function __construct($sourceTables) {
  57. $tables = array('pointsTable' => 'punktyAdresoweTable', 'origWaysTable' => 'drogiTable');
  58. try {
  59. if (!is_array($sourceTables)) throw new Exception("bialePlamy :: Wrong parameters");
  60. foreach ($tables as $arg => $table) {
  61. if (!isset($sourceTables[$arg])) throw new Exception("bialePlamy :: Missing parameter {$arg}");
  62. $this->$table = $sourceTables[$arg];
  63. }
  64. } catch (Exception $e) {
  65. throw $e;
  66. }
  67. }
  68. public function enableShowProgress() {
  69. $this->showProgress = true;
  70. }
  71. public function disableShowProgress() {
  72. $this->showProgress = false;
  73. }
  74. private $dataArrays = array('joins_asText', 'ways_asText', 'xpoints_asText', 'points_asText', 'przylacza_asText',
  75. 'ways2drogi', 'ways2joins', 'joins2ways', 'points2xpoints', 'xpoints2points', 'przylacza2joins', 'przylacza2points');
  76. public function saveCache() {
  77. foreach ($this->dataArrays as $array) if ($this->$array) {
  78. if ($this->showProgress) $progress = new showProgress("Saving {$array}");
  79. file_put_contents('/tmp/save_'.$array.'.txt', serialize($this->$array));
  80. if ($this->showProgress) $progress->stop();
  81. }
  82. }
  83. public function saveXML() {
  84. function array_to_xml($data, &$xml_data) {
  85. foreach ($data as $key => $value) {
  86. $key_ = "";
  87. if (is_numeric($key)) $key_ = 'item';
  88. if (is_array($value)) {
  89. $subnode = $xml_data->addChild($key_);
  90. $subnode->addAttribute('id', $key);
  91. array_to_xml($value, $subnode);
  92. } else {
  93. $subnode = $xml_data->addChild("$key_", htmlspecialchars("$value"));
  94. if (is_numeric($key)) $subnode->addAttribute('id', $key);
  95. }
  96. }
  97. }
  98. foreach ($this->dataArrays as $array) if ($this->$array) {
  99. if ($this->showProgress) $progress = new showProgress("Generating XML for {$array}");
  100. $xml = new SimpleXMLElement("<?xml version=\"1.0\"?><{$array}></{$array}>");
  101. array_to_xml($this->$array, $xml);
  102. $xml->asXML("{$array}.xml");
  103. if ($this->showProgress) $progress->stop();
  104. }
  105. }
  106. public function loadCache() {
  107. try {
  108. foreach ($this->dataArrays as $array) if (file_exists('/tmp/save_'.$array.'.txt')) {
  109. if ($this->showProgress) $progress = new showProgress("Loading {$array}");
  110. $this->$array = unserialize(file_get_contents('/tmp/save_'.$array.'.txt'));
  111. if ($this->showProgress) $progress->stop();
  112. }
  113. } catch (Exception $e) {
  114. throw new Exception ($e->getMessage());
  115. }
  116. }
  117. private function saveDB($src, $dst) {
  118. try {
  119. if ($this->showProgress) $progress = new showProgress("Saving points table");
  120. DB::getPDO()->query("truncate table `{$dst}`");
  121. $result = DB::getPDO()->fetchall("show fields from `{$src}`");
  122. foreach ($result as $col) $cols[] = "`{$col['Field']}`";
  123. $query = "insert into `{$dst}` (".implode(", ", $cols).") select * from `{$src}`";
  124. DB::getPDO()->query($query);
  125. if ($this->showProgress) $progress->stop();
  126. } catch (Exception $e) {
  127. throw new Exception ($e->getMessage());
  128. }
  129. }
  130. public function savePointsTable($dst) {
  131. if (!$this->getSourceData) $this->getSourceData();
  132. if ($this->xpoints2points) $this->updateTemporaryPointsTable();
  133. $this->saveDB("_temp_punkty_adresowe", $dst);
  134. }
  135. public function saveWaysTable($dst) {
  136. if (!$this->createTemporaryWaysTable) $this->createTemporaryWaysTable();
  137. $this->saveDB("_temp_ways", $dst);
  138. }
  139. public function saveJoinsTable($dst) {
  140. if (!$this->createTemporaryJoinsTable) $this->createTemporaryJoinsTable();
  141. $this->saveDB("_temp_joins", $dst);
  142. }
  143. public function savePrzylaczaTable($dst) {
  144. if (!$this->createTemporaryPrzylaczaTable) $this->createTemporaryPrzylaczaTable();
  145. $this->saveDB("_temp_przylacza", $dst);
  146. }
  147. private function arrayFrom1() {
  148. $array = array('');
  149. unset($array[0]);
  150. return $array;
  151. }
  152. private function pointsDistance($a, $b) {
  153. $line = new LineString(array($a, $b));
  154. return $line->length();
  155. }
  156. private function getSourceData() {
  157. try {
  158. if ($this->showProgress) $progress = new showProgress("Loading source data from database");
  159. DB::getPDO()->query("create temporary table `_temp_punkty_adresowe` select * from `{$this->punktyAdresoweTable}`");
  160. DB::getPDO()->query("create temporary table `_temp_drogi` select * from `{$this->drogiTable}` where numpoints(the_geom)>1");
  161. DB::getPDO()->query("create index `ID` on `_temp_punkty_adresowe` (`ID`)");
  162. DB::getPDO()->query("create index `ID` on `_temp_drogi` (`ID`)");
  163. $this->getSourceData = true;
  164. if ($this->showProgress) $progress->stop();
  165. } catch (Exception $e) {
  166. throw new Exception($e->getMessage());
  167. }
  168. }
  169. private function generatePoints() {
  170. try {
  171. if (!$this->getSourceData) $this->getSourceData();
  172. if ($this->showProgress) $progress = new showProgress("Generating points");
  173. $result = DB::getPDO()->query("select `ID`, astext(`the_geom`) as `point_asText` from `_temp_punkty_adresowe`");
  174. foreach ($result as $row) $points_asText[$row['ID']] = $row['point_asText'];
  175. $this->points_asText = $points_asText;
  176. if ($this->showProgress) $progress->stop();
  177. } catch (Exception $e) {
  178. throw new Exception($e->getMessage());
  179. }
  180. }
  181. private function createTemporaryWaysTable() {
  182. try {
  183. if ((!$this->ways_asText) || (!$this->ways2drogi)) throw new Exception("createTemporaryWaysTable() :: Ways not generated, use generateWays() first");
  184. DB::getPDO()->query("drop temporary table if exists `_temp_ways`");
  185. DB::getPDO()->query("create temporary table `_temp_ways` (`ID` int(11), `droga_id` int(11), `the_geom` linestring not null, index(`ID`), index(`droga_id`), spatial index(`the_geom`)) ENGINE=MyISAM");
  186. if ($this->showProgress) $progress = new showProgress("Creating temporary ways table", count($this->ways_asText));
  187. foreach ($this->ways_asText as $way_key => $way_asText) {
  188. DB::getPDO()->query("insert into `_temp_ways` values ('{$way_key}', '{$this->ways2drogi[$way_key]}', LineFromText('{$way_asText}'))");
  189. if ($this->showProgress) $progress->step();
  190. }
  191. if ($this->showProgress) $progress->stop();
  192. $this->createTemporaryWaysTable = true;
  193. } catch (Exception $e) {
  194. throw new Exception ($e->getMessage);
  195. }
  196. }
  197. public function generateWays() {
  198. try {
  199. if (!$this->getSourceData) $this->getSourceData();
  200. $ways_asText = $this->arrayFrom1();
  201. $ways2drogi = $this->arrayFrom1();
  202. $result = DB::getPDO()->fetchall("select ID as droga_id, astext(the_geom) as multiway from `_temp_drogi`");
  203. if ($this->showProgress) $progress = new showProgress("Generating ways", count($result));
  204. foreach ($result as $row) {
  205. $multiway = Vendor_Geophp::load($row['multiway'], 'wkt');
  206. for ($i = 1; $i < $n = $multiway->numPoints(); $i++) {
  207. $way = new LineString(array($multiway->pointN($i), $multiway->pointN($i+1)));
  208. $ways_asText[] = $way->asText();
  209. $ways2drogi[] = $row['droga_id'];
  210. }
  211. if ($this->showProgress) $progress->step();
  212. }
  213. if ($this->showProgress) $progress->stop();
  214. $this->ways_asText = $ways_asText;
  215. $this->ways2drogi = $ways2drogi;
  216. $this->joins_asText = array();
  217. $this->ways2joins = array();
  218. $this->joins2ways = array();
  219. $this->xpoints_asText = array();
  220. $this->points2xpoints = array();
  221. $this->xpoints2points = array();
  222. } catch (Exception $e) {
  223. throw new Exception($e->getMessage());
  224. }
  225. }
  226. private function createTemporaryJoinsTable() {
  227. try {
  228. if (!$this->joins_asText) throw new Exception("createTemporaryJoinsTable() :: Joins not generated, use generateJoins() first");
  229. DB::getPDO()->query("drop temporary table if exists `_temp_joins`");
  230. DB::getPDO()->query("create temporary table `_temp_joins` (`ID` int(11), `the_geom` point not null, index(`ID`), spatial index(`the_geom`)) ENGINE=MyISAM");
  231. if ($this->showProgress) $progress = new showProgress("Generating temporary joins table", count($this->joins_asText));
  232. foreach ($this->joins_asText as $join_key => $join_asText) {
  233. DB::getPDO()->query("insert into `_temp_joins` values ('{$join_key}', PointFromText('{$join_asText}'))");
  234. if ($this->showProgress) $progress->step();
  235. }
  236. if ($this->showProgress) $progress->stop();
  237. $this->createTemporaryJoinsTable = true;
  238. } catch (Exception $e) {
  239. throw new Exception ($e->getMessage());
  240. }
  241. }
  242. public function generateJoins() {
  243. try {
  244. if (!$this->ways_asText) throw new Exception("generateJoins() :: Ways not generated, use generateWays() first");
  245. $joins_asText = $this->arrayFrom1();
  246. $ways2joins = array();
  247. $joins2ways = array();
  248. if ($this->showProgress) $progress = new showProgress("Generating joins", count($this->ways_asText));
  249. foreach ($this->ways_asText as $way_key => $way_asText) {
  250. $way = Vendor_Geophp::load($way_asText, 'wkt');
  251. $points_asText = array($way->startPoint()->asText(), $way->endPoint()->asText());
  252. foreach ($points_asText as $point_asText) {
  253. if (!in_array($point_asText, $joins_asText)) {
  254. $join_key = array_push($joins_asText, $point_asText);
  255. $ways2joins[$way_key][] = $join_key;
  256. $joins2ways[$join_key][] = $way_key;
  257. }
  258. }
  259. if ($this->showProgress) $progress->step();
  260. }
  261. if ($this->showProgress) $progress->stop();
  262. $this->joins_asText = $joins_asText;
  263. $this->ways2joins = $ways2joins;
  264. $this->joins2ways = $joins2ways;
  265. } catch (Exception $e) {
  266. throw new Exception($e->getMessage());
  267. }
  268. }
  269. private function updateTemporaryPointsTable() {
  270. try {
  271. if ((!$this->points2xpoints) || (!$this->xpoints_asText)) throw new Exception("updateTemporaryPointsTable() :: Xpoints not generated, use generateXpoints() first");
  272. if (!$this->getSourceData) $this->getSourceData();
  273. if ($this->showProgress) $progress = new showProgress("Updating temporary points table", count($this->points2xpoints));
  274. foreach ($this->points2xpoints as $point_key => $xpoint_key) {
  275. DB::getPDO()->query("update `_temp_punkty_adresowe` set `the_geom_xpoint` = pointfromtext('{$this->xpoints_asText[$xpoint_key]}') where `ID` = '{$point_key}'");
  276. if ($this->showProgress) $progress->step();
  277. }
  278. if ($this->showProgress) $progress->stop();
  279. } catch (Exception $e) {
  280. throw new Exception($e->getMessage());
  281. }
  282. }
  283. public function generateXpoints() {
  284. try {
  285. if (!$this->ways_asText) throw new Exception("generateXpoints() :: Ways not generated, use generateWays() first");
  286. if (!$this->getSourceData) $this->getSourceData();
  287. if (!$this->createTemporaryWaysTable) $this->createTemporaryWaysTable();
  288. $xpoints_asText = $this->arrayFrom1();
  289. $result = DB::getPDO()->fetchall("select `ID`, astext(`the_geom`) as `point_asText` from `_temp_punkty_adresowe`");
  290. if ($this->showProgress) $progress = new showProgress("Generating xpoints", count($result));
  291. foreach ($result as $row) {
  292. if (!($way_key = DB::getPDO()->fetchValue("select `ID` from (select w.`ID`, st_distance(p.`the_geom`, w.`the_geom`) as `distance` from `_temp_punkty_adresowe` p join `_temp_ways` w on p.`ID` = '{$row['ID']}' order by `distance` limit 1) as ways"))) throw new Exception("generateXpoints() :: błąd spójnosci danych - nie znaleziono najbliższej drogi");
  293. $way = Vendor_Geophp::load($this->ways_asText[$way_key], 'wkt');
  294. $a = $way->startPoint();
  295. $b = $way->endPoint();
  296. $p = Vendor_Geophp::load($row['point_asText'], 'wkt');
  297. if ($a->y() == $b->y()) {
  298. $xp = $p->x();
  299. $yp = $a->y();
  300. $deltaA = $p->x() - $a->x();
  301. $deltaB = $p->x() - $b->x();
  302. } else {
  303. if ($a->x() == $b->x()) {
  304. $xp = $a->x();
  305. $yp = $p->y();
  306. $a2 = 0;
  307. $b2 = $p->y();
  308. } else {
  309. $a1 = ($a->y() - $b->y()) / ($a->x() - $b->x());
  310. $b1 = $a->y() - $a1 * $a->x();
  311. $a2 = -1 / $a1;
  312. $b2 = $p->y() - $a2 * $p->x();
  313. $xp = ($b2 - $b1) / ($a1 - $a2);
  314. $yp = $a1 * $xp + $b1;
  315. }
  316. $deltaA = $a2 * $a->x() + $b2 - $a->y();
  317. $deltaB = $a2 * $b->x() + $b2 - $b->y();
  318. }
  319. if ($deltaA * $deltaB >= 0) { // punkt poza linia, ustalamy na blizszy koniec linii
  320. if (abs($deltaA) < abs($deltaB) ) $xpoint = $a;
  321. else $xpoint = $b;
  322. } else { // punkt w linii - trzeba podzielic linie na dwie
  323. $xpoint = new Point($xp, $yp);
  324. $wayA = new LineString(array($a, $xpoint));
  325. $wayB = new LineString(array($xpoint, $b));
  326. $this->ways_asText[$way_key] = $wayA->asText();
  327. $new_way_key = array_push($this->ways_asText, $wayB->asText());
  328. $this->ways2drogi[$new_way_key] = $this->ways2drogi[$way_key];
  329. DB::getPDO()->query("update `_temp_ways` set `the_geom` = linefromtext('{$wayA->asText()}') where `ID` = '{$way_key}'");
  330. DB::getPDO()->query("insert into `_temp_ways` values ('{$new_way_key}', '{$this->ways2drogi[$way_key]}', linefromtext('{$wayB->asText()}'))");
  331. }
  332. if (!($xpoint_key = array_search($xpoint->asText(), $xpoints_asText))) $xpoint_key = array_push($xpoints_asText, $xpoint->asText());
  333. $points2xpoints[$row['ID']] = $xpoint_key;
  334. $xpoints2points[$xpoint_key][] = $row['ID'];
  335. if ($this->showProgress) $progress->step();
  336. }
  337. if ($this->showProgress) $progress->stop();
  338. $this->xpoints_asText = $xpoints_asText;
  339. $this->points2xpoints = $points2xpoints;
  340. $this->xpoints2points = $xpoints2points;
  341. $this->generateJoins();
  342. $this->updateTemporaryPointsTable();
  343. } catch (Exception $e) {
  344. throw new Exception($e->getMessage());
  345. }
  346. }
  347. private function createTemporaryPrzylaczaTable() {
  348. try {
  349. if ((!$this->przylacza_asText) || (!$this->przylacza2joins) || (!$this->przylacza2points)) throw new Exception("createTemporaryPrzylaczaTable() :: Przylacza not generated, use generatePrzylacza() first");
  350. if ($this->showProgress) $progress = new showProgress("Generating temporary table przylacza", count($this->przylacza_asText));
  351. DB::getPDO()->query("drop temporary table if exists `_temp_przylacza`");
  352. DB::getPDO()->query("create temporary table `_temp_przylacza` (`ID` int(11), `points_id` int(11), `joins_id` int(11), `the_geom` linestring not null, index(`ID`)) ENGINE=MyISAM");
  353. foreach ($this->przylacza_asText as $przylacze_key => $przylacze_asText) {
  354. $query = "insert into `_temp_przylacza` values ('{$przylacze_key}', '{$this->przylacza2points[$przylacze_key]}', '{$this->przylacza2joins[$przylacze_key]}', linefromtext('{$przylacze_asText}'))";
  355. DB::getPDO()->query($query);
  356. if ($this->showProgress) $progress->step();
  357. }
  358. if ($this->showProgress) $progress->stop();
  359. $this->createTemporaryPrzylaczaTable = true;
  360. } catch (Exception $e) {
  361. throw new Exception($e->getMessage());
  362. }
  363. }
  364. public function generatePrzylacza() {
  365. try {
  366. if ((!$this->points2xpoints) || (!$this->xpoints_asText)) throw new Exception("generatePrzylaczaTable() :: Xpoints not generated, use generateXpoints() first");
  367. if (!$this->joins_asText) throw new Exception("generatePrzylaczaTable() :: Joins not generated, use generateJoins() first");
  368. if (!$this->points_asText) $this->generatePoints();
  369. $przylacza_asText = $this->arrayFrom1();
  370. $przylacza2joins = $this->arrayFrom1();
  371. $przylacza2points = $this->arrayFrom1();
  372. if ($this->showProgress) $progress = new showProgress("Generating przylacza", count($this->points_asText));
  373. foreach ($this->points_asText as $point_key => $point_asText) {
  374. $xpoint_key = $this->points2xpoints[$point_key];
  375. $join_key = array_search($this->xpoints_asText[$xpoint_key], $this->joins_asText);
  376. $point = Vendor_Geophp::load($this->points_asText[$point_key], 'wkt');
  377. $xpoint = Vendor_Geophp::load($this->xpoints_asText[$xpoint_key], 'wkt');
  378. $przylacze = new Linestring(array($point, $xpoint));
  379. $przylacza_asText[] = $przylacze->asText();
  380. $przylacza2joins[] = $join_key;
  381. $przylacza2points[] = $point_key;
  382. if ($this->showProgress) $progress->step();
  383. }
  384. if ($this->showProgress) $progress->stop();
  385. $this->przylacza_asText = $przylacza_asText;
  386. $this->przylacza2joins = $przylacza2joins;
  387. $this->przylacza2points = $przylacza2points;
  388. } catch (Exception $e) {
  389. throw new Exception($e->getMessage());
  390. }
  391. }
  392. }
  393. //$test = new bialePlamy(array("pointsTable"=>"Rozdzielcza_test2_bzyk_punkty_adresowe", "origWaysTable"=>"Rozdzielcza_test2_bzyk_drogi"));
  394. //$test->enableShowProgress();
  395. //$test->loadCache();
  396. //$test->generateWays();
  397. //$test->generateXpoints();
  398. //$test->generatePrzylacza();
  399. //$test->saveCache();
  400. //$test->saveXML();
  401. //$test->saveWaysTable("Rozdzielcza_test2_bzyk_ways");
  402. //$test->saveJoinsTable("Rozdzielcza_test2_bzyk_joins");
  403. //$test->savePrzylaczaTable("Rozdzielcza_test2_bzyk_przylacza");