biale_plamy_class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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, $readyForFindWay = false;
  53. private $punktyAdresoweTable = null, $drogiTable = null;
  54. private $drogi_asText = array(), $joins_asText = array(), $ways_asText = array(), $xpoints_asText = array(), $points_asText = array(), $przylacza_asText = array(), $waysLengths = array();
  55. private $ways2drogi = array(), $drogi2ways = array(), $ways2joins = array(), $joins2ways = array(), $points2xpoints = array(), $xpoints2points = array(), $przylacza2joins = array(), $przylacza2points = array(), $points2joins = 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('drogi_asText', 'joins_asText', 'ways_asText', 'xpoints_asText', 'points_asText', 'przylacza_asText', 'waysLengths',
  75. 'ways2drogi', 'drogi2ways', 'ways2joins', 'joins2ways', 'points2xpoints', 'xpoints2points', 'przylacza2joins', 'przylacza2points', 'points2joins');
  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 printArray($array) {
  107. if (isset($this->$array) && is_array($this->$array)) {
  108. return print_r($this->$array, true);
  109. } else return null;
  110. }
  111. public function saveArrays() {
  112. foreach ($this->dataArrays as $array) if ($this->$array) {
  113. if ($this->showProgress) $progress = new showProgress("Saving array {$array} to file");
  114. file_put_contents("/tmp/array_{$array}.txt", $this->printArray($array));
  115. if ($this->showProgress) $progress->stop();
  116. }
  117. }
  118. public function loadCache() {
  119. try {
  120. foreach ($this->dataArrays as $array) if (file_exists('/tmp/save_'.$array.'.txt')) {
  121. if ($this->showProgress) $progress = new showProgress("Loading {$array}");
  122. $this->$array = unserialize(file_get_contents('/tmp/save_'.$array.'.txt'));
  123. if ($this->showProgress) $progress->stop();
  124. }
  125. } catch (Exception $e) {
  126. throw $e;
  127. }
  128. }
  129. private function saveDB($src, $dst) {
  130. try {
  131. if ($this->showProgress) $progress = new showProgress("Saving points table");
  132. DB::getPDO()->query("truncate table `{$dst}`");
  133. $result = DB::getPDO()->fetchall("show fields from `{$src}`");
  134. foreach ($result as $col) $cols[] = "`{$col['Field']}`";
  135. $query = "insert into `{$dst}` (".implode(", ", $cols).") select * from `{$src}`";
  136. DB::getPDO()->query($query);
  137. if ($this->showProgress) $progress->stop();
  138. } catch (Exception $e) {
  139. throw $e;
  140. }
  141. }
  142. public function savePointsTable($dst) {
  143. if (!$this->getSourceData) $this->getSourceData();
  144. if ($this->xpoints2points) $this->updateTemporaryPointsTable();
  145. $this->saveDB("_temp_punkty_adresowe", $dst);
  146. }
  147. public function saveWaysTable($dst) {
  148. if (!$this->createTemporaryWaysTable) $this->createTemporaryWaysTable();
  149. $this->saveDB("_temp_ways", $dst);
  150. }
  151. public function saveJoinsTable($dst) {
  152. if (!$this->createTemporaryJoinsTable) $this->createTemporaryJoinsTable();
  153. $this->saveDB("_temp_joins", $dst);
  154. }
  155. public function savePrzylaczaTable($dst) {
  156. if (!$this->createTemporaryPrzylaczaTable) $this->createTemporaryPrzylaczaTable();
  157. $this->saveDB("_temp_przylacza", $dst);
  158. }
  159. private function arrayFrom1() {
  160. $array = array('');
  161. unset($array[0]);
  162. return $array;
  163. }
  164. private function pointsDistance($a, $b) {
  165. $line = new LineString(array($a, $b));
  166. return $line->length();
  167. }
  168. private function getSourceData() {
  169. try {
  170. if ($this->showProgress) $progress = new showProgress("Loading source data from database");
  171. DB::getPDO()->query("create temporary table `_temp_punkty_adresowe` select * from `{$this->punktyAdresoweTable}` where the_geom is not null");
  172. DB::getPDO()->query("create temporary table `_temp_drogi` select * from `{$this->drogiTable}` where coalesce(numpoints(the_geom),0)>1");
  173. DB::getPDO()->query("create index `ID` on `_temp_punkty_adresowe` (`ID`)");
  174. DB::getPDO()->query("create index `ID` on `_temp_drogi` (`ID`)");
  175. $this->getSourceData = true;
  176. if ($this->showProgress) $progress->stop();
  177. } catch (Exception $e) {
  178. throw $e;
  179. }
  180. }
  181. private function generatePoints() {
  182. try {
  183. if (!$this->getSourceData) $this->getSourceData();
  184. if ($this->showProgress) $progress = new showProgress("Generating points");
  185. $result = DB::getPDO()->query("select `ID`, astext(`the_geom`) as `point_asText` from `_temp_punkty_adresowe`");
  186. foreach ($result as $row) $points_asText[$row['ID']] = $row['point_asText'];
  187. $this->points_asText = $points_asText;
  188. $this->readyForFindWay = false;
  189. if ($this->showProgress) $progress->stop();
  190. } catch (Exception $e) {
  191. throw $e;
  192. }
  193. }
  194. private function createTemporaryWaysTable() {
  195. try {
  196. if ((!$this->ways_asText) || (!$this->ways2drogi)) throw new Exception("createTemporaryWaysTable() :: Ways not generated, use generateWays() first");
  197. DB::getPDO()->query("drop temporary table if exists `_temp_ways`");
  198. 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");
  199. if ($this->showProgress) $progress = new showProgress("Creating temporary ways table", count($this->ways_asText));
  200. foreach ($this->ways_asText as $way_key => $way_asText) {
  201. DB::getPDO()->query("insert into `_temp_ways` values ('{$way_key}', '{$this->ways2drogi[$way_key]}', LineFromText('{$way_asText}'))");
  202. if ($this->showProgress) $progress->step();
  203. }
  204. if ($this->showProgress) $progress->stop();
  205. $this->createTemporaryWaysTable = true;
  206. } catch (Exception $e) {
  207. throw $e;
  208. }
  209. }
  210. public function generateWaysLenghts() {
  211. try {
  212. if (!$this->ways_asText) $this->generateWays;
  213. if ($this->showProgress) $progress = new showProgress("Generating waysLengths", count($this->ways_asText));
  214. foreach ($this->ways_asText as $way_key => $way_asText) {
  215. $way = Vendor_Geophp::load($way_asText, 'wkt');
  216. $waysLengths[$way_key] = $way->greatCircleLength();
  217. if ($this->showProgress) $progress->step();
  218. }
  219. $this->waysLengths = $waysLengths;
  220. if ($this->showProgress) $progress->stop();
  221. } catch (Exception $e) {
  222. throw $e;
  223. }
  224. }
  225. public function generateWays() {
  226. try {
  227. if (!$this->getSourceData) $this->getSourceData();
  228. $ways_asText = $this->arrayFrom1();
  229. $ways2drogi = $this->arrayFrom1();
  230. $result = DB::getPDO()->fetchall("select ID as droga_id, astext(the_geom) as multiway from `_temp_drogi`");
  231. if ($this->showProgress) $progress = new showProgress("Generating ways", count($result));
  232. foreach ($result as $row) {
  233. $multiway = Vendor_Geophp::load($row['multiway'], 'wkt');
  234. for ($i = 1; $i < $n = $multiway->numPoints(); $i++) {
  235. $way = new LineString(array($multiway->pointN($i), $multiway->pointN($i+1)));
  236. $ways_asText[] = $way->asText();
  237. $ways2drogi[] = $row['droga_id'];
  238. $drogi2ways[$row['droga_id']][] = count($ways2drogi);
  239. }
  240. $drogi_asText[$row['droga_id']] = $multiway->asText();
  241. if ($this->showProgress) $progress->step();
  242. }
  243. if ($this->showProgress) $progress->stop();
  244. $this->ways_asText = $ways_asText;
  245. $this->ways2drogi = $ways2drogi;
  246. $this->drogi2ways = $drogi2ways;
  247. $this->drogi_asText = $drogi_asText;
  248. $this->joins_asText = array();
  249. $this->ways2joins = array();
  250. $this->joins2ways = array();
  251. $this->xpoints_asText = array();
  252. $this->points2xpoints = array();
  253. $this->xpoints2points = array();
  254. $this->readyForFindWay = false;
  255. $this->generateWaysLenghts();
  256. } catch (Exception $e) {
  257. throw $e;
  258. }
  259. }
  260. private function createTemporaryJoinsTable() {
  261. try {
  262. if (!$this->joins_asText) throw new Exception("createTemporaryJoinsTable() :: Joins not generated, use generateJoins() first");
  263. DB::getPDO()->query("drop temporary table if exists `_temp_joins`");
  264. DB::getPDO()->query("create temporary table `_temp_joins` (`ID` int(11), `the_geom` point not null, index(`ID`), spatial index(`the_geom`)) ENGINE=MyISAM");
  265. if ($this->showProgress) $progress = new showProgress("Generating temporary joins table", count($this->joins_asText));
  266. foreach ($this->joins_asText as $join_key => $join_asText) {
  267. DB::getPDO()->query("insert into `_temp_joins` values ('{$join_key}', PointFromText('{$join_asText}'))");
  268. if ($this->showProgress) $progress->step();
  269. }
  270. if ($this->showProgress) $progress->stop();
  271. $this->createTemporaryJoinsTable = true;
  272. } catch (Exception $e) {
  273. throw $e;
  274. }
  275. }
  276. public function generateJoins() {
  277. try {
  278. if (!$this->ways_asText) throw new Exception("generateJoins() :: Ways not generated, use generateWays() first");
  279. $joins_asText = $this->arrayFrom1();
  280. $ways2joins = array();
  281. $joins2ways = array();
  282. if ($this->showProgress) $progress = new showProgress("Generating joins", count($this->ways_asText));
  283. foreach ($this->ways_asText as $way_key => $way_asText) {
  284. $way = Vendor_Geophp::load($way_asText, 'wkt');
  285. $points_asText = array($way->startPoint()->asText(), $way->endPoint()->asText());
  286. foreach ($points_asText as $point_asText) {
  287. if (!($join_key=array_search($point_asText, $joins_asText))) $join_key = array_push($joins_asText, $point_asText);
  288. $ways2joins[$way_key][] = $join_key;
  289. $joins2ways[$join_key][] = $way_key;
  290. }
  291. if ($this->showProgress) $progress->step();
  292. }
  293. if ($this->showProgress) $progress->stop();
  294. $this->joins_asText = $joins_asText;
  295. $this->ways2joins = $ways2joins;
  296. $this->joins2ways = $joins2ways;
  297. $this->readyForFindWay = false;
  298. } catch (Exception $e) {
  299. throw $e;
  300. }
  301. }
  302. private function updateTemporaryPointsTable() {
  303. try {
  304. if ((!$this->points2xpoints) || (!$this->xpoints_asText)) throw new Exception("updateTemporaryPointsTable() :: Xpoints not generated, use generateXpoints() first");
  305. if (!$this->getSourceData) $this->getSourceData();
  306. if ($this->showProgress) $progress = new showProgress("Updating temporary points table", count($this->points2xpoints));
  307. foreach ($this->points2xpoints as $point_key => $xpoint_key) {
  308. DB::getPDO()->query("update `_temp_punkty_adresowe` set `the_geom_xpoint` = pointfromtext('{$this->xpoints_asText[$xpoint_key]}') where `ID` = '{$point_key}'");
  309. if ($this->showProgress) $progress->step();
  310. }
  311. if ($this->showProgress) $progress->stop();
  312. } catch (Exception $e) {
  313. throw $e;
  314. }
  315. }
  316. public function generateXpoints() {
  317. try {
  318. if (!$this->ways_asText) throw new Exception("generateXpoints() :: Ways not generated, use generateWays() first");
  319. if (!$this->getSourceData) $this->getSourceData();
  320. if (!$this->createTemporaryWaysTable) $this->createTemporaryWaysTable();
  321. $xpoints_asText = $this->arrayFrom1();
  322. $result = DB::getPDO()->fetchall("select `ID`, astext(`the_geom`) as `point_asText` from `_temp_punkty_adresowe`");
  323. if ($this->showProgress) $progress = new showProgress("Generating xpoints", count($result));
  324. foreach ($result as $row) {
  325. 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");
  326. $way = Vendor_Geophp::load($this->ways_asText[$way_key], 'wkt');
  327. $a = $way->startPoint();
  328. $b = $way->endPoint();
  329. $p = Vendor_Geophp::load($row['point_asText'], 'wkt');
  330. if ($a->y() == $b->y()) {
  331. $xp = $p->x();
  332. $yp = $a->y();
  333. $deltaA = $p->x() - $a->x();
  334. $deltaB = $p->x() - $b->x();
  335. } else {
  336. if ($a->x() == $b->x()) {
  337. $xp = $a->x();
  338. $yp = $p->y();
  339. $a2 = 0;
  340. $b2 = $p->y();
  341. } else {
  342. $a1 = ($a->y() - $b->y()) / ($a->x() - $b->x());
  343. $b1 = $a->y() - $a1 * $a->x();
  344. $a2 = -1 / $a1;
  345. $b2 = $p->y() - $a2 * $p->x();
  346. $xp = ($b2 - $b1) / ($a1 - $a2);
  347. $yp = $a1 * $xp + $b1;
  348. }
  349. $deltaA = $a2 * $a->x() + $b2 - $a->y();
  350. $deltaB = $a2 * $b->x() + $b2 - $b->y();
  351. }
  352. if ($deltaA * $deltaB >= 0) { // punkt poza linia, ustalamy na blizszy koniec linii
  353. if (abs($deltaA) < abs($deltaB) ) $xpoint = $a;
  354. else $xpoint = $b;
  355. } else { // punkt w linii - trzeba podzielic linie na dwie
  356. $xpoint = new Point($xp, $yp);
  357. $wayA = new LineString(array($a, $xpoint));
  358. $wayB = new LineString(array($xpoint, $b));
  359. $this->ways_asText[$way_key] = $wayA->asText();
  360. $new_way_key = array_push($this->ways_asText, $wayB->asText());
  361. $this->ways2drogi[$new_way_key] = $this->ways2drogi[$way_key];
  362. $this->drogi2ways[$this->ways2drogi[$way_key]][] = $new_way_key;
  363. DB::getPDO()->query("update `_temp_ways` set `the_geom` = linefromtext('{$wayA->asText()}') where `ID` = '{$way_key}'");
  364. DB::getPDO()->query("insert into `_temp_ways` values ('{$new_way_key}', '{$this->ways2drogi[$way_key]}', linefromtext('{$wayB->asText()}'))");
  365. }
  366. if (!($xpoint_key = array_search($xpoint->asText(), $xpoints_asText))) $xpoint_key = array_push($xpoints_asText, $xpoint->asText());
  367. $points2xpoints[$row['ID']] = $xpoint_key;
  368. $xpoints2points[$xpoint_key][] = $row['ID'];
  369. if ($this->showProgress) $progress->step();
  370. }
  371. if ($this->showProgress) $progress->stop();
  372. $this->xpoints_asText = $xpoints_asText;
  373. $this->points2xpoints = $points2xpoints;
  374. $this->xpoints2points = $xpoints2points;
  375. $this->generateJoins();
  376. $this->updateTemporaryPointsTable();
  377. $this->generateWaysLenghts();
  378. $this->readyForFindWay = false;
  379. } catch (Exception $e) {
  380. throw $e;
  381. }
  382. }
  383. private function createTemporaryPrzylaczaTable() {
  384. try {
  385. if ((!$this->przylacza_asText) || (!$this->przylacza2joins) || (!$this->przylacza2points)) throw new Exception("createTemporaryPrzylaczaTable() :: Przylacza not generated, use generatePrzylacza() first");
  386. if ($this->showProgress) $progress = new showProgress("Generating temporary table przylacza", count($this->przylacza_asText));
  387. DB::getPDO()->query("drop temporary table if exists `_temp_przylacza`");
  388. 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");
  389. foreach ($this->przylacza_asText as $przylacze_key => $przylacze_asText) {
  390. $query = "insert into `_temp_przylacza` values ('{$przylacze_key}', '{$this->przylacza2points[$przylacze_key]}', '{$this->przylacza2joins[$przylacze_key]}', linefromtext('{$przylacze_asText}'))";
  391. DB::getPDO()->query($query);
  392. if ($this->showProgress) $progress->step();
  393. }
  394. if ($this->showProgress) $progress->stop();
  395. $this->createTemporaryPrzylaczaTable = true;
  396. } catch (Exception $e) {
  397. throw $e;
  398. }
  399. }
  400. public function generatePrzylacza() {
  401. try {
  402. if ((!$this->points2xpoints) || (!$this->xpoints_asText)) throw new Exception("generatePrzylaczaTable() :: Xpoints not generated, use generateXpoints() first");
  403. if (!$this->joins_asText) throw new Exception("generatePrzylaczaTable() :: Joins not generated, use generateJoins() first");
  404. if (!$this->points_asText) $this->generatePoints();
  405. $przylacza_asText = $this->arrayFrom1();
  406. $przylacza2joins = $this->arrayFrom1();
  407. $przylacza2points = $this->arrayFrom1();
  408. if ($this->showProgress) $progress = new showProgress("Generating przylacza", count($this->points_asText));
  409. foreach ($this->points_asText as $point_key => $point_asText) {
  410. $xpoint_key = $this->points2xpoints[$point_key];
  411. $join_key = array_search($this->xpoints_asText[$xpoint_key], $this->joins_asText);
  412. $point = Vendor_Geophp::load($this->points_asText[$point_key], 'wkt');
  413. $xpoint = Vendor_Geophp::load($this->xpoints_asText[$xpoint_key], 'wkt');
  414. $przylacze = new Linestring(array($point, $xpoint));
  415. $przylacza_asText[] = $przylacze->asText();
  416. $przylacza2joins[] = $join_key;
  417. $przylacza2points[] = $point_key;
  418. $points2joins[$point_key] = $join_key;
  419. if ($this->showProgress) $progress->step();
  420. }
  421. if ($this->showProgress) $progress->stop();
  422. $this->przylacza_asText = $przylacza_asText;
  423. $this->przylacza2joins = $przylacza2joins;
  424. $this->przylacza2points = $przylacza2points;
  425. $this->points2joins = $points2joins;
  426. $this->readyForFindWay = false;
  427. } catch (Exception $e) {
  428. throw $e;
  429. }
  430. }
  431. private function checkDataForFindWay() {
  432. try {
  433. if (!$this->readyForFindWay) {
  434. if (!$this->ways_asText) {
  435. $this->generateWays();
  436. $this->generateXpoints();
  437. }
  438. if ((!$this->ways2joins) || (!$this->joins2ways)) $this->generateJoins();
  439. if (!$this->points_asText) $this->generatePoints();
  440. if (!$this->points2joins) $this->generatePrzylacza();
  441. $this->readyForFindWay = true;
  442. }
  443. } catch (Exception $e) {
  444. throw $e;
  445. }
  446. }
  447. public function findWay($joinA, $joinB, $path = array(), $joins = array()) {
  448. try {
  449. $this->checkDataForFindWay();
  450. if ($joinA == $joinB) {
  451. $joinsPath = array($joinB);
  452. foreach (array_reverse($path) as $way_key) {
  453. array_unshift($joinsPath, $this->ways2joins[$way_key][1 - array_search($joinsPath[0], $this->ways2joins[$way_key])]);
  454. }
  455. return $joinsPath;
  456. } else {
  457. $subPaths = array();
  458. foreach ($this->joins2ways[$joinA] as $way_key) {
  459. if (!in_array($way_key, $path)) {
  460. $new_join = $this->ways2joins[$way_key][1 - array_search($joinA, $this->ways2joins[$way_key])];
  461. if (!in_array($new_join, $joins)) {
  462. if ($subPath = $this->findWay($new_join, $joinB, array_merge($path, array($way_key)), array_merge($joins, array($new_join)))) {
  463. $subPaths[] = $subPath;
  464. }
  465. }
  466. }
  467. }
  468. if ($subPaths) {
  469. if (count($subPaths) == 1) return $subPaths[0];
  470. else {
  471. $shortestKey = 0;
  472. foreach ($subPaths as $key => $subPath) {
  473. $distance_cur = 0;
  474. foreach ($subPath as $way_key) {
  475. $way = Vendor_Geophp::load($this->ways_asText[$way_key], 'wkt');
  476. if ($way) $distance_cur += Vendor_Geophp::load($this->ways_asText[$way_key], 'wkt')->length();
  477. else $distance_cur += 999;
  478. }
  479. if (!$shortestKey || ($shortestKey && $distance_cur < $distance)) {
  480. $shortestKey = $key;
  481. $distance = $distance_cur;
  482. }
  483. }
  484. return $subPaths[$key];
  485. }
  486. }
  487. else {
  488. return null;
  489. }
  490. }
  491. } catch (Exception $e) {
  492. throw $e;
  493. }
  494. }
  495. function joins2lineString($joins_keys) {
  496. try {
  497. if (!$this->points_asText) $this->generatePoints();
  498. if ($joins_keys) {
  499. foreach ($joins_keys as $join_key) {
  500. $points[] = Vendor_Geophp::load($this->joins_asText[$join_key], 'wkt');
  501. }
  502. return new LineString($points);
  503. } else return null;
  504. } catch (Exception $e) {
  505. throw $e;
  506. }
  507. }
  508. function generateLongWays() {
  509. try {
  510. if ((!$this->drogi2ways) || (!$this->drogi_asText)) $this->generateWays();
  511. if ($this->showProgress) $progress = new showProgress("Generating longWays", count($this->drogi2ways));
  512. // print_r($this->drogi_asText);
  513. foreach ($this->drogi2ways as $droga_key => $way_keys) {
  514. $droga = Vendor_Geophp::load($this->drogi_asText[$droga_key], 'wkt');
  515. $startPoint = $droga->startPoint();
  516. $endPoint = $droga->endPoint();
  517. $join_key_start = array_search($startPoint->asText(), $this->joins_asText);
  518. $join_key_end = array_search($endPoint->asText(), $this->joins_asText);
  519. $path[] = $join_key_start;
  520. $last_join_key = $join_key_start;
  521. while (($next_join = 0) != $join_key_end) {
  522. foreach ($this->joins2ways[$last_join_key]
  523. }
  524. }
  525. } catch (Exception $e) {
  526. throw $e;
  527. }
  528. }
  529. }
  530. //$test = new bialePlamy(array("pointsTable"=>"Rozdzielcza_test2_bzyk_punkty_adresowe", "origWaysTable"=>"Rozdzielcza_test2_bzyk_drogi"));
  531. //$test->enableShowProgress();
  532. //$test->loadCache();
  533. //$test->generateWays();
  534. //$test->generateXpoints();
  535. //$test->generatePrzylacza();
  536. //$test->saveCache();
  537. //$test->saveXML();
  538. //$test->saveWaysTable("Rozdzielcza_test2_bzyk_ways");
  539. //$test->saveJoinsTable("Rozdzielcza_test2_bzyk_joins");
  540. //$test->savePrzylaczaTable("Rozdzielcza_test2_bzyk_przylacza");