0.06
No commit activity in last 3 years
No release in over 3 years
Calculates the geo distance between two locations using longitude and latitude using Haversine, Speherical or Vincenty formula. This is done using Math formulas without resorting to Active Record or SQL DB functionality
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.1
>= 1.6.2
>= 2.6.0

Runtime

~> 0.2.6
~> 0.3.3
 Project Readme

GeoDistance – geo distance calculations

Calculates the Geo distance between two locations using longitude and latitude.
This is done using “pure” Math formulas without resorting to Active Record or SQL DB functionality

This gem is meant to be a replacement for those geo distance utils out there that use built in SQL DB functionality for their calculations!

The formulas curently supported are

  • Haversine
  • Spherical
  • Vincenty
  • Flat

Install & Usage

require 'geo-distance'

it "should work" do
  lon1 = -104.88544
  lat1 = 39.06546

  lon2 = -104.80
  lat2 = lat1

  dist = GeoDistance.distance( lat1, lon1, lat2, lon2 ) # in radians  

  dist = GeoDistance::Haversine.geo_distance( lat1, lon1, lat2, lon2 ).to_meters

  # Change to (lng, lat) mode
  GeoPoint.coord_mode = :lng_lat

  p1, p2 = [[lon1, lat1].geo_point, [lon2, lat2].geo_point]

  dist = GeoDistance::Haversine.geo_distance( lon1, lat1, lon2, lat2)
  dist = GeoDistance::Haversine.geo_distance( p1, p2).to_miles

  puts "the distance from  #{lat1}, #{lon1} to #{lat2}, #{lon2} is: #{dist.meters} meters"

  dist.feet
  dist.meters
  dist.kms
  dist.miles
end  

Distance API

The call to distance returns an instance of GeoDistance

dist = GeoDistance::Haversine.distance( lat1, lon1, lat2, lon2 )

The #kms, #meters, #miles and #feet methods return an instance og GeoUnit

dist.kms

Setting default algorithm

You can also set a default algorithm to use…
The following will use the Haversine algorithm:

  GeoDistance.default_algorithm = :haversine
  dist = GeoDistance.distance( lat1, lon1, lat2, lon2 )  

GeoDistance is used in the geo_magic gem

Contributing to geo-distance

  • Check out the specs and add specs to spec any added features or changes!
  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright © 2011 Kristian Mandrup. See LICENSE.txt for
further details.