biale_plamy_class.php 25 KB

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