Przeglądaj źródła

Białe plamy - funkcja szukająca drogi

Mariusz Muszyński 9 lat temu
rodzic
commit
11adabf758

+ 121 - 0
SE/dev/php-cli/biale_plamy-find_ways.php

@@ -0,0 +1,121 @@
+<?php
+$_SERVER['SERVER_NAME'] = 'biuro.biall-net.pl';
+require("../../se-lib/bootstrap.php");
+Lib::loadClass("Vendor_Geophp");
+
+
+
+function init_sh_check() {
+    //check if run from bash
+    if(isset($_SERVER["argv"])) {
+        if(@$_SERVER["argv"][1]=='--help') {
+            echo "
+                Options are:
+                php ".$_SERVER["argv"][0]." --check   - control if we have needed tables/data
+                
+                .EOF
+            ";
+        } else if(@$_SERVER["argv"][1]=='--check') { 
+            check();
+        } else echo " bad option, try:
+            php ".$_SERVER["argv"][0]." --help
+            
+            .EOF
+            ";
+    } else {
+        echo "<br> this script could be run from bash e.g. php biale_plamy.php --help <br>" ;
+        check();
+    }
+}
+
+
+
+function check() {
+	echo "<br>\n i run check()";
+    //todo to set arguments to rebuild tables etc:
+    $check_tbls=array('Rozdzielcza_test_bzyk_drogi','Rozdzielcza_test_bzyk_joins','Rozdzielcza_test_bzyk_przylacza_HIST',
+                    'Rozdzielcza_test_bzyk_przylacza','Rozdzielcza_test_bzyk_punkty_adresowe','Rozdzielcza_test_bzyk_ways');
+
+    //$tables =  DB::getPDO()->list_tables();
+    foreach($check_tbls as $tbl) {
+        $query = "select ID from ".$tbl." limit 1 ";
+        echo "<br>\nresult chk for tbl:".$tbl." ;";
+        
+        try{
+            $result = DB::getPDO()->fetchall($query);
+        } catch(Exception $exception) {
+            // return $exception;
+            echo "<br>\n jest blad z tbl:".$tbl." i get sample table structure with data from ../sql/".$tbl.".sql  - it should be manually replaced by correct/newer!";
+            $sql = file_get_contents('../sql/'.$tbl.'.sql');
+            
+            try{
+                $result = DB::getPDO()->fetchall($sql);
+            } catch(Exception $exception2) {
+                echo "<br>\n jest blad dump dla tbl:".$tbl;
+            }
+        } 
+        //	print_r($exception);
+        print_r($result);
+    }
+}
+
+init_sh_check();   
+
+function pointsDistance($a, $b) {
+	$line = new LineString(array($a, $b));
+	return $line->length();
+}
+
+$ways2joins = unserialize(file_get_contents('ways2joins.txt'));
+$joins2ways = unserialize(file_get_contents('joins2ways.txt'));
+$points2joins = unserialize(file_get_contents('points2joins.txt'));
+$joins_asText = unserialize(file_get_contents('joins_asText.txt'));
+$ways_asText = unserialize(file_get_contents('ways_asText.txt'));
+$points_asText = unserialize(file_get_contents('points_asText.txt'));
+
+
+
+function findWay($joinA, $joinB, $path = array()) {
+	global $ways2joins, $joins2ways, $points2joins, $ways_asText, $points_asText, $res;
+//echo implode('->',$path)."\n\n";
+	if ($joinA == $joinB) return $path;
+	else {
+		$subPaths = array();
+		foreach ($joins2ways[$joinA] as $way_key) {
+			if (!in_array($way_key, $path)) {
+//				$joins = $ways2joins[$way_key];
+//				$back_join_subkey = array_search($join_key, $joins);
+//				$new_join_subkey = 1 - $back_join_subkey;
+//				$new_join = $joins[$new_join_subkey];
+				$new_join = $ways2joins[$way_key][1 - array_search($joinA,  $ways2joins[$way_key])];
+				if ($subPath = 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) {
+						$distance_cur += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->length();
+					}
+					if (!$shortestKey || ($shortestKey && $distance_cur < $distance)) {
+						$shortestKey = $key;
+						$distance = $distance_cur;
+					}
+				}
+				return $subPaths[$key];
+			}
+		}
+		else return null;
+	}
+}
+
+$res = findWay(100,110);
+$distance = 0;
+foreach ($res as $way_key) {
+	$distance += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->greatCircleLength();
+}
+print_r($res);
+echo "Distance: ".$distance."m\n";

