prabhasp / yatayat

Playing around with osm and transportation data. See https://github.com/monsooncollective/yatayat for the maintained repo.
http://yatayat.monsooncollective.org
6 stars 23 forks source link

More intelligent nearestStops #10

Open prabhasp opened 12 years ago

prabhasp commented 12 years ago

The basic nearestStops from #9 asks for N, the number of nearestStops to return. A more intelligent implementation for nearestStops would not ask for N, instead it would ask for a multiplier K, and would return all stops where dist(stop) < K * dist(nearestStop). K should probably be ~2 by default.

But that even this schema doesn't prevent us from returning two nearestStops in the same direction from a start location, which is useless (multiple nearestStops are only helpful when they are in different directions). Other ideas?

samrakchan commented 12 years ago

Absolutely correct. If a person want to go to east it might show nearest stop of west which is again completely useless.

prabhasp commented 12 years ago

Actually, that can be useful, if east and west are not connected, except thorugh a second route. For example, I am in an area between Gaushala and Purano Baneshwor, and want to go to Koteshwor. The optimal route is probably ring road circuit through Gaushala, which is ostensibly farther than P.B.

Hmm, still thinking that dist(stop) < K * dist(nearestStop), K ~ 2 is the best, despite edge-case limitations...

Vivekbhusal commented 12 years ago

<?php

$currentlat= 29.776669; $currentlng = 81.2518833;

$connection=mysql_connect('localhost','root',''); mysql_select_db("niporg",$connection);

$q = 'select lat,lng from location'; $result = mysql_query($q,$connection); $numrows =mysql_num_rows($result); while($row = mysql_fetch_array($result)) { $database_lat[]= $row['lat']; $database_lng[]=$row['lng']; }

$var_averagedist2=10000000; $var_averagedist1=9999999; $indexvalue2 = 0; $indexvalue1 = 0;

for($i=0; $i<$numrows; $i++) { $averagedistance = Dist($currentlat,$currentlng, $database_lat[$i],$database_lng[$i]);

    if($averagedistance<$var_averagedist1)
    {   
        $var_averagedist2=$var_averagedist1;
        $indexvalue2 =$indexvalue1;

        $var_averagedist1=$averagedistance;
        $indexvalue1 = $i;
    }
    else if($averagedistance<$var_averagedist2)
    {
        $var_averagedist2=$averagedistance;
        $indexvalue2 =$i;
    }

} $output=array($indexvalue1, $database_lat[$indexvalue1], $database_lng[$indexvalue2], $indexvalue2, $database_lat[$indexvalue2], $database_lng[$indexvalue2]);

print(json_encode($output));

?>

<?php function Dist($LatA, $LngA, $LatB, $LngB) { $R = 6371;
$dLat = rad( $LatB - $LatA ); $dLong = rad( $LngB - $LngA );

$a = asin($dLat/2) * asin($dLat/2) + acos(rad($LatA)) * acos(rad($LatB)) * asin($dLong/2) * asin($dLong/2); $c = 2 * atan2(sqrt($a), sqrt(1-$a)); $d = $R * $c;

return $d;
}

function rad($x) { return $x*M_PI/180; }

?>

hope this works...took long time to solve this...

samrakchan commented 12 years ago

@Vivekbhusal the above code has used Haversine formular for calculating distance between to point, we can do this easily form SQL itself. But here we have logical problem rather than implementation.