$time_start = microtime(true);
include 'db.php';
$con = pg_connect("dbname=".$db." host=".$dbhost." user=".$dbuser." password=".$dbpass)
or die ("PostgreSQL Error --> " . pg_last_error($con));
function RandString($len, $num, $uc, $lc) {
if (!$len || $len<1 || $len>100) {
print "Error: \"Length\" out of range (1-100)
\n";
return;
}
if (!$num && !$uc && !$lc) {
print "Error: No character types specified
\n";
return;
}
$s="";
$i=0;
do {
switch(mt_rand(1,3)) {
// get number - ASCII characters (0:48 through 9:57)
case 1:
if ($num==1) {
$s .= chr(mt_rand(48,57));
$i++;
}
break;
// get uppercase letter - ASCII characters (a:65 through z:90)
case 2:
if ($uc==1) {
$s .= chr(mt_rand(65,90));
$i++;
}
break;
// get lowercase letter - ASCII characters (A:97 through Z:122)
case 3:
if ($lc==1) {
$s .= chr(mt_rand(97,122));
$i++;
}
break;
}
} while ($i<$len);
return $s;
}
function make_time ($i, $xml) {
$totalsec = $xml->edge[$i]->time;
// echo $totalsec;
if ( intval ($totalsec) >= 60 ) {
//that means more than 1 minutes
$min_remainder = fmod ((intval ($totalsec)), 60);
$min = intval ((intval ($totalsec)) / 60);
}
else { $min = 0; }
$sec = round (($totalsec + $min_remainder),0);
$tajm = $min.":".$sec;
return $tajm;
}
function compare_angles ($i, $xml) {
if ($xml->edge[$i]->angle == $xml->edge[$i-1]->angle) {
$direction = "Keep moving ".$xml->edge[$i]->angle ;
}
else $direction = "Turn ".$xml->edge[$i]->angle ;
return $direction ;
}
function node_details ($i, $tajm, $xml) {
$msg = '
Next node ('.$i.') : '. compare_angles($i, $xml).' to enter '. $xml->edge[$i]->sname . '. Keep following the road for ' .$tajm. ' minutes Remember : Maximum Speed is : ' . $xml->edge[$i]->speed . '
';
return $msg;
}
function randomline ($xml, $i ,$j) {
/*
$xml = the xml
$i = the count we are on
$j = max lines in multilinestring
*/
// echo "MAX LINES IS ".$j;
$sql12 = "select astext ( geometryN ( GeomFromEWKT('".$xml->edge[$i]->wkt."' ), ".$j.") ) as random_line ";
// echo "Random Linestring : ".$sql12 ;
$sqlq = pg_query ($sql12) or die ("PostgreSQL Error --> " . pg_last_error($con));
$result2 = pg_fetch_assoc($sqlq);
// print_r ($result2);
// echo "
=========================================================
";
return $result2['random_line']; //the line referenced
}
function howmanypoints ($line) {
// $line = the randome line chosen by function randomline
$sql14 = "select numpoints(GeomFromEWKT('".$line."')) as num_of_points";
// echo "How many poings on line : $sql14
";
$sqlq14 = pg_query ($sql14) or die ("PostgreSQL Error --> " . pg_last_error($con));
$result14 = pg_fetch_assoc($sqlq14);
// print_r ($result14);
$noofpoints = $result14['num_of_points'];
// echo "
=========================================================
";
return $noofpoints ;
// $noofpoints is returning how many points there are in the linestring
}
function randomgeom ($xml, $i , $num_points_on_line, $the_line_string) {
/*
$xml = the xml edges
$i = the count we are working on
$num_points_on_line = the number of linestrings on the multilinestring we are working on
$the_line_string = the actual linestring we just randomised
*/
//randomizing a line number
// $k = rand(1,$j); // where $j is the number of how many linestrings there are
// $randomline = randomline($xml, $i, $k);
//$randomline holds a random line out of the lines in a multistring
//$num_points_on_line = howmanypoints($randomline);
// $numpointsonline holds how many points there are on the line
// randomizing a point on the line now
$g = rand(1,$num_points_on_line);
// $g hows the random reference to the random point
// now it is time to get the random point
$sql9 = "select astext ( pointN ( GeomFromEWKT('".$the_line_string."' ), ".$g.") ) as random_point ";
// echo "Random Point : ".$sql9 ;
$sqlq = pg_query ($sql9) or die ("PostgreSQL Error --> " . pg_last_error($con));
$result9 = pg_fetch_assoc($sqlq);
// echo "
printing result9 ";
// print_r ($result9);
$random_pt = $result9['random_point'];
// $randompt has a random point in text format
return $random_pt;
}
function get_end_point ($xml, $i) {
$sql3 = "select astext( endpoint ( GeomFromEWKT('".$xml->edge[$i]->wkt."') ) ) as end_pt" ;
$sqlq3 = pg_query ($sql3) or die ("PostgreSQL Error --> " . pg_last_error($con));
$result3 = pg_fetch_assoc($sqlq3) ;
// print_r ($result3);
// echo "
=========================================================
";
// $result3 holds the ending POINT of the multilinestring
$endpt = $result3['end_pt'];
// echo "End pt = ".$endpt;
return $endpt;
}
function get_how_many_linestrings_in_multi ($xml,$i) {
$sql = "select numgeometries(GeomFromEWKT('".$xml->edge[$i]->wkt."'))";
//= echo "Lines in MultiLIne String $sql
";
$sqlq = pg_query ($sql) or die ("PostgreSQL Error --> " . pg_last_error($con));
$result = pg_fetch_assoc($sqlq);
//= print_r ($result);
//= echo "
=========================================================
";
$j = $result['numgeometries'];
// $j holds how many linestrings there are in the multilinestring
return $j;
}
function compare_random_with_end ($rgeom, $end_pt) {
/*
$rgeom = the random geometric point on the random linestring
$end_point = the ending point of the MULTIlienstring
*/
$sql4 = "select distance ( (GeomFromEWKT('".$rgeom."')), (GeomFromEWKT('".$end_pt."')) ) as dist ";
//= echo "$sql4
";
$sqlq4 = pg_query ($sql4) or die ("PostgreSQL Error --> " . pg_last_error($con));
$result4 = pg_fetch_assoc($sqlq4) ;
//= print_r ($result4);
$dist = $result4['dist'];
//= echo "
=========================================================
";
//$dist holds a t=true / f=false boolean answer from $sql4
return $dist;
}
function doquery ($sleep, $id, $tid, $contents) {
$ip = $_SERVER['REMOTE_ADDR'];
$contentz = 'sleep='.urlencode($sleep) . '&id=' .urlencode($id). '&tid=' .urlencode($tid). '&ip=' .urlencode($ip). '&contents=' .urlencode($contents);
//echo $contents;
$page = 'http://localhost/~yancho/insertscript.php?'.$contentz;
//echo $page;
$fp = fopen ($page , "r");
fgets($fp,1024);
fclose($fp);
}
// End of functions - Start of main routine
$url = "http://127.0.0.1/~yancho/routing.php?startpoint=642568.5%20224590.5&finalpoint=640956%20224283&method=on®ion=streets&srid=-1&_=";
$xml = simplexml_load_file($url);
$tid = randstring(20,1,0,1);
setcookie("tid", $tid, time()+1200);
session_start();
//echo "tid is : $tid";
$_SESSION['tid'] = $tid;
//print_r ($_SESSION);
//print_r ($_cookie);
echo "STARTING THE RUN";
$contents = "
Welcome to this guided tour
";
doquery(0, 0, $tid, $contents);
$i = 0;
//sleep (2);
// outputting the time + first direction start of journey
//echo date("H:i:s");
// $handle = fopen($filename, "a");
$contents2 = node_details ($i, make_time($i, $xml), $xml );
doquery(2, 1, $tid, $contents2);
$i = 1;
// starting parsing the route
//foreach($xml->edge as $edge){
$k = sizeof($xml->edge);
// echo "THE SIZE OF THE ARRAY : $k";
//ob_start ();
for ($i=2; $i < $k ; $i++) {
// echo "================================++++++++++++++++++++++++++++++=========================================";
// echo " WORKING ON A NEW EDGE!!!!! ";
// echo "================================++++++++++++++++++++++++++++++=========================================";
//= echo "Check how many linestrings the multilinestring has
";
// echo "
WORKING ON EDGE : ".$xml->edge[$i]->id." WITH THE NAME ".$xml->edge[$i]->sname." ";
// Check how many linestrings the multilinestring has
$no_of_linestrings = get_how_many_linestrings_in_multi ($xml,$i);
do {
//= echo "
Randomizings one of the lines in the multiline
";
// randomizing one of the lines
$randomline = randomline ($xml, $i ,$no_of_linestrings) ;
//=echo "
Checking how many points there are on this linestring
";
// Checking how many points there are on this linestring
$how_many_points_linestring = howmanypoints ($randomline) ;
//=echo "
Now we got the line .. time to get a poin on the line
";
// randomizing a point on one of the lines
$rgeom = randomgeom($xml,$i, $how_many_points_linestring, $randomline );
//=echo "
rgeom is : $rgeom
";
//$rgeom now holds a random point on a random line string in the multistring we are dealing with
//=echo "
Now getting end pt
";
// now it is time to get the actual ending point of the multi line string
$end_pt = get_end_point($xml, $i);
//$end_pt has the ending point of the multilinestring
//= echo "
Now comparing both
";
// now we need to compare distance
$dist = compare_random_with_end ($rgeom, $end_pt);
//$dist holds a t = true .. or an f = false
//= echo "
Booelan Dist is : $dist
";
//sleep(2); //sleeping 20seconds before next randomizing
// echo "
-------------------------------------~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~----------------------
";
}
while ( $dist >= 30 );
// we are near 100meters of the ending point .. time to display the new node
// echo date("H:i:s");
$contents3 = node_details ($i, make_time($i, $xml), $xml ); // HAWNEKK FEJN HA JIBGHAT ID-DATA LIL-FUNCTION U HA TECHOWJA LINE
doquery (5, $i, $tid, $contents3);
// ob_flush();
// flush() ;
/* if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fflush($handle);
fclose($handle);
}
else {
echo "The file $filename is not writable";
} */
sleep (5);
// $i = $i + 1 ;
}
//echo date("H:i:s");
//$handle = fopen($filename, "a");
$contents4 = "
Welcome to destination - Hope you enjoyed your trip";
/*fwrite($handle, $contents3);
fflush($handle);
fclose($handle);
echo "File written successfully"; */
doquery (5, $k+1, $tid, $contents4);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "SCRIPT TOOK : $time";
?>