| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?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");
- 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();
- $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'));
- $edge_joins=simplexml_load_file("edge_joins.xml");
- $edge_joins->registerXPathNamespace('bp', 'https://procesy5.pl/biale_plamy-schema.xsd');
- //print_r($edge_joins);
- foreach($edge_joins->children() as $child) {
- // echo "\n<br>";
- //$attrs=$child->attributes("ID_Way");
- foreach($child->attributes() as $a=>$b) {
- // echo "#87Child";print_r($b[0]);
- $ID_Way=$b[0];
- }
- //echo $child->getName() . ": " . " ID_Way: ".$ID_Way. " : ";
-
- $id_j=0;
- settype($ID_Way, "integer");
- //$ID_Way=var_dump($ID_Way);
-
- // echo "#88Child:"; print_r($ID_Way);
- //$edge_joins_path['ID_Way'][$attrs['ID_Way']]=array();
- foreach($child->children('https://procesy5.pl/biale_plamy-schema.xsd') as $edge_joins_xml) {
- $id_j++;
- $attrs_join=$edge_joins_xml->attributes();
- //echo " #93 ID_Join ".$attrs_join["Joins"]." ";
- $Join=$attrs_join["Joins"][0];
- settype($Join, "integer");
- $edge_joins_path[$ID_Way][$id_j]=$attrs_join["Joins"][0];
- }
- }
- //print_r($edge_joins_path);
- function joins2lineString($joins_keys) {
- global $joins_asText;
- if ($joins_keys) {
- foreach ($joins_keys as $join_key) {
- $points[] = Vendor_Geophp::load($joins_asText[$join_key], 'wkt');
- }
- return new LineString($points);
- } else return null;
- }
- function findWay($joinA, $joinB, $path = array()) {
- global $ways2joins, $joins2ways, $points2joins, $ways_asText, $points_asText;
- if ($joinA == $joinB) {
- $joinsPath = array($joinB);
- foreach (array_reverse($path) as $way_key) {
- array_unshift($joinsPath, $ways2joins[$way_key][1 - array_search($joinsPath[0], $ways2joins[$way_key])]);
- }
- return $joinsPath;
- } else {
- $subPaths = array();
- foreach ($joins2ways[$joinA] as $way_key) {
- if (!in_array($way_key, $path)) {
- $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) {
- $way = Vendor_Geophp::load($ways_asText[$way_key], 'wkt');
- if ($way) $distance_cur += Vendor_Geophp::load($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;
- }
- }
- }
- function findJoinInDB($id) {
- global $joins_asText;
- $join = DB::getPDO()->fetchValue("select astext(the_geom) from Rozdzielcza_test_bzyk_joins where ID='{$id}'");
- if ($join) {
- $join = Vendor_Geophp::load($join, 'wkt')->asText();
- $key = array_search($join, $joins_asText);
- if ($key) return $key;
- else return null;
- } else return null;
- }
- 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 {
- // $test_expl=explode(" ", $value);
- //preg_match('/[a-zA-Z]/', $value[0])
- //if($key_=="" and !isset($test_expl[1]) ) $key_=htmlspecialchars("$value");
- //czy nie jest jeden element array
- if($key_=="" and count($data[$key]==1)) {
- $subnode=$xml_data->addChild("$key",htmlspecialchars("$value"));
- } else
- $subnode=$xml_data->addChild("$key_",htmlspecialchars("$value"));
- if( is_numeric($key) ){
- $subnode->addAttribute('id', $key);
- }
- }
- }
- }
- #echo joins2lineString($res = findWay(100,110))->asText();
- #echo joins2lineString($res = findWay(25690,20134))->asText();
- #$path = joins2lineString(findWay(25690,23039));
- //$path = joins2lineString(findWay(findJoinInDB(49),findJoinInDB(50)));
- //generujemy path do $edge_joins_filled_paths
- DB::getPDO()->query("truncate table Rozdzielcza_test_bzyk_paths");
- unset($ID_Way);
- $edge_joins_filled_paths=array();
- foreach($edge_joins_path as $ID_Way=>$Joins_arr) {
- settype($ID_Way, "integer");
- $edge_joins_filled_paths[$ID_Way]['tried']=1;
- if(isset($Joins_arr[1]) and isset($Joins_arr[2])) {
- $pkt_A=$Joins_arr[1];$pkt_B=$Joins_arr[2];
- settype($pkt_A, "integer"); settype($pkt_B, "integer");
- echo "\n<br> Generuje dla ID_Way:".$ID_Way." i joins ".$pkt_A." , ".$pkt_B." ";
- unset($res);unset($path);
- $res = findWay($pkt_A,$pkt_B);
- $path = joins2lineString($res)->asText();
- // joins2lineString(findWay(389,404));
- settype($path, "string");
- $edge_joins_filled_paths[$ID_Way]['asText'] = $path;
- $distance = 0;
- foreach ($res as $way_key) {
- if(isset($ways_asText[$way_key]))
- $distance += Vendor_Geophp::load($ways_asText[$way_key], 'wkt')->greatCircleLength();
- }
- $edge_joins_filled_paths[$ID_Way]['distance'] = $distance;
- //joins2lineString(findWay($Joins_arr[1],$Joins_arr[2]));
- //joins2lineString($res = findWay($joinAB['A'],$joinAB['B']))->asText();
- $sql_insert_path="insert into Rozdzielcza_test_bzyk_paths (the_geom,ID_Way)
- values (ST_GeomFromText('".$edge_joins_filled_paths[$ID_Way]['asText']."'),'".$ID_Way."' ) ";
- echo "#216Query path ins:: ".$sql_insert_path." , distance:".$distance." ";
- if(strlen($edge_joins_filled_paths[$ID_Way]['asText'])>2)
- DB::getPDO()->query($sql_insert_path);
- else echo "#296 path nie wygenerowalo sie !! dla ".$ID_Way." i pkt ".$pkt_A." , ".$pkt_B." \n";
- } else echo "\n#220 droga nie ma koncow ID_Way::".$ID_Way;
- }
- echo "<br>\n edge_joins_filled_paths:";
- print_r($edge_joins_filled_paths);
- //save
- unset($xml_data);
- file_put_contents('edge_joins_filled_paths.txt',serialize($edge_joins_filled_paths));
- ob_start();print_r($edge_joins_filled_paths); $contents = ob_get_contents();ob_end_clean();
- file_put_contents('edge_joins_filled_paths_print_r.txt',$contents);
- $xml_data = new SimpleXMLElement('<?xml version="1.0"?><edge_joins_filled_paths></edge_joins_filled_paths>');
- array_to_xml($edge_joins_filled_paths,$xml_data);
- print $xml_data->asXML("edge_joins_filled_paths.xml");
- unset($xml_data);
-
- //test
- $timeStart = microtime();
- $res = findWay(389,404);
- $path=joins2lineString($res)->asText();
- $timeEnd = microtime();
-
- if ($path) {
- echo $path."\n";
- } else echo "Brak drogi";
-
- list($usecStart, $secStart) = explode(" ", $timeStart);
- list($usecEnd, $secEnd) = explode(" ", $timeEnd);
- $diff = $secEnd - $secStart + $usecEnd - $usecStart;
- echo "\n\nCzas liczenia: ".$diff." s\n";
-
-
- $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";
- //eof test
- echo "\n<br>test2";
- //test
- $timeStart = microtime();
- $res = findWay(389,404);
- $path=joins2lineString($res)->asText();
- $timeEnd = microtime();
-
- if ($path) {
-
- echo $path."\n";
- } else echo "Brak drogi";
-
- list($usecStart, $secStart) = explode(" ", $timeStart);
- list($usecEnd, $secEnd) = explode(" ", $timeEnd);
- $diff = $secEnd - $secStart + $usecEnd - $usecStart;
- echo "\n\nCzas liczenia: ".$diff." s\n";
-
-
- $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";
- //eof test
- //$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";
|