Explorar el Código

Białe plamy opakowane w klasę

Mariusz Muszyński hace 9 años
padre
commit
bca06b7163
Se han modificado 1 ficheros con 459 adiciones y 0 borrados
  1. 459 0
      SE/dev/php-cli/biale_plamy_class.php

+ 459 - 0
SE/dev/php-cli/biale_plamy_class.php

@@ -0,0 +1,459 @@
+<?php
+
+$_SERVER['SERVER_NAME'] = 'biuro.biall-net.pl';
+
+//when used in /Library/Server/Web/Data/Sites/bzyk-biale-plamy-php-temp/SE/dev on biuro.biall-net.pl
+if( file_exists ( "../../se-lib/bootstrap.php"))
+require("../../se-lib/bootstrap.php");
+//when used in  in native SE
+else require("../../../../SE-production-git/SE/se-lib/bootstrap.php");
+
+Lib::loadClass("Vendor_Geophp");
+
+class showProgress {
+
+	private $startTime;
+	private $i = 0, $l = 0, $left = 0, $diff = 0, $n, $msg;
+	private $strlen = 40;
+
+	public function __construct($msg, $n = null) {
+		$this->msg = str_pad($msg, $this->strlen);
+		echo "{$this->msg}:   0%";
+		$this->n = $n;
+		list($usec, $sec) = explode(" ", microtime());
+		$this->startTime = $sec + $usec;
+	}
+
+	public function step($int = 1) {
+		if ($this->n) {
+			$this->i += $int;
+			$p = floor(100 * $this->i / $this->n);
+			list($usec, $sec) = explode(" ", microtime());
+			$curTime = $sec + $usec;
+			$diff = $curTime - $this->startTime;
+			$left = floor((($this->n / $this->i) - 1) * $diff);
+			$diff = floor($diff);
+			if ($p > $this->l || $left != $this->left || $diff != $this->diff) {
+				$this->l = $p;
+				$this->diff = $diff;
+				$this->left = $left;
+				if ($left > 59) $left = floor($left / 60) . "m " . ($left % 60);
+				if ($diff > 59) $diff = floor($diff / 60) . "m " . ($diff % 60);
+				echo "\r".str_pad("", $this->strlen+60);
+				echo "\r{$this->msg}: {$p}% (elapsed {$diff}s, estimated remaining {$left}s)";
+			}
+		}	
+	}
+
+	public function stop() {
+		list($usec, $sec) = explode(" ", microtime());
+		$curTime = $sec + $usec;
+		$diff = $curTime - $this->startTime;
+		$diff = floor($diff);
+		if ($diff > 59) $diff = floor($diff / 60) . "m " . ($diff % 60);
+		echo "\r".str_pad("", $this->strlen+60);
+		echo "\r{$this->msg}: OK (done in {$diff}s)\n";
+	}
+}
+
+
+class bialePlamy {
+
+	private $showProgress = false;
+
+	private $getSourceData = false, $createTemporaryWaysTable = false, $createTemporaryJoinsTable = false, $createTemporaryPrzylaczaTable = 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();
+
+	public function __construct($punktyAdresoweTable, $drogiTable) {
+		$this->punktyAdresoweTable = $punktyAdresoweTable;
+		$this->drogiTable = $drogiTable;
+	}
+
+	public function enableShowProgress() {
+		$this->showProgress = true;
+	}
+
+	public function disableShowProgress() {
+		$this->showProgress = false;
+	}
+
+	private $dataArrays = array('joins_asText', 'ways_asText', 'xpoints_asText', 'points_asText', 'przylacza_asText',
+		'ways2drogi', 'ways2joins', 'joins2ways', 'points2xpoints', 'xpoints2points', 'przylacza2joins', 'przylacza2points');
+
+	public function saveCache() {
+		foreach ($this->dataArrays as $array) if ($this->$array) {
+			if ($this->showProgress) $progress = new showProgress("Saving {$array}");
+			file_put_contents('/tmp/save_'.$array.'.txt', serialize($this->$array));
+			if ($this->showProgress) $progress->stop();
+		}
+	}
+
+	public function saveXML() {
+		function array_to_xml($data, &$xml_data) {
+			foreach ($data as $key => $value) {
+				$key_ = "";
+				if (is_numeric($key)) $key_ = 'item';
+				if (is_array($value)) {
+					$subnode = $xml_data->addChild($key_);
+					$subnode->addAttribute('id', $key);
+					array_to_xml($value, $subnode);
+				} else {
+					$subnode = $xml_data->addChild("$key_", htmlspecialchars("$value"));
+					if (is_numeric($key)) $subnode->addAttribute('id', $key);
+				}
+			}
+		}
+
+		foreach ($this->dataArrays as $array) if ($this->$array) {
+			if ($this->showProgress) $progress = new showProgress("Generating XML for {$array}");
+			$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><{$array}></{$array}>");
+			array_to_xml($this->$array, $xml);
+			$xml->asXML("{$array}.xml");
+			if ($this->showProgress) $progress->stop();
+		}
+	}
+
+	public function loadCache() {
+		try {
+			foreach ($this->dataArrays as $array) if (file_exists('/tmp/save_'.$array.'.txt')) {
+				if ($this->showProgress) $progress = new showProgress("Loading {$array}");
+				$this->$array = unserialize(file_get_contents('/tmp/save_'.$array.'.txt'));
+				if ($this->showProgress) $progress->stop();
+			}
+		} catch (Exception $e) {
+			throw new Exception ($e->getMessage());
+		}
+	}
+
+	private function saveDB($src, $dst) {
+		try {
+			if ($this->showProgress) $progress = new showProgress("Saving points table");
+			DB::getPDO()->query("truncate table `{$dst}`");
+			$result = DB::getPDO()->fetchall("show fields from `{$src}`");
+			foreach ($result as $col) $cols[] = "`{$col['Field']}`";
+			$query = "insert into `{$dst}` (".implode(", ", $cols).") select * from `{$src}`";
+			DB::getPDO()->query($query);
+			if ($this->showProgress) $progress->stop();
+		} catch (Exception $e) {
+			throw new Exception ($e->getMessage());
+		}
+	}
+
+	public function savePointsTable($dst) {
+		if (!$this->getSourceData) $this->getSourceData();
+		if ($this->xpoints2points) $this->updateTemporaryPointsTable();
+		$this->saveDB("_temp_punkty_adresowe", $dst);
+	}
+
+	public function saveWaysTable($dst) {
+		if (!$this->createTemporaryWaysTable) $this->createTemporaryWaysTable();
+		$this->saveDB("_temp_ways", $dst);
+	}
+
+	public function saveJoinsTable($dst) {
+		if (!$this->createTemporaryJoinsTable) $this->createTemporaryJoinsTable();
+		$this->saveDB("_temp_joins", $dst);
+	}
+
+	public function savePrzylaczaTable($dst) {
+		if (!$this->createTemporaryPrzylaczaTable) $this->createTemporaryPrzylaczaTable();
+		$this->saveDB("_temp_przylacza", $dst);
+	}
+
+	private function arrayFrom1() {
+		$array = array('');
+		unset($array[0]);
+		return $array;
+	}
+
+	private function pointsDistance($a, $b) {
+		$line = new LineString(array($a, $b));
+		return $line->length();		
+	}
+
+	private function getSourceData() {
+		try {
+			if ($this->showProgress) $progress = new showProgress("Loading source data from database");
+			DB::getPDO()->query("create temporary table `_temp_punkty_adresowe` select * from `{$this->punktyAdresoweTable}`");
+			DB::getPDO()->query("create temporary table `_temp_drogi` select * from `{$this->drogiTable}` where numpoints(the_geom)>1");
+			DB::getPDO()->query("create index `ID` on `_temp_punkty_adresowe` (`ID`)");
+			DB::getPDO()->query("create index `ID` on `_temp_drogi` (`ID`)");
+			$this->getSourceData = true;
+			if ($this->showProgress) $progress->stop();
+		} catch (Exception $e) {
+			throw new Exception($e->getMessage());
+		}
+	}
+
+	private function generatePoints() {
+		try {
+			if (!$this->getSourceData) $this->getSourceData();
+			if ($this->showProgress) $progress = new showProgress("Generating points");
+			$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;
+			if ($this->showProgress) $progress->stop();
+		} catch (Exception $e) {
+			throw new Exception($e->getMessage());
+		}
+	}
+	private function createTemporaryWaysTable() {
+		try {
+			if ((!$this->ways_asText) || (!$this->ways2drogi)) throw new Exception("createTemporaryWaysTable() :: Ways not generated, use generateWays() first");
+
+			DB::getPDO()->query("drop temporary table if exists `_temp_ways`");
+			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");
+			if ($this->showProgress) $progress = new showProgress("Creating temporary ways table", count($this->ways_asText));
+			foreach ($this->ways_asText as $way_key => $way_asText) {
+				DB::getPDO()->query("insert into `_temp_ways` values ('{$way_key}', '{$this->ways2drogi[$way_key]}', LineFromText('{$way_asText}'))");
+				if ($this->showProgress) $progress->step();
+			}
+			if ($this->showProgress) $progress->stop();
+
+			$this->createTemporaryWaysTable = true;
+		} catch (Exception $e) {
+			throw new Exception ($e->getMessage);
+		}
+	}
+
+	public function generateWays() {
+		try {
+			if (!$this->getSourceData) $this->getSourceData();
+
+			$ways_asText = $this->arrayFrom1();
+			$ways2drogi = $this->arrayFrom1();
+
+			$result = DB::getPDO()->fetchall("select ID as droga_id, astext(the_geom) as multiway from `_temp_drogi`");
+			if ($this->showProgress) $progress = new showProgress("Generating ways", count($result));
+			foreach ($result as $row) {
+				$multiway = Vendor_Geophp::load($row['multiway'], 'wkt');
+				for ($i = 1; $i < $n = $multiway->numPoints(); $i++) {
+					$way = new LineString(array($multiway->pointN($i), $multiway->pointN($i+1)));
+					$ways_asText[] = $way->asText();
+					$ways2drogi[] = $row['droga_id'];
+				}
+				if ($this->showProgress) $progress->step();
+			}
+			if ($this->showProgress) $progress->stop();
+
+			$this->ways_asText = $ways_asText;
+			$this->ways2drogi = $ways2drogi;
+
+			$this->joins_asText = array();
+			$this->ways2joins = array();
+			$this->joins2ways = array();
+			$this->xpoints_asText = array();
+			$this->points2xpoints = array();
+			$this->xpoints2points = array();
+		} catch (Exception $e) {
+			throw new Exception($e->getMessage());
+		}
+		
+	}
+
+	private function createTemporaryJoinsTable() {
+		try {
+			if (!$this->joins_asText) throw new Exception("createTemporaryJoinsTable() :: Joins not generated, use generateJoins() first");
+
+			DB::getPDO()->query("drop temporary table if exists `_temp_joins`");
+			DB::getPDO()->query("create temporary table `_temp_joins` (`ID` int(11), `the_geom` point not null, index(`ID`), spatial index(`the_geom`)) ENGINE=MyISAM");
+			if ($this->showProgress) $progress = new showProgress("Generating temporary joins table", count($this->joins_asText));
+			foreach ($this->joins_asText as $join_key => $join_asText) {
+				DB::getPDO()->query("insert into `_temp_joins` values ('{$join_key}', PointFromText('{$join_asText}'))");
+				if ($this->showProgress) $progress->step();
+			}
+			if ($this->showProgress) $progress->stop();
+			$this->createTemporaryJoinsTable = true;
+		} catch (Exception $e) {
+			throw new Exception ($e->getMessage());
+		}
+	}
+
+	public function generateJoins() {
+		try {
+			if (!$this->ways_asText) throw new Exception("generateJoins() :: Ways not generated, use generateWays() first");
+
+			$joins_asText = $this->arrayFrom1();
+			$ways2joins = array();
+			$joins2ways = array();
+
+			if ($this->showProgress) $progress = new showProgress("Generating joins", count($this->ways_asText));
+			foreach ($this->ways_asText as $way_key => $way_asText) {
+				$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 ($this->showProgress) $progress->step();
+			}
+			if ($this->showProgress) $progress->stop();
+
+			$this->joins_asText = $joins_asText;
+			$this->ways2joins = $ways2joins;
+			$this->joins2ways = $joins2ways;
+		} catch (Exception $e) {
+			throw new Exception($e->getMessage());
+		}
+	}
+
+	private function updateTemporaryPointsTable() {
+		try {
+			if ((!$this->points2xpoints) || (!$this->xpoints_asText)) throw new Exception("updateTemporaryPointsTable() :: Xpoints not generated, use generateXpoints() first");
+			if (!$this->getSourceData) $this->getSourceData();
+
+			if ($this->showProgress) $progress = new showProgress("Updating temporary points table", count($this->points2xpoints));
+			foreach ($this->points2xpoints as $point_key => $xpoint_key) {
+				DB::getPDO()->query("update `_temp_punkty_adresowe` set `the_geom_xpoint` = pointfromtext('{$this->xpoints_asText[$xpoint_key]}')  where `ID` = '{$point_key}'");
+				if ($this->showProgress) $progress->step();
+			}
+			if ($this->showProgress) $progress->stop();
+		} catch (Exception $e) {
+			throw new Exception($e->getMessage());
+		}
+	}
+
+	public function generateXpoints() {
+		try {
+			if (!$this->ways_asText) throw new Exception("generateXpoints() :: Ways not generated, use generateWays() first");
+			if (!$this->getSourceData) $this->getSourceData();
+			if (!$this->createTemporaryWaysTable) $this->createTemporaryWaysTable();
+
+			$xpoints_asText = $this->arrayFrom1();
+			$result = DB::getPDO()->fetchall("select `ID`, astext(`the_geom`) as `point_asText` from `_temp_punkty_adresowe`");
+			if ($this->showProgress) $progress = new showProgress("Generating xpoints", count($result));
+			foreach ($result as $row) {
+				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");
+				$way = Vendor_Geophp::load($this->ways_asText[$way_key], 'wkt');
+
+				$a = $way->startPoint();
+				$b = $way->endPoint();
+				$p = Vendor_Geophp::load($row['point_asText'], 'wkt');
+
+				if ($a->y() == $b->y()) {
+					$xp = $p->x();
+					$yp = $a->y();
+					$deltaA = $p->x() - $a->x();
+					$deltaB = $p->x() - $b->x();
+				} else {
+					if ($a->x() == $b->x()) {
+						$xp = $a->x();
+						$yp = $p->y();
+						$a2 = 0;
+						$b2 = $p->y();
+					} else {
+						$a1 = ($a->y() - $b->y()) / ($a->x() - $b->x());
+						$b1 = $a->y() - $a1 * $a->x();
+
+						$a2 = -1 / $a1;
+						$b2 = $p->y() - $a2 * $p->x();
+
+						$xp = ($b2 - $b1) / ($a1 - $a2);
+						$yp = $a1 * $xp + $b1;
+					}
+					$deltaA = $a2 * $a->x() + $b2 - $a->y();
+					$deltaB = $a2 * $b->x() + $b2 - $b->y();
+				}
+
+				if ($deltaA * $deltaB >= 0) { // punkt poza linia, ustalamy na blizszy koniec linii
+					if (abs($deltaA) < abs($deltaB) ) $xpoint = $xpoint = $a;
+					else $xpoint = $b;
+				} else { // punkt w linii - trzeba podzielic linie na dwie
+					$xpoint = new Point($xp, $yp);
+					$wayA = new LineString(array($a, $xpoint));
+					$wayB = new LineString(array($xpoint, $b));
+					$this->ways_asText[$way_key] = $wayA->asText();
+					$new_way_key = array_push($this->ways_asText, $wayB->asText());
+					$this->ways2drogi[$new_way_key] = $this->ways2drogi[$way_key];
+					DB::getPDO()->query("update `_temp_ways` set `the_geom` = linefromtext('{$wayA->asText()}') where `ID` = '{$way_key}'");
+					DB::getPDO()->query("insert into `_temp_ways` values ('{$new_way_key}', '{$this->ways2drogi[$way_key]}', linefromtext('{$wayB->asText()}'))");
+				}
+				if (!($xpoint_key = array_search($xpoint->asText(), $xpoints_asText))) $xpoint_key = array_push($xpoints_asText, $xpoint->asText());
+				$points2xpoints[$row['ID']] = $xpoint_key;
+				$xpoints2points[$xpoint_key][] = $row['ID'];
+				if ($this->showProgress) $progress->step();
+			}
+			if ($this->showProgress) $progress->stop();
+
+			$this->xpoints_asText = $xpoints_asText;
+			$this->points2xpoints = $points2xpoints;
+			$this->xpoints2points = $xpoints2points;
+			$this->generateJoins();
+			$this->updateTemporaryPointsTable();
+		} catch (Exception $e) {
+			throw new Exception($e->getMessage());
+		}
+	}
+
+	private function createTemporaryPrzylaczaTable() {
+		try {
+			if ((!$this->przylacza_asText) || (!$this->przylacza2joins) || (!$this->przylacza2points)) throw new Exception("createTemporaryPrzylaczaTable() :: Przylacza not generated, use generatePrzylacza() first");
+
+			if ($this->showProgress) $progress = new showProgress("Generating temporary table przylacza", count($this->przylacza_asText));
+			DB::getPDO()->query("drop temporary table if exists `_temp_przylacza`");
+			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");
+			foreach ($this->przylacza_asText as $przylacze_key => $przylacze_asText) {
+				$query = "insert into `_temp_przylacza` values ('{$przylacze_key}', '{$this->przylacza2points[$przylacze_key]}', '{$this->przylacza2joins[$przylacze_key]}', linefromtext('{$przylacze_asText}'))";
+				DB::getPDO()->query($query);
+				if ($this->showProgress) $progress->step();
+			}
+			if ($this->showProgress) $progress->stop();
+
+			$this->createTemporaryPrzylaczaTable = true;
+		} catch (Exception $e) {
+			throw new Exception($e->getMessage());
+		}
+	}
+
+	public function generatePrzylacza() {
+		try {
+			if ((!$this->points2xpoints) || (!$this->xpoints_asText)) throw new Exception("generatePrzylaczaTable() :: Xpoints not generated, use generateXpoints() first");
+			if (!$this->joins_asText) throw new Exception("generatePrzylaczaTable() :: Joins not generated, use generateJoins() first");
+			if (!$this->points_asText) $this->generatePoints();
+
+			$przylacza_asText = $this->arrayFrom1();
+			$przylacza2joins = $this->arrayFrom1();
+			$przylacza2points = $this->arrayFrom1();
+
+			if ($this->showProgress) $progress = new showProgress("Generating przylacza", count($this->points_asText));
+			foreach ($this->points_asText as $point_key => $point_asText) {
+				$xpoint_key = $this->points2xpoints[$point_key];
+				$join_key = array_search($this->xpoints_asText[$xpoint_key], $this->joins_asText);
+				$point = Vendor_Geophp::load($this->points_asText[$point_key], 'wkt');
+				$xpoint = Vendor_Geophp::load($this->xpoints_asText[$xpoint_key], 'wkt');
+				$przylacze = new Linestring(array($point, $xpoint));
+				$przylacza_asText[] = $przylacze->asText();
+				$przylacza2joins[] = $join_key;
+				$przylacza2points[] = $point_key;
+				if ($this->showProgress) $progress->step();
+			}
+			if ($this->showProgress) $progress->stop();
+
+			$this->przylacza_asText = $przylacza_asText;
+			$this->przylacza2joins = $przylacza2joins;
+			$this->przylacza2points = $przylacza2points;
+		} catch (Exception $e) {
+			throw new Exception($e->getMessage());
+		}
+	}
+
+}
+
+$test = new bialePlamy("Rozdzielcza_test2_bzyk_punkty_adresowe", "Rozdzielcza_test2_bzyk_drogi");
+$test->enableShowProgress();
+$test->loadCache();
+//$test->generateWays();
+//$test->generateXpoints();
+//$test->generatePrzylacza();
+//$test->saveCache();
+$test->saveXML();
+//$test->saveWaysTable("Rozdzielcza_test2_bzyk_ways");
+//$test->saveJoinsTable("Rozdzielcza_test2_bzyk_joins");
+//$test->savePrzylaczaTable("Rozdzielcza_test2_bzyk_przylacza");
+