Adapting the code so that the gaps are filled¶
First of all you need to change the line 164-168 in the index.html
instead of
OpenLayers.loadURL("routing.php?startpoint="+startPoint.geometry.x+" "+startPoint.geometry.y+"&finalpoint="+
stopPoint.geometry.x+" "+stopPoint.geometry.y+"&method=SPD&srid=900913",
null,
null,
displayRoute,
null);
write
OpenLayers.loadURL("routing2.php?startpoint="+startPoint.geometry.x+" "+startPoint.geometry.y+"&finalpoint="
+stopPoint.geometry.x+" "+stopPoint.geometry.y+"&method=SPD&srid=900913",
null,
null,
displayRoute,
null);
The code of routing2 looks like
<?php
// Database connection settings
define("PG_DB" , "test");
define("PG_HOST", "localhost");
define("PG_USER", "postgres");
define("PG_PORT", "5432");
define("TABLE", "ways");
$counter = $pathlength = 1;
// Retrieve start point
$start = split(' ',$_REQUEST['startpoint']);
$startPoint = array($start[0], $start[1]);
/* echo $startPoint[0];
echo "<br>";
echo $startPoint[1];
echo "<br>";*/
// Retrieve end point
$end = split(' ',$_REQUEST['finalpoint']);
$endPoint = array($end[0], $end[1]);
// Find the nearest edge
$startEdge = findNearestEdge($startPoint);
$endEdge = findNearestEdge($endPoint);
// FUNCTION findNearestEdge
function findNearestEdge($lonlat) {
// Connect to database
$con = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER);
$sql = "SELECT gid, source, target, the_geom,astext(the_geom),
distance(the_geom, GeometryFromText(
'POINT(".$lonlat[0]." ".$lonlat[1].")', 900913)) AS dist
FROM ".TABLE."
WHERE the_geom && setsrid(
'BOX3D(".($lonlat[0]-200)."
".($lonlat[1]-200).",
".($lonlat[0]+200)."
".($lonlat[1]+200).")'::box3d, 900913)
ORDER BY dist LIMIT 1";
$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);
$edge['the_geom_as_text'] = pg_fetch_result($query, 0, 4);
pg_close($con);
return $edge;
}
$sql2="SELECT astext(multiline_locate_point(the_geom,PointFromText('POINT(".$startPoint[0]."
".$startPoint[1].")',900913))) from ways where gid=".$startEdge[gid];
$sql3="SELECT astext(multiline_locate_point(the_geom,PointFromText('POINT(".$endPoint[0].
" ".$endPoint[1].")',900913))) from ways where gid=".$endEdge[gid];
$con = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER);
$query2 = pg_query($con,$sql2);
$query3 = pg_query($con,$sql3);
$point_on_line_anfang= pg_fetch_result($query2, 0, 0);
$point_on_line_ende= pg_fetch_result($query3, 0, 0);
//echo $point_on_line_ende;
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'].",
3000) WHERE gid!=".$startEdge['gid']." AND gid!=".$endEdge['gid']."
) as rt
WHERE ".TABLE.".gid=rt.gid;";
// echo $sql;
break;
case 'SPA' : // Shortest Path A*
$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 astar_sp_delta(
'".TABLE."',
".$startEdge['source'].",
".$endEdge['target'].",
3000)
) as rt
WHERE ".TABLE.".gid=rt.gid;";
break;
case 'SPS' : // Shortest Path Shooting*
$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 shootingstar_sp(
'".TABLE."',
".$startEdge['gid'].",
".$endEdge['gid'].",
3000, 'length', false, false)
) as rt
WHERE ".TABLE.".gid=rt.gid;";
break;
} // close switch
$geometrie_results=pg_query($sql);
$anzahl_geometrien=pg_num_rows($geometrie_results);
//Ergebnis Nebengeometrie Anfang
for ($x=0;$x<$anzahl_geometrien;$x++)
{
$ergebnis_geometrie_gid[$x]=pg_result($geometrie_results,$x,gid);
$treffer_nebengeometrie_anfang=pg_query("SELECT b.gid,astext(b.the_geom) from (select the_geom from ways where
gid=".$startEdge['gid'].")a, (select gid,the_geom from ways WHERE gid=".$ergebnis_geometrie_gid[$x].") b
WHERE touches (a.the_geom,b.the_geom)");
$ergebnis_nebengeometrie_anfang_gid=@pg_result($treffer_nebengeometrie_anfang,0,gid);
IF ($ergebnis_nebengeometrie_anfang_gid!=''){
Break;
}
}
//Ergebnis Nebengeometrie Ende
for ($x=0;$x<$anzahl_geometrien;$x++)
{
$ergebnis_geometrie_gid[$x]=pg_result($geometrie_results,$x,gid);
$treffer_nebengeometrie_ende=pg_query("SELECT d.gid,astext(d.the_geom) from (select the_geom from ways where
gid=".$endEdge['gid'].")c, (select gid,the_geom from ways WHERE gid=".$ergebnis_geometrie_gid[$x].") d
WHERE touches (c.the_geom,d.the_geom)");
$ergebnis_nebengeometrie_ende_gid=@pg_result($treffer_nebengeometrie_ende,0,gid);
IF ($ergebnis_nebengeometrie_ende_gid!=''){
Break;
}
}
//echo "Also: ".$ergebnis_nebengeometrie_anfang_gid;
//echo "<br>";
//echo "Also: ".$ergebnis_nebengeometrie_ende_gid;
$con = pg_connect("dbname=".PG_DB." host=".PG_HOST." password=".PG_PASS." user=".PG_USER);
$query = pg_query($con,$sql);
//Geometrie-Anfang
$sql3="SELECT astext(give_we_wkt(".$startEdge['gid'].",".$ergebnis_nebengeometrie_anfang_gid.",'".
$point_on_line_anfang."'))";
$query3 = pg_query($con,$sql3);
$gesuchtes_wkt_anfang= pg_fetch_result($query3, 0, 0);
$sql4="SELECT length('".$gesuchtes_wkt_anfang."')";
$query4 = pg_query($con,$sql4);
$laenge_anfang= pg_fetch_result($query4, 0, 0);
//----------------------------------------
//Geometrie-Ende
$sql5="SELECT astext(give_we_wkt(".$endEdge['gid'].",".$ergebnis_nebengeometrie_ende_gid.",'".
$point_on_line_ende."'))";
$query5 = pg_query($con,$sql5);
$gesuchtes_wkt_ende= pg_fetch_result($query5, 0, 0);
$sql6="SELECT length('".$gesuchtes_wkt_ende."')";
$query6 = pg_query($con,$sql6);
$laenge_ende= pg_fetch_result($query6, 0, 0);
// Return route as XML
$xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n";
$xml .= "<route>\n";
$xml .= "\t<edge id='1'>\n";
$xml .= "\t\t<id>".$startEdge['gid']."</id>\n";
$xml .= "\t\t<wkt>".$gesuchtes_wkt_anfang."</wkt>\n";
$xml .= "\t\t<length>".round(($laenge_anfang/1000),3)."</length>\n";
$xml .= "\t</edge>\n";
// Add edges to XML file
while($edge=pg_fetch_assoc($query)) {
/*
if ($startEdge['gid']==$edge['id']) {
$edge=pg_fetch_assoc($query,1);
}*/
$pathlength += $edge['length'];
$xml .= "\t<edge id='".++$counter."'>\n";
$xml .= "\t\t<id>".$edge['id']."</id>\n";
$xml .= "\t\t<wkt>".$edge['wkt']."</wkt>\n";
$xml .= "\t\t<length>".round(($pathlength/1000),3)."</length>\n";
$xml .= "\t</edge>\n";
}
$xml .= "\t<edge id='".++$counter."'>\n";
$xml .= "\t\t<id>".$startEdge['gid']."</id>\n";
$xml .= "\t\t<wkt>".$gesuchtes_wkt_ende."</wkt>\n";
$xml .= "\t\t<length>".round(($laenge_ende/1000),3)."</length>\n";
$xml .= "\t</edge>\n";
$xml .= "</route>\n";
// Close database connection
pg_close($con);
// Return routing result
header('Content-type: text/xml',true);
echo $xml;
?>
… and with that the gaps will be filled :-)
Best regards, Kai Behncke