Permalink
Browse files

Further fix of distance calculation, this time in SQL. Now uses least…

…() function, which is available in MySQL version 3.22.5+ and postgres versions 8.1+

Made it version 1.0.1

git-svn-id: http://geokit.rubyforge.org/svn/trunk@46 9265c765-0211-4c68-b2df-6d1bd6e20c4d
  • Loading branch information...
1 parent c4ec608 commit e65a8ed1e7f7d72a6e075cc425defcca1e233c23 lewisac committed Jan 21, 2008
Showing with 17 additions and 6 deletions.
  1. +6 −0 README
  2. +4 −0 VERSION_HISTORY.txt
  3. +1 −1 about.yml
  4. +4 −4 lib/geo_kit/acts_as_mappable.rb
  5. +2 −1 lib/geo_kit/geocoders.rb
View
6 README
@@ -409,6 +409,12 @@ If you need to sort things post-query, you can do so:
Obviously, each of the items in the array must have a latitude/longitude so
they can be sorted by distance.
+Database Compatability
+=================================================================================
+GeoKit does *not* work with SQLite, as it lacks the necessary geometry functions.
+GeoKit works with MySQL (tested with version 5.0.41) or PostgreSQL (tested with version 8.2.6)
+GeoKit is known to *not* work with Postgres <8.1 -- it uses the least() funciton.
+
HIGH-LEVEL NOTES ON WHAT'S WHERE
=================================================================================
View
@@ -0,0 +1,4 @@
+01/20/08 Version 1.0.1. Further fix of distance calculation, this time in SQL. Now uses least() function, which is available in MySQL version 3.22.5+ and postgres versions 8.1+
+01/16/08 fixed the "zero-distance" bug (calculating between two points that are the same)
+12/11/07 fixed a small but with queries crossing meridian, and also fixed find(:closest)
+10/11/07 Fixed Rails2/Edge compatability
View
@@ -4,6 +4,6 @@ author:
name_2: Andre Lewis
homepage_2: http://www.earthcode.com
summary: Geo distance calculations, distance calculation query support, geocoding for physical and ip addresses.
-version: 1.0
+version: 1.0.1
rails_version: 1.0+
license: MIT
@@ -380,15 +380,15 @@ def sphere_distance_sql(origin, units)
case connection.adapter_name.downcase
when "mysql"
sql=<<-SQL_END
- (ACOS(COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+
+ (ACOS(least(1,COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+
COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+
- SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name})))*#{multiplier})
+ SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier})
SQL_END
when "postgresql"
sql=<<-SQL_END
- (ACOS(COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+
+ (ACOS(least(1,COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+
COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+
- SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name})))*#{multiplier})
+ SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier})
SQL_END
else
sql = "unhandled #{connection.adapter_name.downcase} adapter"
View
@@ -176,7 +176,8 @@ def self.do_geocode(address)
res.street_address = doc.elements['//ThoroughfareName'].text if doc.elements['//ThoroughfareName']
# Translate accuracy into Yahoo-style token address, street, zip, zip+4, city, state, country
# For Google, 1=low accuracy, 8=high accuracy
- address_details=doc.elements['//AddressDetails','urn:oasis:names:tc:ciq:xsdschema:xAL:2.0']
+ # old way -- address_details=doc.elements['//AddressDetails','urn:oasis:names:tc:ciq:xsdschema:xAL:2.0']
+ address_details=doc.elements['//*[local-name() = "AddressDetails"]']
accuracy = address_details ? address_details.attributes['Accuracy'].to_i : 0
res.precision=%w{unknown country state state city zip zip+4 street address}[accuracy]
res.success=true

0 comments on commit e65a8ed

Please sign in to comment.