geo_mysql_spatial_query_distance_sphere

geo/db/mysql_spatial.inc, line 78

Versions
6
geo_mysql_spatial_query_distance_sphere($field, $srid, $point)

Calculate the distance using the Great Circle Distance Formula. TODO only works for points - the X() and Y() functions should be supplanted by something that's cognizant of other geometries.

Code

<?php
function geo_mysql_spatial_query_distance_sphere($field, $srid, $point) {
  // Radius of the earth in meters.
  $r = 6370986;

  // Degree offset.
  $d = 57.2958;

  // Point data.
  $x = $point['lon'];
  $y = $point['lat'];

  // Offsets, in meters.
  $ysin = sin($y / $d);
  $ycos = cos($y / $d);
  $xd   = $x/$d;

  return "($r * ACOS($ysin * SIN(Y($field)/$d) + $ycos * COS(Y($field)/$d) * COS(X($field)/$d - $xd)))";
}
?>