Geo Magic

This little clever project was the result of me not being satisfied with existing Geo Location and Coding solutions out there to suit my needs. Many of the projects did not return quality data (fx when requesting IP or location) and others were tightly bound to Active Record and SQL databases for calculations etc.

This little gem should get you going…

  • Get IP and location data using freegeoip.net
  • Calculate of distance between map points using haversine (supports multiple distance units)

You can either include the complete library like this:

require 'geo_magic'

Or require only the functionality you need (see usage examples below)

IP and location data

Uses freegeoip.net JSON API

RSpec 2 examples of API usage

require 'geo_magic/remote'

it "should get the remote IP address" do
  ip = GeoMagic::Remote.my_ip    
  puts "ip: #{ip.inspect}"    
end

it "should get other location" do
  location = GeoMagic::Remote.location_of '74.200.247.59'
  puts "location: #{location['city']}"    
end

it "should get my location" do
  location = GeoMagic::Remote.my_location
  puts location
  puts "location: #{location['city']}"    
end  

Distance calculation

RSpec 2 examples of API usage

require 'geo_magic/calculate'

it "calculates distance using array args" do
  dist = GeoMagic::Calculate.distance [@long1, @lat1], [@long2, @lat2]    
  puts dist    
end

it "calculates distance using Point args" do
  from_point = GeoMagic::Point.new @long1, @lat1    
  to_point = GeoMagic::Point.new @long2, @lat2

  puts "from: #{from_point}, to: #{to_point}"

  dist = GeoMagic::Calculate.distance from_point, to_point
  puts dist    
end

it "calculates distance using Hash args (short)" do
  from_point = GeoMagic::Point.new(@long1, @lat1).to_hash :short
  to_point = GeoMagic::Point.new(@long2, @lat2).to_hash :short

  puts "from: #{from_point}, to: #{to_point}"

  dist = GeoMagic::Calculate.distance from_point, to_point
  puts dist    
end  

Extra features

Lots of extra features has been added recently. For now, please check out the specs to get an idea of the features included. Most recently I have added support to filter a list of points for those who are within a radius of another point (see select_neares_spec.rb). I have also just created a Geocode adapter for Geocode and Graticule (see: geocoder_spec.rb) – This can also easily be configured for Rails.

Create random points in Radius

create_points_in_square works, but create_points_in_circle is currently broken. Please help fix this ASAP ;) Thanks!

Meta magic

You can also include the functionality directly into any class like this


class Map
  include GeoMagic::Calculate
end

class MapCalc
  geo_magic :calc
end

it "calculates distance using array args" do
  dist = Map.distance [@long1, @lat1], [@long2, @lat2]    
  puts dist    
end

it "calculates distance using array args" do
  dist = MapCalc.distance [@long1, @lat1], [@long2, @lat2]    
  puts dist    
end  

Contributing to geo_magic

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
  • 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.