array(139.641481388889,35.4490805555556), "6280" => array(139.623825,35.4648452777778), "17838" => array(139.6484875,35.5529108333333 ), "76309" => array(139.480447777778,35.3009916666667), "69902" => array(139.504161666667,35.3190688888889), "124369" => array(139.048158611111,35.2500388888889), "8165" => array(139.646665,35.4395533333333), "45825" => array(139.691666944444,35.5235488888889), "125513" => array(139.131833333333,35.1568252777778) ); for($i=1;$i<5;$i++) { $next[$i]=0; } if($_GET["start"]) { //print($_GET); $start=$_GET["start"]; $valX = $coords[$start][0]; $valY = $coords[$start][1]; if( $valX < $minx ) $minx = $valX; if( $valY < $miny ) $miny = $valY; if( $valX > $maxx ) $maxx = $valX; if( $valY > $maxy ) $maxy = $valY; } for($i=1;$i<5;$i++) { if($_GET['next'.$i]) { $next[$i] = $_GET['next'.$i]; } if($next[$i] == 0) { $next[$i] = $start; } $valX = $coords[ $next[$i] ][0]; $valY = $coords[ $next[$i] ][1]; $px[ $i ] = $valX; $py[ $i ] = $valY; if( $valX < $minx ) $minx = $valX; if( $valY < $miny ) $miny = $valY; if( $valX > $maxx ) $maxx = $valX; if( $valY > $maxy ) $maxy = $valY; } $sql="the_geom from ". "tsp_astar('kanagawa', '".$start; $ids=$start.","; for($i=1;$i<5;$i++) { $sql = $sql.",".$next[$i]; $ids = $ids . $next[$i]; if( $i < sizeof($next) ) { $ids = $ids.","; } } $sql = $sql . $ids . "', ".$start.") as g using unique gid using SRID=-1"; // replace path_to_map_file below with the actual path to your map file. $map_file="path_to_map_file"; $map=ms_newMapObj($map_file); if( $minx != 9999 && $miny != 9999 && $maxx != -9999 && $maxy != -9999 || $minx == $maxx && $miny == $maxy ){ $map->setextent($minx-$delta,$miny-$delta, $maxx+$delta,$maxy+$delta); } $l=$map->getLayerByName("path"); if($l && $start!=0) { $l->set('data', $sql); $l->set('status', MS_DEFAULT); $tsp = solveTsp( $ids, $start ); for($i=1;$igetLayerByName("P".$i); if( $pl ) { addPoint($pl, $coords[$tsp[$i]][0], $coords[$tsp[$i]][1]); $pl->set('status', MS_DEFAULT); } } } else { $l->set('status', MS_OFF); } $image=$map->draw(); $image_url=$image->saveWebImage(); function solveTsp($ids,$start) { $tsp = array(); // modify the next line to include the correct values for database name, host, port, and user $conStr = "dbname=myDb host=myHost port=5432 user=myUser"; @$dbcon = pg_connect( $conStr ); $sql = "select vertex_id from tsp('select distinct source::integer as source_id, x(startpoint(the_geom)) as x, y(startpoint(the_geom)) as y from kanagawa where source in (".$ids.")','".$ids."',".$start.")"; if( $dbcon ) { @$res = pg_query($dbcon,$sql ); @$rowCount = pg_num_rows( $res ); while( $row = pg_fetch_row($res) ) { $tsp[] = $row[0]; } } return $tsp; } function addPoint($layer,$xx,$yy) { // // Create Point and Assign lon,lat values // $pointobj = ms_newPointObj(); $pointobj->setXY($xx,$yy); // // Create a Line Object that will contain the Point Object // $lineobj = ms_newLineObj(); $lineobj->add($pointobj); $pointobj->free(); $shapeobj = ms_newShapeObj(MS_SHAPE_POINT); $shapeobj->add($lineobj); $lineobj->free(); // // Add Line Object to the Layer // $layer->addFeature($shapeobj); $shapeobj->free(); } ?> pgRouting Application

pgRouting Sample Application

Travelling SalesPerson Problem


Start_Point



Destination_Points






Return