Mariusz Muszyński 9 سال پیش
والد
کامیت
a87607ec3e
3فایلهای تغییر یافته به همراه134 افزوده شده و 23 حذف شده
  1. 115 20
      SE/dev/php-cli/biale_plamy_class.php
  2. 13 0
      SE/dev/php-cli/biale_plamy_class_findWay.php
  3. 6 3
      SE/dev/php-cli/biale_plamy_class_usage.php

+ 115 - 20
SE/dev/php-cli/biale_plamy_class.php

@@ -61,11 +61,11 @@ class bialePlamy {
 
 	private $showProgress = false;
 
-	private $getSourceData = false, $createTemporaryWaysTable = false, $createTemporaryJoinsTable = false, $createTemporaryPrzylaczaTable = false;
+	private $getSourceData = false, $createTemporaryWaysTable = false, $createTemporaryJoinsTable = false, $createTemporaryPrzylaczaTable = false, $readyForFindWay = false;
 
 	private $punktyAdresoweTable = null, $drogiTable = null;
 	private $joins_asText = array(), $ways_asText = array(), $xpoints_asText = array(), $points_asText = array(), $przylacza_asText = array();
-	private $ways2drogi = array(), $ways2joins = array(), $joins2ways = array(), $points2xpoints = array(), $xpoints2points = array(), $przylacza2joins = array(), $przylacza2points = array();
+	private $ways2drogi = array(), $ways2joins = array(), $joins2ways = array(), $points2xpoints = array(), $xpoints2points = array(), $przylacza2joins = array(), $przylacza2points = array(), $points2joins = array();
 
 	public function __construct($sourceTables) {
 		$tables = array('pointsTable' => 'punktyAdresoweTable', 'origWaysTable' => 'drogiTable');
@@ -89,7 +89,7 @@ class bialePlamy {
 	}
 
 	private $dataArrays = array('joins_asText', 'ways_asText', 'xpoints_asText', 'points_asText', 'przylacza_asText',
-		'ways2drogi', 'ways2joins', 'joins2ways', 'points2xpoints', 'xpoints2points', 'przylacza2joins', 'przylacza2points');
+		'ways2drogi', 'ways2joins', 'joins2ways', 'points2xpoints', 'xpoints2points', 'przylacza2joins', 'przylacza2points', 'points2joins');
 
 	public function saveCache() {
 		foreach ($this->dataArrays as $array) if ($this->$array) {
@@ -124,6 +124,20 @@ class bialePlamy {
 		}
 	}
 
+	public function printArray($array) {
+		if (isset($this->$array) && is_array($this->$array)) {
+			return print_r($this->$array, true);
+		} else return null;
+	}
+
+	public function saveArrays() {
+		foreach ($this->dataArrays as $array) if ($this->$array) {
+			if ($this->showProgress) $progress = new showProgress("Saving array {$array} to file");
+			file_put_contents("/tmp/array_{$array}.txt", $this->printArray($array));
+			if ($this->showProgress) $progress->stop();
+		}
+	}
+
 	public function loadCache() {
 		try {
 			foreach ($this->dataArrays as $array) if (file_exists('/tmp/save_'.$array.'.txt')) {
@@ -132,7 +146,7 @@ class bialePlamy {
 				if ($this->showProgress) $progress->stop();
 			}
 		} catch (Exception $e) {
-			throw new Exception ($e->getMessage());
+			throw $e;
 		}
 	}
 
@@ -146,7 +160,7 @@ class bialePlamy {
 			DB::getPDO()->query($query);
 			if ($this->showProgress) $progress->stop();
 		} catch (Exception $e) {
-			throw new Exception ($e->getMessage());
+			throw $e;
 		}
 	}
 
@@ -192,7 +206,7 @@ class bialePlamy {
 			$this->getSourceData = true;
 			if ($this->showProgress) $progress->stop();
 		} catch (Exception $e) {
-			throw new Exception($e->getMessage());
+			throw $e;
 		}
 	}
 
@@ -203,9 +217,10 @@ class bialePlamy {
 			$result = DB::getPDO()->query("select `ID`, astext(`the_geom`) as `point_asText` from `_temp_punkty_adresowe`");
 			foreach ($result as $row) $points_asText[$row['ID']] = $row['point_asText'];
 			$this->points_asText = $points_asText;
+			$this->readyForFindWay = false;
 			if ($this->showProgress) $progress->stop();
 		} catch (Exception $e) {
-			throw new Exception($e->getMessage());
+			throw $e;
 		}
 	}
 	private function createTemporaryWaysTable() {
@@ -223,7 +238,7 @@ class bialePlamy {
 
 			$this->createTemporaryWaysTable = true;
 		} catch (Exception $e) {
-			throw new Exception ($e->getMessage);
+			throw $e;
 		}
 	}
 
