Skip to content
Snippets Groups Projects
Commit 53fdf15e authored by Alex Reisner's avatar Alex Reisner
Browse files

Bump gem version to 0.9.9.

Update gemspec: summary, description, and files. Also add json gem
dependency and update CHANGELOG and README with lots of new features!
parent 61629efd
No related branches found
Tags 0.9.9
No related merge requests found
......@@ -2,6 +2,21 @@
Per-release changes to Geocoder.
== 0.9.9 (2011 Mar 9)
* Add support for IP address geocoding via FreeGeoIp.net.
* Add support for Yahoo PlaceFinder geocoding API.
* Add support for custom geocoder data handling by passing a block to geocoded_by or reverse_geocoded_by.
* Add <tt>Rack::Request#location</tt> method for geocoding user's IP address.
* Change gem name to geocoder (no more rails-geocoder).
* Gem now works outside of Rails.
* DEPRECATION: +fetch_coordinates+ no longer takes an argument.
* DEPRECATION: +fetch_address+ no longer takes an argument.
* DEPRECATION: Geocoder.search now returns a single result instead of an array.
* DEPRECATION: <tt>fetch_coordinates!</tt> has been superceded by +geocode+ (then save your object manually).
* DEPRECATION: <tt>fetch_address!</tt> has been superceded by +reverse_geocode+ (then save your object manually).
* Fix: don't die when trying to get coordinates with a nil address (github.com/zmack).
== 0.9.8 (2011 Feb 8)
* Include geocode:all Rake task in gem (was missing!).
......@@ -13,13 +28,13 @@ Per-release changes to Geocoder.
== 0.9.7 (2011 Feb 1)
* Add reverse geocoding (+reverse_geocoded_by+).
* Prevent exception (uninitialized constant Geocoder::Net) when net/http not already required (sleepycat).
* Prevent exception (uninitialized constant Geocoder::Net) when net/http not already required (github.com/sleepycat).
* Refactor: split monolithic Geocoder module into several smaller ones.
== 0.9.6 (2011 Jan 19)
* Fix incompatibility with will_paginate gem.
* Include table names in GROUP BY clause of nearby scope to avoid ambiguity in joins (matchu).
* Include table names in GROUP BY clause of nearby scope to avoid ambiguity in joins (github.com/matchu).
== 0.9.5 (2010 Oct 15)
......@@ -40,7 +55,7 @@ Per-release changes to Geocoder.
== 0.9.2 (2010 Jun 3)
* Fix LIMIT clause bug in PostgreSQL (reported by kenzie).
* Fix LIMIT clause bug in PostgreSQL (reported by github.com/kenzie).
== 0.9.1 (2010 May 4)
......@@ -48,7 +63,7 @@ Per-release changes to Geocoder.
== 0.9.0 (2010 Apr 2)
* Fix bug in PostgreSQL support (caused "PGError: ERROR: column "distance" does not exist"), reported by developish.
* Fix bug in PostgreSQL support (caused "PGError: ERROR: column "distance" does not exist"), reported by github.com/developish.
== 0.8.9 (2010 Feb 11)
......
= Geocoder
Geocoder adds object geocoding and database-agnostic distance calculations to Ruby on Rails. It's as simple as calling <tt>fetch_coordinates!</tt> on your objects, and then using a scope like <tt>Venue.near("Billings, MT")</tt>. Since it does not rely on proprietary database functions finding geocoded objects in a given area works with out-of-the-box MySQL or even SQLite.
Geocoder is a complete geocoding solution for Ruby. With Rails it adds object geocoding (by street or IP address), reverse geocoding (find street address based on given coordinates), and distance calculations to Ruby on Rails. It's as simple as calling +geocode+ on your objects, and then using a scope like <tt>Venue.near("Billings, MT")</tt>. Since it does not rely on proprietary database functions finding geocoded objects in a given area works with out-of-the-box PostgreSQL, MySQL, and even SQLite.
Geocoder is compatible with Rails 2.x and 3.x. <b>This is the README for the 3.x branch.</b> Please see the 2.x branch for installation instructions, documentation, and issues.
== Compatibility
== 1. Install
Geocoder is compatible with Rails 3. If you need to use it with Rails 2 please see the <tt>rails2</tt> branch (no longer maintained, limited feature set).
== Install
=== As a Gem
Add this to your Gemfile:
Add to your Gemfile:
gem "geocoder"
and run this at the command prompt:
and run at the command prompt:
bundle install
......@@ -24,39 +27,35 @@ At the command prompt:
rails plugin install git://github.com/alexreisner/geocoder.git
== 2. Configure
A) Add +latitude+ and +longitude+ columns to your model:
rails generate migration AddLatitudeAndLongitudeToYourModel latitude:float longitude:float
rake db:migrate
== Configure Object Geocoding
B) Tell geocoder where your model stores its address:
=== Required Attributes
geocoded_by :address
Your object must have two attributes (database columns) for storing latitude and longitude coordinates. By default they should be called +latitude+ and +longitude+ but this can be changed (see "More on Configuration" below):
C) Optionally, auto-fetch coordinates every time your model is saved:
rails generate migration AddLatitudeAndLongitudeToModel latitude:float longitude:float
rake db:migrate
after_validation :fetch_coordinates
For reverse geocoding your model must provide a method that returns an address. This can be a single attribute, but it can also be a method that returns a string assembled from different attributes (eg: +city+, +state+, and +country+).
<i>Note that you are not stuck with the +latitude+ and +longitude+ column names, or the +address+ method. See "More On Configuration" below for details.</i>
=== Model Behavior
In your model, tell Geocoder which method returns your object's full address:
== 3. Use
geocoded_by :full_street_address # can also be an IP address
after_validation :geocode # auto-fetch coordinates
Assuming +obj+ is an instance of a geocoded class, you can get its coordinates:
For reverse geocoding, tell Geocoder which methods return latitude and longitude:
obj.fetch_coordinates # fetches and assigns coordinates
obj.fetch_coordinates! # also saves lat, lon attributes
reverse_geocoded_by :lat, :lon
after_validation :reverse_geocode # auto-fetch address
If you have a lot of objects you can use this Rake task to geocode them all:
If you have just added geocoding to a class and have a lot of existing objects you can use this Rake task to geocode them all:
rake geocode:all CLASS=YourModel
Once +obj+ is geocoded you can do things like this:
obj.nearbys(30) # other objects within 30 miles
obj.distance_to(40.714, -100.234) # distance to arbitrary point
== Location-Aware Database Queries
To find objects by location, use the following scopes:
......@@ -65,67 +64,121 @@ To find objects by location, use the following scopes:
Venue.geocoded # venues with coordinates
Venue.not_geocoded # venues without coordinates
Some utility methods are also available:
With geocoded objects you can do things like this:
# distance (in miles) between Eiffel Tower and Empire State Building
Geocoder::Calculations.distance_between( 48.858205,2.294359, 40.748433,-73.985655 )
obj.nearbys(30) # other objects within 30 miles
obj.distance_to(40.714, -100.234) # distance from object to arbitrary point
Some utility methods are also available:
# look up coordinates of some location (like searching Google Maps)
Geocoder::Lookup.coordinates("25 Main St, Cooperstown, NY")
Geocoder.coordinates("25 Main St, Cooperstown, NY")
=> [42.700149, -74.922767]
# distance (in miles) between Eiffel Tower and Empire State Building
Geocoder::Calculations.distance_between( 47.858205,2.294359, 40.748433,-73.985655 )
=> 3619.77359999382
# find the geographic center (aka center of gravity) of objects or points
Geocoder::Calculations.geographic_center([ city1, city2, city3, [40.22,-73.99], city4 ])
=> [35.14968, -90.048929]
Please see the code for more methods and detailed information about arguments (eg, working with kilometers).
== More On Configuration
== More on Configuration
You are not stuck with using the +latitude+ and +longitude+ database column names for storing coordinates. For example, to use +lat+ and +lon+:
geocoded_by :address, :latitude => :lat, :longitude => :lon
The string to use for geocoding can be anything you'd use to search Google Maps. For example, any of the following are acceptable:
The +address+ method can return any string you'd use to search Google Maps. For example, any of the following are acceptable:
714 Green St, Big Town, MO
Eiffel Tower, Paris, FR
Paris, TX, US
* "714 Green St, Big Town, MO"
* "Eiffel Tower, Paris, FR"
* "Paris, TX, US"
If your model has +address+, +city+, +state+, and +country+ attributes you might do something like this:
If your model has +street+, +city+, +state+, and +country+ attributes you might do something like this:
geocoded_by :location
geocoded_by :address
def location
[address, city, state, country].compact.join(', ')
def address
[street, city, state, country].compact.join(', ')
end
Please see the code (<tt>lib/geocoder/active_record.rb</tt>) for more methods and detailed information about arguments (eg, working with kilometers).
For reverse geocoding you can also specify an alternate name attribute where the address will be stored, for example:
You can also set the timeout used for connections to Google's geocoding service. The default is 3 seconds, but if you want to set it to 5 you could put the following in an initializer:
reverse_geocoded_by :lat, :lon, :address => :location
Geocoder::Configuration.timeout = 5
== Advanced Geocoding
== Reverse Geocoding
So far we have looked at shortcuts for assigning geocoding results to object attributes. However, if you need to do something fancy you can skip the auto-assignment by providing a block (takes the object to be geocoded and a <tt>Geocoder::Result</tt> object) in which you handle the parsed geocoding result any way you like, for example:
If you need reverse geocoding (lat/long coordinates to address), do something like the following in your model:
reverse_geocoded_by :lat, :lon do |obj,geo|
obj.city = geo.city
obj.zipcode = geo.postal_code
obj.country = geo.country_code
end
after_validation :reverse_geocode
reverse_geocoded_by :latitude, :longitude
after_validation :fetch_address
Every <tt>Geocoder::Result</tt> object, +result+, provides the following data:
and make sure it has +latitude+ and +longitude+ attributes, as well as an +address+ attribute. As with regular geocoding, you can specify alternate names for all of these attributes, for example:
* <tt>result.latitude # float</tt>
* <tt>result.longitude # float</tt>
* <tt>result.coordinates # array of the above two</tt>
* <tt>result.address # string</tt>
* <tt>result.city # string</tt>
* <tt>result.postal_code # string</tt>
* <tt>result.country_name # string</tt>
* <tt>result.country_code # string</tt>
reverse_geocoded_by :lat, :lon, :address => :location
and if you're familiar with the results returned by the geocoding service you're using, you can access even more (see code comments for details: <tt>lib/geocoder/results/*</tt>).
== Geocoding Services
By default Geocoder uses Google's geocoding API to fetch coordinates and addresses. However if you wish to use Yahoo's geocoding API you can simply add this to an initializer:
# config/initializers/geocoder.rb
Geocoder::Configuration.lookup = :yahoo
Geocoder::Configuration.yahoo_appid = "..."
To obtain a Yahoo app id go to:
https://developer.apps.yahoo.com/wsregapp
Note that the result objects returned by different geocoding services all implement the methods listed above. Beyond that, however, you must be familiar with your particular subclass of <tt>Geocoder::Result</tt> and the geocoding service's result structure:
Google: http://code.google.com/apis/maps/documentation/geocoding/#JSON
Yahoo: http://developer.yahoo.com/geo/placefinder/guide/responses.html
FreeGeoIP: http://github.com/fiorix/freegeoip/blob/master/README.rst
=== Timeouts
You can set the timeout used for connections to the geocoding service. The default is 3 seconds but if you want to set it to 5, for example, put the following in an initializer:
# config/initializers/geocoder.rb
Geocoder::Configuration.timeout = 5
== Forward and Reverse Geocoding in the Same Model
If you apply both forward and reverse geocoding functionality to the same model, you can provide different methods for storing the fetched address (reverse geocoding) and providing an address to use when fetching coordinates (forward geocoding), for example:
If you apply both forward and reverse geocoding functionality to the same model, you will provide two address methods:
* one for storing the fetched address (reverse geocoding)
* one for providing an address to use when fetching coordinates (forward geocoding)
For example:
class Venue
# build an address from street, city, and state attributes
geocoded_by :address_from_components
# store the Google-provided address in the full_address attribute
# store the fetched address in the full_address attribute
reverse_geocoded_by :latitude, :longitude, :address => :full_address
end
......@@ -143,38 +196,26 @@ However, there can be only one set of latitude/longitude attributes, and whichev
The reason for this is that we don't want ambiguity when doing distance calculations. We need a single, authoritative source for coordinates!
== Getting More Information
== Request Geocoding by IP Address
Geocoder adds a +location+ method to the standard <tt>Rack::Request</tt> object so you can easily look up the location of any HTTP request by IP address. For example, in a Rails controller or a Sinatra app:
Those familiar with Google's Geocoding API know that it returns much more information than just an address or set of coordinates. If you want access to the entire response you can use the <tt>Geocoder.search</tt> method:
# returns Geocoder::Result object
result = request.location
results = Geocoder.search("McCarren Park, Brooklyn, NY")
r = results.first
+r+ is now a Geocoder::Result object which has methods like the following:
== Use Outside of Rails
r.geometry
=> {
"location"=>{"lng"=>-79.3801601, "lat"=>43.6619568},
"location_type"=>"ROOFTOP",
"viewport"=>{
"northeast"=>{"lng"=>-79.3770125, "lat"=>43.6651044},
"southwest"=>{"lng"=>-79.3833077, "lat"=>43.6588092}
}
}
You can use Geocoder outside of Rails by calling the <tt>Geocoder.search</tt> method:
r.address_components_of_type(:neighborhood)
=> [{
"long_name"=>"Greenpoint",
"short_name"=>"Greenpoint",
"types"=>["neighborhood", "political"]
}]
result = Geocoder.search("McCarren Park, Brooklyn, NY")
Please see the Geocoder::Result class for more information, as well as Google's API documentation (http://code.google.com/apis/maps/documentation/geocoding/#JSON).
This returns a <tt>Geocoder::Result</tt> object with all information provided by the geocoding service. Please see above and in the code for details.
== SQLite
== Distance Queries in SQLite
SQLite's lack of trigonometric functions requires an alternate implementation of the +near+ method (scope). When using SQLite, Geocoder will automatically use a less accurate algorithm for finding objects near a given point. Results of this algorithm should not be trusted too much as it will return objects that are outside the given radius.
SQLite's lack of trigonometric functions requires an alternate implementation of the +near+ scope. When using SQLite, Geocoder will automatically use a less accurate algorithm for finding objects near a given point. Results of this algorithm should not be trusted too much as it will return objects that are outside the given radius.
It is also not possible to calculate distances between points without the trig functions so you cannot sort results by "nearness."
......@@ -204,10 +245,8 @@ If anyone has a more elegant solution to this problem I am very interested in se
== To-do List
* support different ORMs (DataMapper, Mongoid, etc)
* use completely separate "drivers" for different AR adapters?
* seems reasonable since we're using very DB-specific features
* also need to make sure 'mysql2' is supported
* add support for DataMapper
* add support for Mongoid
* make 'near' scope work with AR associations
* http://stackoverflow.com/questions/3266358/geocoder-rails-plugin-near-search-problem-with-activerecord
......
0.9.8
0.9.9
......@@ -7,10 +7,9 @@ Gem::Specification.new do |s|
s.email = ["alex@alexreisner.com"]
s.homepage = "http://github.com/alexreisner/geocoder"
s.date = Date.today.to_s
s.summary = "Simple, database-agnostic geocoding and distance calculations for Rails."
s.description = "Geocoder adds object geocoding and distance calculations to ActiveRecord models. It does not rely on proprietary database functions so finding geocoded objects in a given area is easily done using out-of-the-box MySQL, PostgreSQL, or SQLite."
s.files = Dir.glob("{lib,lib/geocoder,lib/tasks,test}/*") + %w(CHANGELOG.rdoc Rakefile README.rdoc LICENSE)
s.summary = "Complete geocoding solution for Ruby."
s.description = "Provides object geocoding (by street or IP address), reverse geocoding (coordinates to street address), and distance calculations for geocoded objects. Designed for Rails but works with other frameworks too."
s.files = `git ls-files`.split("\n") - %w[geocoder.gemspec Gemfile init.rb]
s.require_paths = ["lib"]
s.add_dependency 'json', '>= 1.0.0'
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment