[pgrouting-users] pgrouting - Driving Direction - foss4g2010,
WORKSHOP
Adil BOUMAZOUED
boumazoued at gmail.com
Tue Feb 1 13:12:10 EST 2011
hi,
i hope this code can help you. it s not complete and not optimised
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
if ( extension_loaded('pgsql') != 1)
{
switch (PHP_OS)
{
case "WINNT": if (!extension_loaded('pgsql')) dl("php_pgsql.dll");
break;
default: if (!extension_loaded('pgsql')) dl("php_pgsql.so");
break;
}
}
//=========================================
// includes du fichier fonctions
//=========================================
require 'connexion.php';
function ConvertTemps ($temps) {
if ($temps<60) {
$temps=round($temps,0);
$msg= $temps.'s';
}
if ($temps>=60 && $temps<3600) {
$temps=round($temps,0);
$temps_s=$temps % 60;
$temps_mn = ($temps - $temps_s)/60;
$msg= $temps_mn.'mn'.$temps_s.'s';
}
if ($temps>=3600) {
$temps=round($temps,0);
$temps_s=$temps % 3600;
$temps_h=($temps - $temps_s)/3600;
if ($temps_s>=60) {
$temps_s=$temps % 60;
$temps_mn = ($temps - $temps_s)/60;
$msg= $temps_h.'h'.$temps_mn.'mn'.$temps_s.'s';
} else {
$msg= $temps_h.' h00mn '.$temps_s.'s';
}
}
return $msg;
}
function ConvertDistance ($longueur) {
$longueur=round($longueur,0);
if ($longueur>=1000) {
$longueur = round($longueur,0);
$longueur_metre=$longueur % 1000;
$longueur_km = ($longueur - $longueur_metre)/1000;
$msg= $longueur_km.'km'.$longueur_metre.'m';
} else {
$msg= $longueur.'m';
}
return $msg;
}
function direction($x1,$y1,$x2,$y2,$x3,$y3,$x4,$y4,$typ)
{
$pi=pi();
$Gisement=100000;
$Gisement1=100000;
$dx=1000000*($x2-$x1);
$dy=1000000*($y2-$y1);
if (($dx==0)&&($dy==0))
{
$Gisement=0;
}
if (($dy==0)&&($dx>0))
{
$Gisement=100;
}
if (($dy==0)&&($dx<0))
{
$Gisement=300;
}
if (($Gisement!=0)&&($Gisement!=100)&&($Gisement!=300))
{
$di = atan(abs($dx/$dy))*200/$pi;
if (($dx>=0)&&($dy<0)) {
$di=200-$di;
}
if (($dx<=0)&&($dy<0)) {
$di=200+$di;
}
if (($dx<=0)&&($dy>0)) {
$di=400-$di;
}
$Gisement=$di;
}
$dx=1000000*($x3-$x4);
$dy=1000000*($y3-$y4);
if (($dx==0)&&($dy==0))
{
$Gisement1=0;
}
if (($dy==0)&&($dx>0))
{
$Gisement1=100;
}
if (($dy==0)&&($dx<0))
{
$Gisement1=300;
}
if (($Gisement1!=0)&&($Gisement1!=100)&&($Gisement1!=300))
{
$di = atan(abs($dx/$dy))*200/$pi;
if (($dx>=0)&&($dy<0)) {
$di=200-$di;
}
if (($dx<=0)&&($dy<0)) {
$di=200+$di;
}
if (($dx<=0)&&($dy>0)) {
$di=400-$di;
}
$Gisement1=$di;
}
$g21=$Gisement;
$g23=$Gisement1;
$dir=-$g23+$g21+200;
if ($dir>200) {
$dir = $dir-400;
}
if ($dir<-200) {
$dir = $dir+400;
}
if ($dir>=-10 AND $dir<=10) {
$msg="<IMG width=32 height=32 SRC=img/roadmap/21.gif> Continuer tout
droit";
}
if ($dir>=-50 AND $dir<-10) {
$msg="<IMG width=32 height=32 SRC=img/roadmap/22.gif> Serrer à droite";
}
if ($dir>=-110 AND $dir<-50) {
$msg="<IMG width=32 height=32 SRC=img/roadmap/22.gif> Tourner à droite";
}
if ($dir>=-150 AND $dir<-100) {
$msg="<IMG width=32 height=32 SRC=img/roadmap/22.gif>Tourner trop serrer à
droite";
}
if ($dir>=-200 AND $dir<-150) {
$msg="<IMG width=32 height=32 SRC=img/roadmap/22.gif>Faite demi tour à
droite";
}
if ($dir>10 AND $dir<=50) {
$msg="<IMG width=32 height=32 SRC=img/roadmap/24.gif>Serrer à gauche";
}
if ($dir>50 AND $dir<=110) {
$msg="<IMG width=32 height=32 SRC=img/roadmap/24.gif>Tourner à gauche";
}
if ($dir>110 AND $dir<=150) {
$msg="<IMG width=32 height=32 SRC=img/roadmap/24.gif>Tourner trop serrer à
gauche";
}
if ($dir>150 AND $dir<=200) {
$msg="<IMG width=32 height=32 SRC=img/roadmap/24.gif>Faite demi tour à
gauche";
}
if($typ==1)
{
$msg="<IMG width=32 height=32 SRC=img/roadmap/10.gif> Prendre le Rond Point
: ";
}
RETURN $msg;
}
$counter = $pathlength = 0;
// Retrieve start point
$start = split(' ',$_REQUEST['startpoint']);
$startPoint1 = array($start[0], $start[1]);
$txtStart = $_REQUEST['txtstartpoint'];
$txtEnd = $_REQUEST['txtendpoint'];
//echo $_REQUEST['startpoint'];
// Retrieve end point
$end = split(' ',$_REQUEST['finalpoint']);
$endPoint1 = array($end[0], $end[1]);
//echo $_REQUEST['finalpoint'];
// Find the nearest edge
$startEdge = findNearestEdge($startPoint1);
$endEdge = findNearestEdge($endPoint1);
// FUNCTION findNearestEdge
function findNearestEdge($lonlat) {
// Connect to database
$con = pg_connect("host=".PG_HOST."
dbname=".PG_DB."
user=".PG_USER."
password=".PG_PASSWORD."");
//$con = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER."
password=".PG_PASSWORD);
$sql = "SELECT gid, source, target, the_geom,
distance(the_geom, GeometryFromText(
'POINT(".$lonlat[0]." ".$lonlat[1].")', -1)) AS dist
FROM ".TABLE."
WHERE the_geom && setsrid(
'BOX3D(".($lonlat[0]-0.1)."
".($lonlat[1]-0.1).",
".($lonlat[0]+0.1)."
".($lonlat[1]+0.1).")'::box3d, -1)
ORDER BY dist LIMIT 1";
//echo $sql;
$query = pg_query($con,$sql);
$edge['gid'] = pg_fetch_result($query, 0, 0);
$edge['source'] = pg_fetch_result($query, 0, 1);
$edge['target'] = pg_fetch_result($query, 0, 2);
$edge['the_geom'] = pg_fetch_result($query, 0, 3);
// Close database connection
pg_close($con);
return $edge;
}
// Select the routing algorithm
switch($_REQUEST['method']) {
case 'SPD' : // Shortest Path Dijkstra
$sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
length(rt.the_geom) AS length, ".TABLE.".id
FROM ".TABLE.",
(SELECT gid, the_geom
FROM dijkstra_sp_delta(
'".TABLE."',
".$startEdge['source'].",
".$endEdge['target'].",
0.01)
) as rt
WHERE ".TABLE.".gid=rt.gid;";
break;
case 'SPA' : // Shortest Path A*
$sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
".TABLE.".length, ".TABLE.".id, ".TABLE.".temps,
".TABLE.".on, ".TABLE.".typ, ".TABLE.".id , ".TABLE.".x1 as xs,
".TABLE.".y1 as ys, ".TABLE.".x2 as xe, ".TABLE.".y2 as ye
FROM ".TABLE.",
(SELECT *
FROM astar_sp_delta(
'".TABLE."',
".$startEdge['source'].",
".$endEdge['target'].",
0.01)
) as rt
WHERE ".TABLE.".gid=rt.gid;";
break;
case 'SPS' : // Shortest Path Shooting*
if ($_REQUEST['mode']==1) {
$sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
length(rt.the_geom) AS length, rt.length, rt.temps,
rt.on, rt.typ, ".TABLE.".id , rt.xs, rt.ys, rt.xe, rt.ye
FROM ".TABLE.",
(SELECT a.gid, a.the_geom, b.temps, b.on, b.typ ,
b.length as length, b.x1 as xs, b.y1 as ys, b.x2 as xe, b.y2 as ye
FROM shootingstar_sp(
'".TABLE."',
".$startEdge['gid'].",
".$endEdge['gid'].",
0.01, 'length',true, false) a, roads b where
a.gid=b.gid
) as rt
WHERE ".TABLE.".gid=rt.gid;";
} else {
$sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
length(rt.the_geom) AS length, rt.length, rt.temps,
rt.on, rt.typ, ".TABLE.".id , rt.xs, rt.ys, rt.xe, rt.ye
FROM ".TABLE.",
(SELECT a.gid, a.the_geom, b.temps, b.on, b.typ ,
b.length as length, b.x1 as xs, b.y1 as ys, b.x2 as xe, b.y2 as ye
FROM shootingstar_sp(
'".TABLE."',
".$startEdge['gid'].",
".$endEdge['gid'].",
0.01, 'temps',true, false) a, roads b where
a.gid=b.gid
) as rt
WHERE ".TABLE.".gid=rt.gid;";
}
break;
} // close switch
//echo $sql;
// Database connection and query
$dbcon = pg_connect("host=".PG_HOST."
dbname=".PG_DB."
user=".PG_USER."
password=".PG_PASSWORD."");
$query = pg_query($dbcon,$sql);
// Return route as html table
$xml1 .= "<style type='text/css'>";
$xml1 .= "a:link {";
$xml1 .= " color: #FFCC00;";
$xml1 .= "}";
$xml1 .= "a:visited {";
$xml1 .= " color: #FFCC00;";
$xml1 .= "}";
$xml1 .= ".style1 {font-size: 12px;font-family: Verdana, Arial, Helvetica,
sans-serif;font-weight: bold;}";
$xml1 .= ".style2 {font-family: Verdana, Arial, Helvetica,
sans-serif;font-size: 12px;}";
$xml1 .= ".style3 {color: #808080}";
$xml1 .= "</style>";
echo $xml1;
//echo "depart ".$startEdge['gid']." -------<p>";
$xml .= "sql=".$sql." fin de la requête";
$xml .= "<table width='100%' border='2' cellpadding='0' cellspacing='1'
bordercolor='#FF9F00' bgcolor='#FFFFFF'>";
$xml .="<tr bordercolor='#C0C0C0'>";
$xml .= "<td><strong>Etape</strong></td>";
$xml .= "<td><strong>Id Tronçon</strong></td>";
$xml .= "<td><strong>length Etape</strong></td>";
$xml .= "<td><strong>length Cumulé</strong></td>";
$xml .= "<td><strong>Temps moyen</strong></td>";
$xml .= "<td><strong>Nom de la Rue</strong></td>";
$xml .="</tr>";
// Add edges to XML file
while($edge=pg_fetch_assoc($query)) {
$pathlength += $edge['length'];
$xml .="<tr bordercolor='#C0C0C0'>";
$xml .= "<td> ".++$counter."</td>";
$xml .= "<td> ".$edge['gid']."</td>";
//déclaration des tableaux pour organisation de la feuille du route
$ii[$counter] = $edge['gid'];
$startPoint[$counter] = array($edge['xs'], $edge['ys']);
$endPoint[$counter] = array($edge['xe'], $edge['ye']);
$rue[$counter] = $edge['on'];
$x1[$counter] = $edge['xs'];
$y1[$counter] = $edge['ys'];
$x2[$counter] = $edge['xe'];
$y2[$counter] = $edge['ye'];
$length[$counter]=$edge['length'];
$temps[$counter]=3600*40/(1000*$edge['length']);
$type[$counter]=$edge['typ'];
//fin des déclaration
$temps1=3600*40/(1000*$edge['length']);
$temps_trajet +=$temps1;
if ($temps1>60) {
$temps1=$temps1/60;
$temps2=round($temps1,0)." mn";
}
if ($temps1>3600) {
$temps1=$temps1/3660;
$temps2=round($temps1,0)." h";
}
if ($temps1<60) {
$temps1=$temps1;
$temps2=round($temps1,0)." s";
}
$xml .= "<td> ".round($edge['length'],2)." m</td>";
$xml .= "<td> ".round(($pathlength),2)." m</td>";
$xml .= "<td> ".$edge['wkt']."</td>";
//pour vérifier le changement du rue par son nom
$on[$i+1]=$edge['on'];
$j = $i;
$i +=1;
if ($on[$i] != $on[$j])
{
$xml .= "<td> <img src='img/roadmap/2.png'>".$edge['on'].".</td>";
}
else {
$xml .= "<td> ".$edge['on'].".</td>";
}
$xml .="</tr>";
}
$xml .= "</table>";
// Close database connection
pg_close($dbcon);
echo "<h3 style='color:#2e585c;'>Feuille de route</h3>";
// Return routing result
//header('Content-type: text/xml',true);
echo "<div style='width:250px; float:left; border-left:1px solid #CCCCCC;
border-right:1px solid #CCCCCC; padding:0 10px;'>";
echo "<span class='style3'>".htmlentities("Résumé du Trajet")."</span><br>";
if ($_REQUEST['method']=='SPA')
{
echo "<span class='style3'>".htmlentities("Mode de Navigation :
")."</span><span
class='style3'>".htmlentities("Piéton")."</span></span><br>";
}
if ($_REQUEST['method']=='SPS')
{
echo "<span class='style3'>Mode de Navigation :</span><span
class='style3'>Voiture</span><br>";
if ($_REQUEST['mode']==1)
{
echo "<span class='style3'>".htmlentities("Type d'itinéraire :
")."</span><span class='style3'>Le plus court</span><br>";
} else {
echo "<span class='style3'>".htmlentities("Type d'itinéraire :
")."</span><span class='style3'>Le plus Rapide</span><br>";
}
}
echo "<span class='style3'>Longueur du Trajet : </span><span
class='style3'>";
echo ConvertDistance(round(($pathlength),0))."</span><br>";
echo "<span class='style3'>Temps total du Trajet : </span><span
class='style3'>";
echo ConvertTemps($temps_trajet)."</span></br>";
echo "</div>";
echo "</br>";
echo "</br>";
echo "</br>";
echo "</br>";
//echo $xml;
// echo "<p>_______________________________________________<p>";
// echo $counter."<p>";
// echo "_______________________________________________<p>";
//for($i=1; $i<=$counter ; $i++) {
//echo "&&startPoint[".$i."] = array(".$x1[$i].",".$y1[$i].");<p>";
//echo "&&endPoint[".$i."] = array(".$x2[$i].",".$y2[$i].");<p>";
//echo "&&rue[".$i."] = |".$rue[$i]."|;<p>";
//}
//recherche du premier tronçon de la route
for($i=1; $i<=$counter ; $i++) {
if ($ii[$i]==$startEdge['gid']) {
// echo "<script>alert(".$startEdge['gid'].");</script>";
//stockage temporaire de la ligne n°1
$tt=$rue[1];
$st=$startPoint[1];
$se=$endPoint[1];
$ll= $length[1];
$tm= $temps[1];
$xxx1=$x1[1];
$xxx2=$x2[1];
$yyy1=$y1[1];
$yyy2=$y2[1];
$type_route=$type[1];
//Attribution de la ligne n° 1 au premier tronçon de la route
$rue[1]=$rue[$i];
$startPoint[1]=$startPoint[$i];
$endPoint[1]=$endPoint[$i];
$length[1]=$length[$i];
$temps[1]=$temps[$i];
$x1[1]=$x1[$i];
$x2[1]=$x2[$i];
$y1[1]=$y1[$i];
$y2[1]=$y2[$i];
$type[1]=$type[$i];
//attribution du tronçon n° i de l'ex début de route par l'exe premier
enregistrement
$rue[$i]=$tt;
$startPoint[$i]=$st;
$endPoint[$i]=$se;
$length[$i]=$ll;
$temps[$i]=$tm;
$x1[$i]=$xxx1;
$x2[$i]=$xxx2;
$y1[$i]=$yyy1;
$y2[$i]=$yyy2;
$type[$i]=$type_route;
}
}
$i=1;
$j=1;
for($i=1; $i<=$counter ; $i++)
{
for($j=$i+1; $j<=$counter ; $j++)
{
if ($startPoint[$j]==$endPoint[$i])
{
$T0=$startPoint[$j];
$T1=$startPoint[$i+1];
$T2=$endPoint[$j];
$T3=$endPoint[$i+1];
$T4=$rue[$j];
$T5=$rue[$i+1];
$T6=$length[$j];
$T7=$length[$i+1];
$T8=$temps[$j];
$T9=$temps[$i+1];
$T10=$x1[$j];
$T11=$x1[$i+1];
$T12=$y1[$j];
$T13=$y1[$i+1];
$T14=$x2[$j];
$T15=$x2[$i+1];
$T16=$y2[$j];
$T17=$y2[$i+1];
$T18=$type[$j];
$T19=$type[$i+1];
$startPoint[$j]=$T1;
$startPoint[$i+1]=$T0;
$endPoint[$j]=$T3;
$endPoint[$i+1]=$T2;
$rue[$j]=$T5;
$rue[$i+1]=$T4;
$length[$j]=$T7;
$length[$i+1]=$T6;
$temps[$j]=$T9;
$temps[$i+1]=$T8;
$x1[$j]=$T11;
$x1[$i+1]=$T10;
$y1[$j]=$T13;
$y1[$i+1]=$T12;
$x2[$j]=$T15;
$x2[$i+1]=$T14;
$y2[$j]=$T17;
$y2[$i+1]=$T16;
$type[$j]=$T19;
$type[$i+1]=$T18;
}
}
}
//////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
for($i=0; $i<=$counter ; $i++)
{
//assemblage des tronçons par nom des rues
$comt_temp +=1;
$temps_troncon += $temps[$i];
$temps_cumule +=$temps[$i];
$length_troncon +=$length[$i];
$length_cumule +=$length[$i];
if ($rue[$i+1]!=$rue[$i])
{
$etape +=1;
if ($etape % 2 != 0) {
//echo "<tr bgcolor='#B8E1F5'>";
} else {
//echo "<tr>";
}
//echo "<td><span class='style2'>".$etape."</span></td>";
//echo "<td><span class='style1'>";
if ($i==1) {
//echo "<IMG width=32 height=32 SRC=img/drapeau/11.gif> Prendre ";
} elseif ($i==$counter) {
//echo "<IMG width=32 height=32 SRC=img/drapeau/12.gif> Arrivé à ";
} else {
//echo direction($x1[$i-1],$y1[$i-1],$x1[$i],$y1[$i],$x1[$i+1],$y1[$i+1],
$x1[$i+2],$y1[$i+2], $type[$i+1]);
$direction[$i]=direction($x1[$i-1],$y1[$i-1],$x1[$i],$y1[$i],$x1[$i+1],$y1[$i+1],
$x1[$i+2],$y1[$i+2], $type[$i+1]);
}
//echo $rue[$i]."</span></td>";
$NomRue[$i]=$rue[$i];
//echo "<td><span class='style2'>";
//echo ConvertDistance($length_troncon)."</span></td>";
$DistanceTroncon[$i]=ConvertDistance($length_troncon);
//echo "<td><span class='style2'>";
//echo ConvertDistance($length_cumule)."</span></td>";
$DistanceCumul[$i]=ConvertDistance($length_cumule);
//echo "<td><span class='style2'>";
//echo ConvertTemps($temps_troncon)."</span></td>";
$TempsTroncon[$i]=ConvertTemps($temps_troncon);
//echo "<td><span class='style2'>";
//echo ConvertTemps($temps_cumule)."</span></td>";
$TempsCumul[$i]=ConvertTemps($temps_cumule);
//echo "</tr>";
//initialiser le compteur
$comt_temp =0;
$temps_troncon=0;
$length_troncon=0;
}
}
//echo "</table>";
echo "<div style='background-color:#f0f0f0; color:#00519e; padding:0 10px;
margin-bottom:5px;'><div style='float:left; width:275px;'><img
src='img/drapeau/14.png'>".htmlentities("Départ")." :
".$txtStart."</div></div><br>";
echo "<div style='background-color:#f0f0f0; color:#00519e; padding:0
10px; margin-bottom:5px;'><div style='float:left;
width:275px;'>".$direction[0]."</div>";
echo $DistanceTroncon[1]."</div>";
echo "<div style='background-color:#f0f0f0; color:#00519e; padding:0
10px; margin-bottom:5px;'><div style='float:left; width:275px;'> de
</div></div>";
echo "<div style='background-color:#f0f0f0; color:#00519e; padding:0
10px; margin-bottom:5px;'><div style='float:left;
width:275px;'>".$NomRue[1]."</div></div>";
echo "<div style='background-color:#f0f0f0; color:#00519e; padding:0
10px; margin-bottom:5px;'><div style='float:left;
width:275px;'>".htmlentities(" ")."</div></div>";
echo "<p>";
for($i=1; $i<=$counter-2 ; $i++)
{
echo "<div style='background-color:#f0f0f0; color:#00519e; padding:0 10px;
margin-bottom:5px;'><div style='float:left;
width:275px;'>".$direction[$i]."</div></div>";
echo " ";
echo "<div style='background-color:#f0f0f0; color:#00519e; padding:0 10px;
margin-bottom:5px;'><div style='float:left;
width:275px;'>".$NomRue[$i+1]."</div>";
echo $DistanceTroncon[$i]."</div>";
//echo "coucou";
//echo "<p>";
}
echo "<div style='background-color:#f0f0f0; color:#00519e; padding:0 10px;
margin-bottom:5px;'><div style='float:left;
width:275px;'>".$direction[$counter-1]."</div></div>";
echo "<div style='background-color:#f0f0f0; color:#00519e; padding:0
10px; margin-bottom:5px;'><div style='float:left;
width:275px;'>".$direction[$i]."</div></div>";htmlentities(" et vous arrivez
à ")."</div></div>";
echo "<div style='background-color:#f0f0f0; color:#00519e; padding:0
10px; margin-bottom:5px;'><div style='float:left;
width:275px;'>".$NomRue[$counter]."</div>";
echo $DistanceTroncon[$counter-1]."</div></div>";
echo "<div style='background-color:#f0f0f0; color:#00519e; padding:0
10px; margin-bottom:5px;'><div style='float:left; width:275px;'><img
src='img/drapeau/15.png'>".htmlentities("Arrivé")." :
".$txtEnd."</div></div><br>";
?>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/pgrouting-users/attachments/20110201/58011936/attachment-0001.html
More information about the Pgrouting-users
mailing list