@@ -256,8 +271,9 @@ class bialePlamy {
 			$this->xpoints_asText = array();
 			$this->points2xpoints = array();
 			$this->xpoints2points = array();
+			$this->readyForFindWay = false;
 		} catch (Exception $e) {
-			throw new Exception($e->getMessage());
+			throw $e;
 		}
 		
 	}
@@ -276,7 +292,7 @@ class bialePlamy {
 			if ($this->showProgress) $progress->stop();
 			$this->createTemporaryJoinsTable = true;
 		} catch (Exception $e) {
-			throw new Exception ($e->getMessage());
+			throw $e;
 		}
 	}
 
@@ -293,11 +309,9 @@ class bialePlamy {
 				$way = Vendor_Geophp::load($way_asText, 'wkt');
 				$points_asText = array($way->startPoint()->asText(), $way->endPoint()->asText());
 				foreach ($points_asText as $point_asText) {
-					if (!in_array($point_asText, $joins_asText)) {
-						$join_key = array_push($joins_asText, $point_asText);
-						$ways2joins[$way_key][] = $join_key;
-						$joins2ways[$join_key][] = $way_key;						
-					}
+					if (!($join_key=array_search($point_asText, $joins_asText))) $join_key = array_push($joins_asText, $point_asText);
+					$ways2joins[$way_key][] = $join_key;
+					$joins2ways[$join_key][] = $way_key;						
 				}
 				if ($this->showProgress) $progress->step();
 			}
@@ -306,8 +320,9 @@ class bialePlamy {
 			$this->joins_asText = $joins_asText;
 			$this->ways2joins = $ways2joins;
 			$this->joins2ways = $joins2ways;
+			$this->readyForFindWay = false;
 		} catch (Exception $e) {
-			throw new Exception($e->getMessage());
+			throw $e;
 		}
 	}
 
@@ -323,7 +338,7 @@ class bialePlamy {
 			}
 			if ($this->showProgress) $progress->stop();
 		} catch (Exception $e) {
-			throw new Exception($e->getMessage());
+			throw $e;
 		}
 	}
 
@@ -394,8 +409,9 @@ class bialePlamy {
 			$this->xpoints2points = $xpoints2points;
 			$this->generateJoins();
 			$this->updateTemporaryPointsTable();
+			$this->readyForFindWay = false;
 		} catch (Exception $e) {
-			throw new Exception($e->getMessage());
+			throw $e;
 		}
 	}
 
@@ -415,7 +431,7 @@ class bialePlamy {
 
 			$this->createTemporaryPrzylaczaTable = true;
 		} catch (Exception $e) {
-			throw new Exception($e->getMessage());
+			throw $e;
 		}
 	}
 
@@ -439,6 +455,7 @@ class bialePlamy {
 				$przylacza_asText[] = $przylacze->asText();
 				$przylacza2joins[] = $join_key;
 				$przylacza2points[] = $point_key;
+				$points2joins[$point_key] = $join_key;
 				if ($this->showProgress) $progress->step();
 			}
 			if ($this->showProgress) $progress->stop();
