From 79a829c5ea9954eff688dea12cc2e20fb7eff58d Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Tue, 22 Mar 2011 00:11:01 -0400 Subject: [PATCH] Make bearing calculation Postgres-compatible. Only worked with MySQL. --- lib/geocoder/orms/active_record.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/geocoder/orms/active_record.rb b/lib/geocoder/orms/active_record.rb index 8f724cf9..7b585940 100644 --- a/lib/geocoder/orms/active_record.rb +++ b/lib/geocoder/orms/active_record.rb @@ -88,17 +88,16 @@ module Geocoder::Orm bearing = "(DEGREES(ATAN2( " + "SIN(RADIANS(#{lon_attr} - #{longitude})) * " + "COS(RADIANS(#{lat_attr})), (" + - "COS(RADIANS(#{latitude})) * " + - "SIN(RADIANS(#{lat_attr}))) - " + - "(SIN(RADIANS(#{latitude})) * " + - "COS(RADIANS(#{lat_attr})) * " + - "COS(RADIANS(#{lon_attr} - #{longitude}))))) + 360) % 360" + "COS(RADIANS(#{latitude})) * SIN(RADIANS(#{lat_attr}))" + + ") - (" + + "SIN(RADIANS(#{latitude})) * COS(RADIANS(#{lat_attr})) * " + + "COS(RADIANS(#{lon_attr} - #{longitude}))" + + "))) + 360)" + bearing = "CAST(#{bearing} AS decimal) % 360" distance = "#{Geocoder::Calculations.earth_radius} * 2 * ASIN(SQRT(" + - "POWER(SIN((#{latitude} - #{lat_attr}) * " + - "PI() / 180 / 2), 2) + COS(#{latitude} * PI() / 180) * " + - "COS(#{lat_attr} * PI() / 180) * " + - "POWER(SIN((#{longitude} - #{lon_attr}) * " + - "PI() / 180 / 2), 2) ))" + "POWER(SIN((#{latitude} - #{lat_attr}) * PI() / 180 / 2), 2) + " + + "COS(#{latitude} * PI() / 180) * COS(#{lat_attr} * PI() / 180) * " + + "POWER(SIN((#{longitude} - #{lon_attr}) * PI() / 180 / 2), 2) ))" options[:order] ||= "#{distance} ASC" default_near_scope_options(latitude, longitude, radius, options).merge( :select => "#{options[:select] || '*'}, " + -- GitLab