[pgrouting-users] Re: Pgrouting-users Digest, Vol 29, Issue 2

Thanks Adil,
I will take a look on it and let see what I will come up with.
Merci
Cleber

P.S. I will keep in touch if I have any doubt if you don’t mind.


On Tue, Feb 1, 2011 at 7:12 PM, <pgrouting-users-request@lists.osgeo.org> wrote:

Send Pgrouting-users mailing list submissions to
pgrouting-users@lists.osgeo.org

To subscribe or unsubscribe via the World Wide Web, visit
http://lists.osgeo.org/mailman/listinfo/pgrouting-users
or, via email, send a message with subject or body ‘help’ to
pgrouting-users-request@lists.osgeo.org

You can reach the person managing the list at
pgrouting-users-owner@lists.osgeo.org

When replying, please edit your Subject line so it is more specific
than “Re: Contents of Pgrouting-users digest…”

Today’s Topics:

  1. Re : [pgrouting-users] pgrouting - Driving Direction -
    foss4g2010, WORKSHOP (Adil BOUMAZOUED)

Message: 1
Date: Tue, 1 Feb 2011 18:12:10 +0000
From: Adil BOUMAZOUED <boumazoued@gmail.com>
Subject: Re : [pgrouting-users] pgrouting - Driving Direction -
foss4g2010, WORKSHOP
To: pgrouting-users@lists.osgeo.org
Message-ID:
<AANLkTimHofmC6TXUgSP-W3pw4DnM8XK9Y+Bb1kb1K8rP@mail.gmail.com>
Content-Type: text/plain; charset=“iso-8859-1”

hi,
i hope this code can help you. it s not complete and not optimised
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<?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=" Continuer tout droit"; } if ($dir>=-50 AND $dir<-10) { $msg=" Serrer à droite"; } if ($dir>=-110 AND $dir<-50) { $msg=" Tourner à droite"; } if ($dir>=-150 AND $dir<-100) { $msg="Tourner trop serrer à droite"; } if ($dir>=-200 AND $dir<-150) { $msg="Faite demi tour à droite"; } if ($dir>10 AND $dir<=50) { $msg="Serrer à gauche"; } if ($dir>50 AND $dir<=110) { $msg="Tourner à gauche"; } if ($dir>110 AND $dir<=150) { $msg="Tourner trop serrer à gauche"; } if ($dir>150 AND $dir<=200) { $msg="Faite demi tour à gauche"; } if($typ==1) { $msg=" 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](http://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](http://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 .= ""; $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 .= ""; echo $xml1; //echo "depart ".$startEdge['gid']." -------

"; $xml .= "sql=".$sql." fin de la requête"; $xml .= "

"; $xml .=""; $xml .= ""; $xml .= ""; $xml .= ""; $xml .= ""; $xml .= ""; $xml .= ""; $xml .=""; // Add edges to XML file while($edge=pg_fetch_assoc($query)) { $pathlength += $edge['length']; $xml .=""; $xml .= ""; $xml .= ""; //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 .= ""; $xml .= ""; $xml .= ""; //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 .= ""; } else { $xml .= ""; } $xml .=""; } $xml .= "
EtapeId Tronçonlength Etapelength CumuléTemps moyenNom de la Rue
".++$counter." ".$edge['gid']." ".round($edge['length'],2)." m ".round(($pathlength),2)." m ".$edge['wkt']." ".$edge['on'].". ".$edge['on'].".
"; // Close database connection pg_close($dbcon); echo "

Feuille de route

"; // Return routing result //header('Content-type: text/xml',true); echo "
"; echo "".htmlentities("Résumé du Trajet")."
"; if ($_REQUEST['method']=='SPA') { echo "".htmlentities("Mode de Navigation : ")."".htmlentities("Piéton")."
"; } if ($_REQUEST['method']=='SPS') { echo "Mode de Navigation :Voiture
"; if ($_REQUEST['mode']==1) { echo "".htmlentities("Type d'itinéraire : ")."Le plus court
"; } else { echo "".htmlentities("Type d'itinéraire : ")."Le plus Rapide
"; } } echo "Longueur du Trajet : "; echo ConvertDistance(round(($pathlength),0))."
"; echo "Temps total du Trajet : "; echo ConvertTemps($temps_trajet)."
"; echo "
"; echo "
"; echo "
"; echo "
"; echo "
"; //echo $xml; // echo "

_______________________________________________

"; // echo $counter."

"; // echo "_______________________________________________

"; //for($i=1; $i<=$counter ; $i++) { //echo "&&startPoint[".$i."] = array(".$x1[$i].",".$y1[$i].");

"; //echo "&&endPoint[".$i."] = array(".$x2[$i].",".$y2[$i].");

"; //echo "&&rue[".$i."] = |".$rue[$i]."|;

"; //} //recherche du premier tronçon de la route for($i=1; $i<=$counter ; $i++) { if ($ii[$i]==$startEdge['gid']) { // echo ""; //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 ""; } else { //echo ""; } //echo "".$etape.""; //echo ""; if ($i==1) { //echo " Prendre "; } elseif ($i==$counter) { //echo " 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].""; $NomRue[$i]=$rue[$i]; //echo ""; //echo ConvertDistance($length_troncon).""; $DistanceTroncon[$i]=ConvertDistance($length_troncon); //echo ""; //echo ConvertDistance($length_cumule).""; $DistanceCumul[$i]=ConvertDistance($length_cumule); //echo ""; //echo ConvertTemps($temps_troncon).""; $TempsTroncon[$i]=ConvertTemps($temps_troncon); //echo ""; //echo ConvertTemps($temps_cumule).""; $TempsCumul[$i]=ConvertTemps($temps_cumule); //echo ""; //initialiser le compteur $comt_temp =0; $temps_troncon=0; $length_troncon=0; } } //echo ""; echo "

".htmlentities("Départ")." : ".$txtStart."

"; echo "
".$direction[0]."
"; echo $DistanceTroncon[1]."
"; echo "
de
"; echo "
".$NomRue[1]."
"; echo "
".htmlentities(" ")."
"; echo "

"; for($i=1; $i<=$counter-2 ; $i++) { echo "

".$direction[$i]."
"; echo " "; echo "
".$NomRue[$i+1]."
"; echo $DistanceTroncon[$i]."
"; //echo "coucou"; //echo "

"; } echo "

".$direction[$counter-1]."
"; echo "
".$direction[$i]."
";htmlentities(" et vous arrivez à ").""; echo "
".$NomRue[$counter]."
"; echo $DistanceTroncon[$counter-1]."
"; echo "
".htmlentities("Arrivé")." : ".$txtEnd."

"; ?>

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.osgeo.org/pipermail/pgrouting-users/attachments/20110201/58011936/attachment.html



Pgrouting-users mailing list
Pgrouting-users@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/pgrouting-users

End of Pgrouting-users Digest, Vol 29, Issue 2



Cléber D. Arruda
Kämnärsvägen 33C
226 46
Lund - Sweden
Phone +46737762526
E-mail: cleverdo@gmail.com

Peace and Love Always.