biale_plamy_class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 $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(), $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('joins_asText', 'ways_asText', 'xpoints_asText', 'points_asText', 'przylacza_asText',
  75. 'ways2drogi', '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}`");
  172. DB::getPDO()->query("create temporary table `_temp_drogi` select * from `{$this->drogiTable}` where numpoints(the_geom)>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 generateWays() {
  211. try {
  212. if (!$this->getSourceData) $this->getSourceData();
  213. $ways_asText = $this->arrayFrom1();
  214. $ways2drogi = $this->arrayFrom1();
  215. $result = DB::getPDO()->fetchall("select ID as droga_id, astext(the_geom) as multiway from `_temp_drogi`");
  216. if ($this->showProgress) $progress = new showProgress("Generating ways", count($result));
  217. foreach ($result as $row) {
  218. $multiway = Vendor_Geophp::load($row['multiway'], 'wkt');
  219. for ($i = 1; $i < $n = $multiway->numPoints(); $i++) {
  220. $way = new LineString(array($multiway->pointN($i), $multiway->pointN($i+1)));
  221. $ways_asText[] = $way->asText();
  222. $ways2drogi[] = $row['droga_id'];
  223. }
  224. if ($this->showProgress) $progress->step();
  225. }
  226. if ($this->showProgress) $progress->stop();
  227. $this->ways_asText = $ways_asText;
  228. $this->ways2drogi = $ways2drogi;
  229. $this->joins_asText = array();
  230. $this->ways2joins = array();
  231. $this->joins2ways = array();
  232. $this->xpoints_asText = array();
  233. $this->points2xpoints = array();
  234. $this->xpoints2points = array();
  235. $this->readyForFindWay = false;
  236. } catch (Exception $e) {
  237. throw $e;
  238. }
  239. }
  240. private function createTemporaryJoinsTable() {
  241. try {
  242. if (!$this->joins_asText) throw new Exception("createTemporaryJoinsTable() :: Joins not generated, use generateJoins() first");
  243. DB::getPDO()->query("drop temporary table if exists `_temp_joins`");
  244. DB::getPDO()->query("create temporary table `_temp_joins` (`ID` int(11), `the_geom` point not null, index(`ID`), spatial index(`the_geom`)) ENGINE=MyISAM");
  245. if ($this->showProgress) $progress = new showProgress("Generating temporary joins table", count($this->joins_asText));
  246. foreach ($this->joins_asText as $join_key => $join_asText) {
  247. DB::getPDO()->query("insert into `_temp_joins` values ('{$join_key}', PointFromText('{$join_asText}'))");
  248. if ($this->showProgress) $progress->step();
  249. }
  250. if ($this->showProgress) $progress->stop();
  251. $this->createTemporaryJoinsTable = true;
  252. } catch (Exception $e) {
  253. throw $e;
  254. }
  255. }
  256. public function generateJoins() {
  257. try {
  258. if (!$this->ways_asText) throw new Exception("generateJoins() :: Ways not generated, use generateWays() first");
  259. $joins_asText = $this->arrayFrom1();
  260. $ways2joins = array();
  261. $joins2ways = array();
  262. if ($this->showProgress) $progress = new showProgress("Generating joins", count($this->ways_asText));
  263. foreach ($this->ways_asText as $way_key => $way_asText) {
  264. $way = Vendor_Geophp::load($way_asText, 'wkt');
  265. $points_asText = array($way->startPoint()->asText(), $way->endPoint()->asText());
  266. foreach ($points_asText as $point_asText) {
  267. if (!($join_key=array_search($point_asText, $joins_asText))) $join_key = array_push($joins_asText, $point_asText);
  268. $ways2joins[$way_key][] = $join_key;
  269. $joins2ways[$join_key][] = $way_key;
  270. }
  271. if ($this->showProgress) $progress->step();
  272. }
  273. if ($this->showProgress) $progress->stop();
  274. $this->joins_asText = $joins_asText;
  275. $this->ways2joins = $ways2joins;
  276. $this->joins2ways = $joins2ways;
  277. $this->readyForFindWay = false;
  278. } catch (Exception $e) {
  279. throw $e;
  280. }
  281. }
  282. private function updateTemporaryPointsTable() {
  283. try {
  284. if ((!$this->points2xpoints) || (!$this->xpoints_asText)) throw new Exception("updateTemporaryPointsTable() :: Xpoints not generated, use generateXpoints() first");
  285. if (!$this->getSourceData) $this->getSourceData();
  286. if ($this->showProgress) $progress = new showProgress("Updating temporary points table", count($this->points2xpoints));
  287. foreach ($this->points2xpoints as $point_key => $xpoint_key) {
  288. DB::getPDO()->query("update `_temp_punkty_adresowe` set `the_geom_xpoint` = pointfromtext('{$this->xpoints_asText[$xpoint_key]}') where `ID` = '{$point_key}'");
  289. if ($this->showProgress) $progress->step();
  290. }
  291. if ($this->showProgress) $progress->stop();
  292. } catch (Exception $e) {
  293. throw $e;
  294. }
  295. }
  296. public function generateXpoints() {
  297. try {
  298. if (!$this->ways_asText) throw new Exception("generateXpoints() :: Ways not generated, use generateWays() first");
  299. if (!$this->getSourceData) $this->getSourceData();
  300. if (!$this->createTemporaryWaysTable) $this->createTemporaryWaysTable();
  301. $xpoints_asText = $this->arrayFrom1();
  302. $result = DB::getPDO()->fetchall("select `ID`, astext(`the_geom`) as `point_asText` from `_temp_punkty_adresowe`");
  303. if ($this->showProgress) $progress = new showProgress("Generating xpoints", count($result));
  304. foreach ($result as $row) {
  305. 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");
  306. $way = Vendor_Geophp::load($this->ways_asText[$way_key], 'wkt');
  307. $a = $way->startPoint();
  308. $b = $way->endPoint();
  309. $p = Vendor_Geophp::load($row['point_asText'], 'wkt');
  310. if ($a->y() == $b->y()) {
  311. $xp = $p->x();
  312. $yp = $a->y();
  313. $deltaA = $p->x() - $a->x();
  314. $deltaB = $p->x() - $b->x();
  315. } else {
  316. if ($a->x() == $b->x()) {
  317. $xp = $a->x();
  318. $yp = $p->y();
  319. $a2 = 0;
  320. $b2 = $p->y();
  321. } else {
  322. $a1 = ($a->y() - $b->y()) / ($a->x() - $b->x());
  323. $b1 = $a->y() - $a1 * $a->x();
  324. $a2 = -1 / $a1;
  325. $b2 = $p->y() - $a2 * $p->x();
  326. $xp = ($b2 - $b1) / ($a1 - $a2);
  327. $yp = $a1 * $xp + $b1;
  328. }
  329. $deltaA = $a2 * $a->x() + $b2 - $a->y();
  330. $deltaB = $a2 * $b->x() + $b2 - $b->y();
  331. }
  332. if ($deltaA * $deltaB >= 0) { // punkt poza linia, ustalamy na blizszy koniec linii
  333. if (abs($deltaA) < abs($deltaB) ) $xpoint = $a;
  334. else $xpoint = $b;
  335. } else { // punkt w linii - trzeba podzielic linie na dwie
  336. $xpoint = new Point($xp, $yp);
  337. $wayA = new LineString(array($a, $xpoint));
  338. $wayB = new LineString(array($xpoint, $b));
  339. $this->ways_asText[$way_key] = $wayA->asText();
  340. $new_way_key = array_push($this->ways_asText, $wayB->asText());
  341. $this->ways2drogi[$new_way_key] = $this->ways2drogi[$way_key];
  342. DB::getPDO()->query("update `_temp_ways` set `the_geom` = linefromtext('{$wayA->asText()}') where `ID` = '{$way_key}'");
  343. DB::getPDO()->query("insert into `_temp_ways` values ('{$new_way_key}', '{$this->ways2drogi[$way_key]}', linefromtext('{$wayB->asText()}'))");
  344. }
  345. if (!($xpoint_key = array_search($xpoint->asText(), $xpoints_asText))) $xpoint_key = array_push($xpoints_asText, $xpoint->asText());
  346. $points2xpoints[$row['ID']] = $xpoint_key;
  347. $xpoints2points[$xpoint_key][] = $row['ID'];
  348. if ($this->showProgress) $progress->step();
  349. }
  350. if ($this->showProgress) $progress->stop();
  351. $this->xpoints_asText = $xpoints_asText;
  352. $this->points2xpoints = $points2xpoints;
  353. $this->xpoints2points = $xpoints2points;
  354. $this->generateJoins();
  355. $this->updateTemporaryPointsTable();
  356. $this->readyForFindWay = false;
  357. } catch (Exception $e) {
  358. throw $e;
  359. }
  360. }
  361. private function createTemporaryPrzylaczaTable() {
  362. try {
  363. if ((!$this->przylacza_asText) || (!$this->przylacza2joins) || (!$this->przylacza2points)) throw new Exception("createTemporaryPrzylaczaTable() :: Przylacza not generated, use generatePrzylacza() first");
  364. if ($this->showProgress) $progress = new showProgress("Generating temporary table przylacza", count($this->przylacza_asText));
  365. DB::getPDO()->query("drop temporary table if exists `_temp_przylacza`");
  366. 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");
  367. foreach ($this->przylacza_asText as $przylacze_key => $przylacze_asText) {
  368. $query = "insert into `_temp_przylacza` values ('{$przylacze_key}', '{$this->przylacza2points[$przylacze_key]}', '{$this->przylacza2joins[$przylacze_key]}', linefromtext('{$przylacze_asText}'))";
  369. DB::getPDO()->query($query);
  370. if ($this->showProgress) $progress->step();
  371. }
  372. if ($this->showProgress) $progress->stop();
  373. $this->createTemporaryPrzylaczaTable = true;
  374. } catch (Exception $e) {
  375. throw $e;
  376. }
  377. }
  378. public function generatePrzylacza() {
  379. try {
  380. if ((!$this->points2xpoints) || (!$this->xpoints_asText)) throw new Exception("generatePrzylaczaTable() :: Xpoints not generated, use generateXpoints() first");
  381. if (!$this->joins_asText) throw new Exception("generatePrzylaczaTable() :: Joins not generated, use generateJoins() first");
  382. if (!$this->points_asText) $this->generatePoints();
  383. $przylacza_asText = $this->arrayFrom1();
  384. $przylacza2joins = $this->arrayFrom1();
  385. $przylacza2points = $this->arrayFrom1();
  386. if ($this->showProgress) $progress = new showProgress("Generating przylacza", count($this->points_asText));
  387. foreach ($this->points_asText as $point_key => $point_asText) {
  388. $xpoint_key = $this->points2xpoints[$point_key];
  389. $join_key = array_search($this->xpoints_asText[$xpoint_key], $this->joins_asText);
  390. $point = Vendor_Geophp::load($this->points_asText[$point_key], 'wkt');
  391. $xpoint = Vendor_Geophp::load($this->xpoints_asText[$xpoint_key], 'wkt');
  392. $przylacze = new Linestring(array($point, $xpoint));
  393. $przylacza_asText[] = $przylacze->asText();
  394. $przylacza2joins[] = $join_key;
  395. $przylacza2points[] = $point_key;
  396. $points2joins[$point_key] = $join_key;
  397. if ($this->showProgress) $progress->step();
  398. }
  399. if ($this->showProgress) $progress->stop();
  400. $this->przylacza_asText = $przylacza_asText;
  401. $this->przylacza2joins = $przylacza2joins;
  402. $this->przylacza2points = $przylacza2points;
  403. $this->points2joins = $points2joins;
  404. $this->readyForFindWay = false;
  405. } catch (Exception $e) {
  406. throw $e;
  407. }
  408. }
  409. private function checkDataForFindWay() {
  410. try {
  411. if (!$this->readyForFindWay) {
  412. if (!$this->ways_asText) {
  413. $this->generateWays();
  414. $this->generateXpoints();
  415. }
  416. if ((!$this->ways2joins) || (!$this->joins2ways)) $this->generateJoins();
  417. if (!$this->points_asText) $this->generatePoints();
  418. if (!$this->points2joins) $this->generatePrzylacza();
  419. $this->readyForFindWay = true;
  420. }
  421. } catch (Exception $e) {
  422. throw $e;
  423. }
  424. }
  425. public function findWay($joinA, $joinB, $path = array()) {
  426. try {
  427. $this->checkDataForFindWay();
  428. if ($joinA == $joinB) {
  429. $joinsPath = array($joinB);
  430. foreach (array_reverse($path) as $way_key) {
  431. array_unshift($joinsPath, $this->ways2joins[$way_key][1 - array_search($joinsPath[0], $this->ways2joins[$way_key])]);
  432. }
  433. return $joinsPath;
  434. } else {
  435. $subPaths = array();
  436. foreach ($this->joins2ways[$joinA] as $way_key) {
  437. if (!in_array($way_key, $path)) {
  438. $new_join = $this->ways2joins[$way_key][1 - array_search($joinA, $this->ways2joins[$way_key])];
  439. if ($subPath = $this->findWay($new_join, $joinB, array_merge($path, array($way_key)))) $subPaths[] = $subPath;
  440. }
  441. }
  442. if ($subPaths) {
  443. if (count($subPaths) == 1) return $subPaths[0];
  444. else {
  445. $shortestKey = 0;
  446. foreach ($subPaths as $key => $subPath) {
  447. $distance_cur = 0;
  448. foreach ($subPath as $way_key) {
  449. $way = Vendor_Geophp::load($this->ways_asText[$way_key], 'wkt');
  450. if ($way) $distance_cur += Vendor_Geophp::load($this->ways_asText[$way_key], 'wkt')->length();
  451. else $distance_cur += 999;
  452. }
  453. if (!$shortestKey || ($shortestKey && $distance_cur < $distance)) {
  454. $shortestKey = $key;
  455. $distance = $distance_cur;
  456. }
  457. }
  458. return $subPaths[$key];
  459. }
  460. }
  461. else {
  462. return null;
  463. }
  464. }
  465. } catch (Exception $e) {
  466. throw $e;
  467. }
  468. }
  469. function joins2lineString($joins_keys) {
  470. try {
  471. if (!$this->points_asText) $this->generatePoints();
  472. if ($joins_keys) {
  473. foreach ($joins_keys as $join_key) {
  474. $points[] = Vendor_Geophp::load($this->joins_asText[$join_key], 'wkt');
  475. }
  476. return new LineString($points);
  477. } else return null;
  478. } catch (Exception $e) {
  479. throw $e;
  480. }
  481. }
  482. }
  483. //$test = new bialePlamy(array("pointsTable"=>"Rozdzielcza_test2_bzyk_punkty_adresowe", "origWaysTable"=>"Rozdzielcza_test2_bzyk_drogi"));
  484. //$test->enableShowProgress();
  485. //$test->loadCache();
  486. //$test->generateWays();
  487. //$test->generateXpoints();
  488. //$test->generatePrzylacza();
  489. //$test->saveCache();
  490. //$test->saveXML();
  491. //$test->saveWaysTable("Rozdzielcza_test2_bzyk_ways");
  492. //$test->saveJoinsTable("Rozdzielcza_test2_bzyk_joins");
  493. //$test->savePrzylaczaTable("Rozdzielcza_test2_bzyk_przylacza");