I have created an Android application that will accept source and destination from the user. It then calls a php page which takes in the lattitude and longitude of the destination and the source. I am using postgis in the backend, and have created the topology required for pgrouting. Following is my php code:
First I find the all the source integers thats near to my current location using the following function:-
function getSourceId($currentPositionLongitude,$currentPositionLattitude,$approximation,$dbConnectionString){
// the query will return the source id of the user.
$db_connection = pg_connect($dbConnectionString);
$queryForSource = "select source from mumbai_roads
where ST_Dwithin(geom,ST_GeomFromText('POINT( $currentPositionLongitude $currentPositionLattitude)'),$approximation)";
$source=array();
$myresult = pg_query($db_connection,$queryForSource);
while($myrow = pg_fetch_assoc($myresult)) {
$resultSource = $myrow['source'];
array_push($source, $resultSource);
}
//print_r($source);
return $source;
}
Now i find all the target integers that are near my destination location:-
function getTarget($destinationLongitude,$destinationLattitude,$approximation,$dbConnectionString){
$db_connection = pg_connect($dbConnectionString);
$queryForDestination = "select target from mumbai_roads
where ST_Dwithin(geom,ST_GeomFromText('POINT( $destinationLongitude $destinationLattitude)'),$approximation)";
$target = array();
$myresult = pg_query($db_connection,$queryForDestination);
while($myrow = pg_fetch_assoc($myresult)) {
$resultTarget = $myrow['target'];
array_push($target, $resultTarget);
}
//print_r($target);
return $target;
}
Finally as i have both the source and target integers, I try to get the shortest path for all the sources and the targets:-
if(!(empty($source) || empty($target))){
/*looping through all the source and the targets */
foreach ($source as $sourceId) {
foreach ($target as $targetId) {
$json = getShortestPath($sourceId,$targetId,$dbConnectionString);
if($json!=""){
$result=$result.$json;
echo "$result";
}
}
}
}
function getShortestPath($source,$target,$dbConnectionString){
$db_connection = pg_connect($dbConnectionString);
$queryForShortestDistance = "select seq, id1 AS node, id2 AS edge, di.cost AS cost1, ST_AsGeoJSON(geom) AS geom
FROM pgr_dijkstra(
'SELECT id, source, target, cost FROM mumbai_roads',
$source, $target, false, false
) as di
JOIN mumbai_roads pt
ON di.id2 = pt.id";
$geom="";
$myresult = pg_query($db_connection,$queryForShortestDistance);
while($myrow = pg_fetch_assoc($myresult)) {
$geom = $myrow['geom'];
}
return $geom;
}
I am getting the path as a multiline which i am mapping on android, but using the above code, i am not getting the whole path, but just a small fraction of the whole path. Please help!