Shortest distance from GPS point to a great circle inconsistency

Multi tool use


Shortest distance from GPS point to a great circle inconsistency
I have followed a few links form both here on stackoverflow and also around the internet and have in turn created some functions for crosstrack distance. However i think i am missing something as the calculations are inconsistent.
From the following link https://www.movable-type.co.uk/scripts/latlong.html shown in another answer here Minimum distance between a point and a line in latitude, longitude i have created what i thought were the functions needed for the calculation as shown below.
public function distanceTo($from, $to) {
//Calculate point differences
$latc = floatval($to->getRadialLatitude() - $from->getRadialLatitude());
$lngc = floatval($to->getRadialLongitude() - $from->getRadialLongitude());
//Calculate a
$a = sin($latc/2) * sin($latc/2) + cos($from->getRadialLatitude()) *
cos($to->getRadialLatitude()) * sin($lngc/2) * sin($lngc/2);
//Calculate c
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
//Calculate d
$d = 6371000 * $c;
return $d;
}
public function bearingTo($pa, $pb) {
//Convert to rads
$lata = $pa->getRadialLatitude();
$latb = $pb->getRadialLatitude();
$lngc = floatval(($pb->getDecimalLongitude() - $pa->getDecimalLongitude()) * (pi() / 180));
//Calculate y
$y = sin($lngc) * cos($latb);
//Calulate x
$x = cos($lata) * sin($latb) - sin($lata) * cos($latb) * cos($lngc);
//Calculate 0
$b = atan2($y, $x) * (180 / pi());
return fmod(($b + 360), 360);
}
public function crossTrackDistance($pc, $pa, $pb) {
$a = floatval($this->distanceTo($pa, $pc) / 6371000);
$b = floatval($this->bearingTo($pa, $pc) * (pi() / 180));
$c = floatval($this->bearingTo($pa, $pb) * (pi() / 180));
$d = asin(sin($a) * sin($b - $c));
return $d * 6371000;
}
These functions return the correct value for certain point line combinations for example,
point -37.756047854746626,175.37913657724857
line-37.75967614891693,175.38304656744003
-37.759917887500336,175.38264960050583
which is returning 529.37651757282m
which seems okay as an estimation using google maps shows the distance as about 530m.
529.37651757282m
However this is where my issue lies with the
point -38.66714197682136,176.14404257386923
and the
line -38.6661326,176.1452709,-38.6660406,176.1453836
i am getting a distance of 0.51372771654236m
which is obviously very very far off as it is around 150m
-38.66714197682136,176.14404257386923
-38.6661326,176.1452709,-38.6660406,176.1453836
0.51372771654236m
Any help as to where the issue is would be very helpful
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you looking to find the closest coordinates from any given co-ordinate? As an example, nearest location in the database from a certain xy?
– delboy1978uk
just now