Select closest edge
Usually the start and end point, which we retrieved from the client, is not the start or end vertex of an edge. It is more convenient to look for the closest edge than for the closest vertex, because Shooting Star algorithm is “edge-based”. For “vertex-based” algorithms (Dijkstra, A-Star) we can choose arbitrary start or end of the selected edge.
Previous: PHP page template (basics)
// 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, distance(the_geom, GeometryFromText( 'POINT(".$lonlat[0]." ".$lonlat[1].")', 54004)) AS dist FROM ".TABLE." WHERE the_geom && setsrid( 'BOX3D(".($lonlat[0]-200)." ".($lonlat[1]-200).", ".($lonlat[0]+200)." ".($lonlat[1]+200).")'::box3d, 54004) 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); // Close database connection pg_close($con); return $edge; }
Next: Routing query