+ 97 - 0
SE/dev/php-cli/biale_plamy-find_ways_old.php

@@ -0,0 +1,97 @@
+<?php
+$_SERVER['SERVER_NAME'] = 'biuro.biall-net.pl';
+require("../../se-lib/bootstrap.php");
+Lib::loadClass("Vendor_Geophp");
+
+
+
+function init_sh_check() {
+    //check if run from bash
+    if(isset($_SERVER["argv"])) {
+        if(@$_SERVER["argv"][1]=='--help') {
+            echo "
+                Options are:
+                php ".$_SERVER["argv"][0]." --check   - control if we have needed tables/data
+                
+                .EOF
+            ";
+        } else if(@$_SERVER["argv"][1]=='--check') { 
+            check();
+        } else echo " bad option, try:
+            php ".$_SERVER["argv"][0]." --help
+            
+            .EOF
+            ";
+    } else {
+        echo "<br> this script could be run from bash e.g. php biale_plamy.php --help <br>" ;
+        check();
+    }
+}
+
+
+
+function check() {
+	echo "<br>\n i run check()";
+    //todo to set arguments to rebuild tables etc:
+    $check_tbls=array('Rozdzielcza_test_bzyk_drogi','Rozdzielcza_test_bzyk_joins','Rozdzielcza_test_bzyk_przylacza_HIST',
+                    'Rozdzielcza_test_bzyk_przylacza','Rozdzielcza_test_bzyk_punkty_adresowe','Rozdzielcza_test_bzyk_ways');
+
+    //$tables =  DB::getPDO()->list_tables();
+    foreach($check_tbls as $tbl) {
+        $query = "select ID from ".$tbl." limit 1 ";
+        echo "<br>\nresult chk for tbl:".$tbl." ;";
+        
+        try{
+            $result = DB::getPDO()->fetchall($query);
+        } catch(Exception $exception) {
+            // return $exception;
+            echo "<br>\n jest blad z tbl:".$tbl." i get sample table structure with data from ../sql/".$tbl.".sql  - it should be manually replaced by correct/newer!";
+            $sql = file_get_contents('../sql/'.$tbl.'.sql');
+            
+            try{
+                $result = DB::getPDO()->fetchall($sql);
+            } catch(Exception $exception2) {
+                echo "<br>\n jest blad dump dla tbl:".$tbl;
+            }
+        } 
+        //	print_r($exception);
+        print_r($result);
+    }
+}
+
+init_sh_check();   
+
+function pointsDistance($a, $b) {
+	$line = new LineString(array($a, $b));
+	return $line->length();
+}
+
+$ways2joins = unserialize(file_get_contents('ways2joins.txt'));
+$joins2ways = unserialize(file_get_contents('joins2ways.txt'));
+$points2joins = unserialize(file_get_contents('points2joins.txt'));
+$joins_asText = unserialize(file_get_contents('joins_asText.txt'));
+$ways_asText = unserialize(file_get_contents('ways_asText.txt'));
+$points_asText = unserialize(file_get_contents('points_asText.txt'));
+
+
+
+function findWay($joinA, $joinB, $path = array()) {
+	global $ways2joins, $joins2ways, $points2joins, $ways_asText, $points_asText, $res;
+//echo implode('->',$path)."\n\n";
+	if ($joinA == $joinB) $res[] = $path;
+	else {
+		foreach ($joins2ways[$joinA] as $way_key) {
+			if (!in_array($way_key, $path)) {
+//				$joins = $ways2joins[$way_key];
+//				$back_join_subkey = array_search($join_key, $joins);
+//				$new_join_subkey = 1 - $back_join_subkey;
+//				$new_join = $joins[$new_join_subkey];
+				$new_join = $ways2joins[$way_key][1 - array_search($joinA,  $ways2joins[$way_key])];
+				findWay($new_join, $joinB, array_merge($path,array($way_key)));
+			}
+		}
+	}
+}
+
+findWay(100,110);
+print_r($res);

+ 199 - 0
SE/dev/php-cli/biale_plamy-generate_data.php

