Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Location from IP, geocoding, distance calculation, and geo/map utilities
Ruby

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.document
.gitignore
.rspec
Gemfile
Gemfile.lock
LICENSE.txt
README.textile
Rakefile
VERSION
geo_magic.gemspec

README.textile

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).

Geocoder adapters

I have also just created a Geocode adapter for Geocode and Graticule (see: geocoder_spec.rb) – This can also easily be configured for Rails.

I recently discovered that the Geocode adapter didn’t work for all addresses, so I made some improvements to make it more “stable”.
I’m sure there are still corner cases it wont handle, so please join in the effort to fix this!

Also note that the API has changed a little bit, so that it uses the same GeoMagic namespace as the rest of geo_magic.

Create random points in Radius

  • create_points_in_square
  • create_points_in_circle

Points within an area or distance

Within distance:

@points.as_map_points.within_distance 4.km, :from => @center_point

The closest n points:

@points.as_map_points.the_closest 4, :from => @center_point

Or using container objects (objects containing a location):

@people.as_map_points.the_closest 4, :from => @house

Where @people is an array of Person and @house is an instance of House and where both House and Person has a #location, #to_point or #point method that returns an object
with latitude and longitude methods that each return a float.

Within bounding rectangle:

rectangle = GeoMagic::Rectangle.new(GeoMagic::Point.new(-115, 50), GeoMagic::Point.new(-100, 20))
# or
# rect = GeoMagic::Rectangle.create_from_coords lat1, long1, lat2, long2
@points.as_map_points.within_rectangle rect, :from => @center_point

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.

Something went wrong with that request. Please try again.