Giter Site home page Giter Site logo

geo-distance's Introduction

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.

geo-distance's People

Contributors

kristianmandrup avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

geo-distance's Issues

rpd method no longer works

Hi,

We just upgraded geo-distance from v. 1.2 to 2.2, and now we're seeing a failure where the rpd method no longer works, though I realize that haversine.rb hasn't changed in a very long time.

NoMethodError: undefined method `rpd' for 3.552713678800501e-14:Float
/Users/mpd/.rvm/gems/ruby-2.1.7/gems/geo-distance-0.2.2/lib/geo-distance/formula/haversine.rb:52:in `distance'

Too old version of sugar-high results in Stack level too deep error

Hi @kristianmandrup , I used this gem about 2 weeks ago, the below lists my application informations:

  • Rails v4.2.4
  • Ruby v2.0 (Also run in v2.2)

The problem is that I randomly got SystemStackError: stack level too deep error, I spent much time to debug it and I could not find out any problem source until I tried to customize the log for errors, and I luckily got one line:

I, [2016-01-10T09:13:28.528437 #29987]  INFO -- : stack level too deep
["/data/apps/djiplus/shared/bundle/ruby/2.0.0/gems/sugar-high-0.4.6.4/lib/sugar-high/string.rb:8"]

I also try to search related issues and got this report: kristianmandrup/sugar-high#17 .

I have the below line in my Gemfile:

gem 'geo-distance', '~> 0.2.0'

and bundler installed the related gems for me:

geo-distance (0.2.0)
      geo_point (~> 0.2.5)
      geo_units (~> 0.2.4.1)
    geo_calc (0.7.6)
      activesupport (>= 3.0.1)
      geo_units (~> 0.2.1)
      i18n
      require_all (~> 1.2.0)
      sugar-high (~> 0.4.6.3)
    geo_point (0.2.5)
      geo_calc (~> 0.7.5)
      geo_units (~> 0.2.1)
    geo_units (0.2.4.1)
      sugar-high (~> 0.4.6.2)
    sugar-high (0.4.6.4)

See? the installed version of sugar-high is 0.4.6.4, which is the version mentioned in issue report kristianmandrup/sugar-high#17 .

I guess the reason why I got the error is same to one in the above report. Maybe you should update to the latest versions of your denpendices, that is, geo_calc and geo_units, etc... ?

About half of methods don't seem to work

Ex:

    users = base.select{|x|GeoDistance::Haversine.geo_distance(mylat,mylong,x.lat,x.long).to_miles <= dist}

undefined method `geo_distance' for GeoDistance::Haversine:Class

Change it to the .distance method? Nope:

undefined method `to_miles' for #<GeoDistance::Distance:0x000000038686e0 @distance=0.0>

So I do;

    users = base.select{|x|GeoDistance::Haversine.distance(mylat,mylong,x.lat,x.long).distance <= dist}

Of course now I'm stuck using radians. I guess I multiply the number * 630.0944 to get the right number? (3959 / 2*pi). Hell if I know.

Broken

Totally broken- not even the examples work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.