@@ -0,0 +1,199 @@
+<?php
+$_SERVER['SERVER_NAME'] = 'biuro.biall-net.pl';
+require("../../se-lib/bootstrap.php");
+Lib::loadClass("Vendor_Geophp");
+
+
+
+function init_sh_check() {
+    //check if run from bash
+    if(isset($_SERVER["argv"])) {
+        if(@$_SERVER["argv"][1]=='--help') {
+            echo "
+                Options are:
+                php ".$_SERVER["argv"][0]." --check   - control if we have needed tables/data
+                
+                .EOF
+            ";
+        } else if(@$_SERVER["argv"][1]=='--check') { 
+            check();
+        } else echo " bad option, try:
+            php ".$_SERVER["argv"][0]." --help
+            
+            .EOF
+            ";
+    } else {
+        echo "<br> this script could be run from bash e.g. php biale_plamy.php --help <br>" ;
+        check();
+    }
+}
+
+
+
+function check() {
+	echo "<br>\n i run check()";
+    //todo to set arguments to rebuild tables etc:
+    $check_tbls=array('Rozdzielcza_test_bzyk_drogi','Rozdzielcza_test_bzyk_joins','Rozdzielcza_test_bzyk_przylacza_HIST',
+                    'Rozdzielcza_test_bzyk_przylacza','Rozdzielcza_test_bzyk_punkty_adresowe','Rozdzielcza_test_bzyk_ways');
+
+    //$tables =  DB::getPDO()->list_tables();
+    foreach($check_tbls as $tbl) {
+        $query = "select ID from ".$tbl." limit 1 ";
+        echo "<br>\nresult chk for tbl:".$tbl." ;";
+        
+        try{
+            $result = DB::getPDO()->fetchall($query);
+        } catch(Exception $exception) {
+            // return $exception;
+            echo "<br>\n jest blad z tbl:".$tbl." i get sample table structure with data from ../sql/".$tbl.".sql  - it should be manually replaced by correct/newer!";
+            $sql = file_get_contents('../sql/'.$tbl.'.sql');
+            
+            try{
+                $result = DB::getPDO()->fetchall($sql);
+            } catch(Exception $exception2) {
+                echo "<br>\n jest blad dump dla tbl:".$tbl;
+            }
+        } 
+        //	print_r($exception);
+        print_r($result);
+    }
+}
+
+init_sh_check();   
+
+$query = "select astext(the_geom) as multiway from Rozdzielcza_test_bzyk_drogi where numpoints(the_geom)>1";
+$result = DB::getPDO()->fetchall($query);
+$joins_asText = array();
+foreach ($result as $row) {
+	$multiway = Vendor_Geophp::load($row['multiway'], 'wkt');
+//echo $multiway->length();
+	for ($i = 1; $i < $n = $multiway->numPoints(); $i++) {
+		$way = new LineString(array($multiway->pointN($i), $multiway->pointN($i+1)));
+		$ways_asText[] = $way->asText();
+		if (!in_array($multiway->pointN($i)->asText(), $joins_asText)) $joins_asText[] = $multiway->pointN($i)->asText();
+	}
+	if (!in_array($multiway->pointN($n)->asText(), $joins_asText)) $joins_asText[] = $multiway->pointN($n)->asText();
+}
+
+$query = "select ID, astext(the_geom) as point, astext(the_geom_nearest_way) as way, astext(the_geom_xpoint) as xpoint from Rozdzielcza_test_bzyk_punkty_adresowe";
+$result = DB::getPDO()->fetchall($query);
+$xpoints = array();
+foreach ($result as $row) {
+        $points[$row['ID']] = Vendor_Geophp::load($row['point'], 'wkt');
+	$points_asText[$row['ID']] = $points[$row['ID']]->asText();
+        $xpoint = Vendor_Geophp::load($row['xpoint'], 'wkt');
+	if (!($key = array_search($xpoint->asText(), $xpoints))) $key = array_push($xpoints, $xpoint->asText())-1;
+	$xpoints2points[$key][] = $row['ID'];
+	$points2xpoints[$row['ID']] = $key;
+
+        $way = Vendor_Geophp::load($row['way'], 'wkt');
+
+	if (!(in_array($xpoint->asText(), $joins_asText))) {
+		if (@!in_array($key, $ways2cut[array_search($way->asText(), $ways_asText)])) {
+			$ways2cut[array_search($way->asText(), $ways_asText)][] = $key;
+		} else echo ".";
+	};
+}
+
+function pointsDistance($a, $b) {
+	$line = new LineString(array($a, $b));
+	return $line->length();
+}
+
+foreach ($ways2cut as $way_key => $xpoint_keys) {
+	$distances = array();
+	$way = Vendor_Geophp::load($ways_asText[$way_key], 'wkt');
+	foreach ($xpoint_keys as $xpoint_key) {
+		$xpoint = Vendor_Geophp::load($xpoints[$xpoint_key], 'wkt');
+		$key = array_push($joins_asText, $xpoint->asText())-1;
+		foreach ($xpoints2points[$xpoint_key] as $point_key) {
+			$points2joins[$point_key] = $key;
+		}
+		$distances[$xpoint_key] = pointsDistance($way->startPoint(), $xpoint);
+	}
+	asort($distances);
+
+	$sub_xpoints = array();
+	foreach (array_keys($distances) as $xpoint_key) {
+		$sub_xpoints[] = Vendor_Geophp::load($xpoints[$xpoint_key]);
+	}
+
+	$startPoint = $way->startPoint();
+	$endPoint = $way->endPoint();
+	$way = new LineString(array($startPoint, $sub_xpoints[0]));
+	$ways_asText[$way_key] = $way->asText();
+	for ($i = 0; $i < $n = count($sub_xpoints) - 1; $i++) {
+		$way = new LineString(array($sub_xpoints[$i], $sub_xpoints[$i+1]));
+		$ways_asText[] = $way->asText();
+	}
+	$way = new LineString(array($sub_xpoints[$n], $endPoint));
+	$ways_asText[] = $way->asText();
+}
+
+foreach (array_keys($ways_asText) as $way_key) {
+	$way = Vendor_Geophp::load($ways_asText[$way_key], 'wkt');
+	$ways_lenghts[$way_key] = $way->length();
+	for ($i = 1; $i <= 2; $i++) {
+		$join = $way->pointN($i)->asText();
+		$join_key = array_search($join, $joins_asText);
+//if (!$join_key) echo ".";
+		$ways2joins[$way_key][] = $join_key;
+		$joins2ways[$join_key][] = $way_key;
+	}
+}
+
+file_put_contents('ways2joins.txt',serialize($ways2joins));
+file_put_contents('joins2ways.txt',serialize($joins2ways));
+file_put_contents('points2joins.txt',serialize($points2joins));
+file_put_contents('joins_asText.txt',serialize($joins_asText));
+file_put_contents('ways_asText.txt',serialize($ways_asText));
+file_put_contents('points_asText.txt',serialize($points_asText));
+
+
+
+/*
+function findWay($join_key, $point_key, $path = array()) {
+	global $ways2joins, $joins2ways, $points2joins, $ways_asText, $joins2ways, $points, $points2xpoints, $xpoints2joins, $ways_lengths, $res;
+//echo implode('->',$path)."\n\n";
+	if ($join_key == $points2joins[$point_key]) $res[] = $path;
+	else {
+		foreach ($joins2ways[$join_key] as $way_key) {
+			if (!in_array($way_key, $path)) {
+//				$joins = $ways2joins[$way_key];
+//				$back_join_subkey = array_search($join_key, $joins);
+//				$new_join_subkey = 1 - $back_join_subkey;
+//				$new_join = $joins[$new_join_subkey];
+				$new_join = $ways2joins[$way_key][1 - array_search($join_key,  $ways2joins[$way_key])];
+				findWay($new_join, $point_key, array_merge($path,array($way_key)));
+			}
+		}
+	}
+}
+*/
+//print_r($ways2joins);
+//for ($i = 0; $i < count($points); $i++) {
+//	if (isset($res)) unset($res);
+//	findWay(2000, $i);
+//	print_r($res);
+//}
+//print_r($ways);
+
+echo "ways count: ".count($ways_asText)."\n";
+
+
+$db = DB::getDB();
+$db->query("truncate table Rozdzielcza_test_bzyk_ways");
+$db->query("truncate table Rozdzielcza_test_bzyk_joins");
+
+
+foreach ($ways_asText as $way) {
+	$query = "insert into Rozdzielcza_test_bzyk_ways (the_geom) values (linefromtext('".$way."'))";
+	$db->query($query);
+	//echo $query."\n";
+}
+
+foreach ($joins_asText as $join) {
+	$query = "insert into Rozdzielcza_test_bzyk_joins (the_geom) values (pointfromtext('".$join."'))";
+	$db->query($query);
+}
+

Plik diff jest za duży
+ 0 - 0
SE/dev/php-cli/joins2ways.txt


Plik diff jest za duży
+ 0 - 0
SE/dev/php-cli/joins_asText.txt


Plik diff jest za duży
+ 0 - 0
SE/dev/php-cli/points2joins.txt


Plik diff jest za duży
+ 0 - 0
SE/dev/php-cli/points_asText.txt


Plik diff jest za duży
+ 0 - 0
SE/dev/php-cli/ways2joins.txt


Plik diff jest za duży
+ 0 - 0
SE/dev/php-cli/ways_asText.txt


Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików