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…
Some of the main features:
- Get IP and location data using freegeoip.net
- Calculate distance between map points using various algorithms (haversine, spherical, vincenty, straight line)
- Geocoding and Reverse Geocoding using adapters (currently: Graticule and Geocode gems)
- Calculate nearest points from a point
- Find points within a radius (circle or square from center)
- Calculate points within a bounding box
You can either include the complete library like this:
require 'geo_magic'
Or require only the functionality you need (see usage examples below)
I am currently in the process of refactoring and improving this library in a major way in the refactor branch. You are most welcome to help out!
The new version coming soon will be truly awesome and magical with a very powerful DSL… stay tuned!
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
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
The following Geocode adapters are supported
- Geocode
- Graticule
- GraticuleMulti
Single Geocode Geocoder:
# Use Graticule Multi geocoder, which tries multiple geocode services in succession! geocoder = GeoMagic.geo_coder(:type => :geocode) geocoder.configure File.expand_path('../fixtures/map_api_keys.yaml', File.dirname(__FILE__)), :development location = geocoder.instance.geocode "Mullerstrasse 9, Munich" location.city
Single Graticule Geocoder:
# Use Graticule Multi geocoder, which tries multiple geocode services in succession! geocoder = GeoMagic.geo_coder(:type => graticule) geocoder.configure File.expand_path('../fixtures/map_api_keys.yaml', File.dirname(__FILE__)), :development location = geocoder.instance.geocode "Mullerstrasse 9, Munich" location.city
Multi Graticule Geocoder:
# Use Graticule Multi geocoder, which tries multiple geocode services in succession! geocoder = GeoMagic.geo_coder(:type => graticule_multi) ..
Multi Graticule Customization:
# Use Graticule Multi geocoder, which tries multiple geocode services in succession! geocoder = GeoMagic.geo_coder(:type => graticule_multi) location = geocoder.instance(:timeout => 3) do |result| [:address, :street].include?(result.precision)] end ..
Geocoder in Rails:
geocoder = GeoMagic.geo_coder(:env => :rails).configure location = geocoder.instance.geocode "Mullerstrasse 9, Munich" location.city.should == 'Munich'
By default, the configuration expects a file in config/map_api_keys.yml containing the keys for each environment (see spec/fictures folder).
This can be customized if needed: geocoder.configure '../my_keys.yaml'
From a given radius, you can create random points either in a square or circle radius.
- create_points_in_square
- create_points_in_circle
Example use:
radius = center_point.within(10.km) square_points = radius.create_points_in_square 6 circle_points = radius.create_points_in_circle 4
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
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
- 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 © 2011 Kristian Mandrup. See LICENSE.txt for
further details.