Spherical Law of Cosines different results between MySQL and PHP -


in our app using php calculate distance between coordinates using spherical law of cosines formula. though, isn't scaling well, refactored code calculate distance in mysql.
when compare results between php calculation , mysql calculation, see major differences.

lat       | lng        | php     | mysql 47.457131 | -10.615383 | 1112.8  | 7604.07 61.265367 | 19.939854  | 1508.5  | 6905.3 56.64524  | -21.312707 | 1745.6  | 8589.3 37.028064 | 18.732122  | 1957.5  | 4954.33 38.823754 | -17.029437 | 2112    | 7856.57 70.687405 | 11.08298   | 2217.1  | 7969.53 

all these distances calculated between these coordinates , (lat: 51.0363432, lng: 3.7351858).

this how calculated in php

public function getdistance($longitude, $latitude) {     $pi = pi();     $dist = (             (             acos(                 sin($latitude * $pi / 180)                 *                 sin($this->latitude * $pi / 180)                 +                 cos($latitude * $pi / 180)                 *                 cos($this->latitude * $pi / 180)                 *                 cos(($longitude - $this->longitude) * $pi / 180)             )             ) * 180 / $pi         ) * 60 * 1.1515;      $metric = $dist / 0.62137;     return $metric; } 

this how it's calculated in sql

public static function getdistancequery($lat, $lng, $distance_name, $units) {     /*      *  allow changing of units of measurement      */     switch ($units) {         case 'mi':             //radius of great circle in miles             $gr_circle_radius = 3959;             break;         default:             //radius of great circle in kilometers             $gr_circle_radius = 6371;             break;      }      /*      *  generate select field distance      */     $distance_select = sprintf(         "round(             ( %d                 * acos(                     cos( radians(%s) )                     * cos( radians( latitude ) )                     * cos( radians( longitude ) - radians(%s) )                     + sin( radians(%s) )                     * sin( radians( latitude ) )                 )             )         , 2 )         %s ",         $gr_circle_radius,         $lat,         $lng,         $lat,         $distance_name     );     return $distance_select; } 

are these big differences due higher precision in sql, or there wrong 1 of codes?

i found issue, should've done math hand before assuming. latitude , longitude flipped function mysql code being called.
results close each other now, small decimal differences.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -