You are here

function geofield_haversine in Geofield 7.2

Same name and namespace in other branches
  1. 8 geofield.module \geofield_haversine()

Haversine formula, useful for injecting into an SQL statement. In instances where it isn't possible to pass in variables dynamically (i.e. field definitions), this function will bake in values directly into the snippet.

Parameters

$options: An array of parameters that can be passed along to be injected directly into the SQL snippet. The following array keys are checked...

  • origin_latitude
  • origin_longitude
  • destination_latitude
  • destination_longitude
  • earth_radius

Return value

A string suitable for injection into DBTNG. Any option passed into the function will be baked into the string directly. Any variable missing will be represented as :[variable]. (i.e. :earth_radius).

5 calls to geofield_haversine()
geofield_handler_argument_proximity::query in views/handlers/geofield_handler_argument_proximity.inc
Set up the where clause for the contextual filter argument.
geofield_handler_field::query in views/handlers/geofield_handler_field.inc
Called to add the field to a query.
geofield_handler_filter::op_between in views/handlers/geofield_handler_filter.inc
geofield_handler_filter::op_simple in views/handlers/geofield_handler_filter.inc
geofield_handler_sort::query in views/handlers/geofield_handler_sort.inc
Called to add the sort to a query.

File

./geofield.module, line 688

Code

function geofield_haversine($options = array()) {
  $formula = '( :earth_radius * ACOS( COS( RADIANS(:origin_latitude) ) * COS( RADIANS(:destination_latitude) ) * COS( RADIANS(:destination_longitude) - RADIANS(:origin_longitude) ) + SIN( RADIANS(:origin_latitude) ) * SIN( RADIANS(:destination_latitude) ) ) )';
  foreach ($options as $key => $option) {
    if (is_numeric($option)) {
      $formula = str_replace(':' . $key, $option, $formula);
    }
    else {
      $formula = str_replace(':' . $key, db_escape_field($option), $formula);
    }
  }
  return $formula;
}