@@ -446,8 +463,86 @@ class bialePlamy {
 			$this->przylacza_asText = $przylacza_asText;
 			$this->przylacza2joins = $przylacza2joins;
 			$this->przylacza2points = $przylacza2points;
+			$this->points2joins = $points2joins;
+			$this->readyForFindWay = false;
 		} catch (Exception $e) {
-			throw new Exception($e->getMessage());
+			throw $e;
+		}
+	}
+
+	private function checkDataForFindWay() {
+		try {
+			if (!$this->readyForFindWay) {
+				if (!$this->ways_asText) {
+					$this->generateWays();
+					$this->generateXpoints();
+				}
+				if ((!$this->ways2joins) || (!$this->joins2ways)) $this->generateJoins();
+				if (!$this->points_asText) $this->generatePoints();
+				if (!$this->points2joins) $this->generatePrzylacza();
+				$this->readyForFindWay = true;
+			}
+		} catch (Exception $e) {
+			throw $e;
+		}
+	}
+
+	public function findWay($joinA, $joinB, $path = array()) {
+		try {
+			$this->checkDataForFindWay();
+			if ($joinA == $joinB) {
+				$joinsPath = array($joinB);
+				foreach (array_reverse($path) as $way_key) {
+					array_unshift($joinsPath, $this->ways2joins[$way_key][1 - array_search($joinsPath[0], $this->ways2joins[$way_key])]);
+				}
+				return $joinsPath;
+			} else {
+				$subPaths = array();
+				foreach ($this->joins2ways[$joinA] as $way_key) {
+					if (!in_array($way_key, $path)) {
+						$new_join = $this->ways2joins[$way_key][1 - array_search($joinA,  $this->ways2joins[$way_key])];
+						if ($subPath = $this->findWay($new_join, $joinB, array_merge($path, array($way_key)))) $subPaths[] = $subPath;
+					}
+				}
+				if ($subPaths) {
+					if (count($subPaths) == 1) return $subPaths[0];
+					else {
+						$shortestKey = 0;
+						foreach ($subPaths as $key => $subPath) {
+							$distance_cur = 0;
+							foreach ($subPath as $way_key) {
+								$way = Vendor_Geophp::load($this->ways_asText[$way_key], 'wkt');
+								if ($way) $distance_cur += Vendor_Geophp::load($this->ways_asText[$way_key], 'wkt')->length();
+								else $distance_cur += 999;
+							}
+							if (!$shortestKey || ($shortestKey && $distance_cur < $distance)) {
+								$shortestKey = $key;
+								$distance = $distance_cur;
+							}
+						}
+						return $subPaths[$key];
+					}
+				}
+				else {
+					return null;
+				}
+			}
+		} catch (Exception $e) {
+			throw $e;
+		}
+	}
+
+	function joins2lineString($joins_keys) {
+		try {
+			if (!$this->points_asText) $this->generatePoints();
+			if ($joins_keys) {
+				foreach ($joins_keys as $join_key) {
+					$points[] = Vendor_Geophp::load($this->joins_asText[$join_key], 'wkt');
+				}
+				return new LineString($points);
+			} else return null;
+		} catch (Exception $e) {
+			throw $e;
 		}
 	}
 

+ 13 - 0
SE/dev/php-cli/biale_plamy_class_findWay.php

@@ -0,0 +1,13 @@
+<?php
+
+require("biale_plamy_class.php");
+
+
+	$test = new bialePlamy(array("pointsTable"=>"Rozdzielcza_test2_bzyk_punkty_adresowe", "origWaysTable"=>"Rozdzielcza_test2_bzyk_drogi"));
+	//$test->enableShowProgress();
+	$test->loadCache();
+	$joins = $test->findWay(42366, 42376);
+	$lineStringObj = $test->joins2lineString($joins);
+	$lineString_asText = $lineStringObj->asText();
+	echo $lineString_asText;
+

+ 6 - 3
SE/dev/php-cli/biale_plamy_class_usage.php

@@ -5,11 +5,14 @@ require("biale_plamy_class.php");
 $test = new bialePlamy(array("pointsTable"=>"Rozdzielcza_test2_bzyk_punkty_adresowe", "origWaysTable"=>"Rozdzielcza_test2_bzyk_drogi"));
 $test->enableShowProgress();
 $test->loadCache();
-$test->generateWays();
+//$test->generateWays();
 //$test->generateXpoints();
+$test->generateJoins();
 //$test->generatePrzylacza();
-//$test->saveCache();
-//$test->saveXML();
+$test->saveCache();
+$test->saveXML();
+$test->saveArrays();
+echo $test->printArray('joins2ways');
 //$test->saveWaysTable("Rozdzielcza_test2_bzyk_ways");
 //$test->saveJoinsTable("Rozdzielcza_test2_bzyk_joins");
 //$test->savePrzylaczaTable("Rozdzielcza_test2_bzyk_przylacza");