Skip to content

Commit

Permalink
Prepare for release of gem version 0.9.12.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreisner committed Apr 6, 2011
1 parent 2d07b5e commit 54329e5
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 56 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

Per-release changes to Geocoder.

== 0.9.12 (2011 Apr 6)

* Add support for Mongoid.
* Add bearing_to/from methods to geocoded objects.
* Improve SQLite's distance calculation heuristic.
* Fix: Geocoder::Calculations.geographic_center was modifying its argument in-place (reported by github.com/joelmats).
* Fix: sort 'near' query results by distance when using SQLite.
* Clean up input: search for coordinates as a string with space after comma yields zero results from Google. Now we get rid of any such space before sending the query.
* DEPRECATION: Geocoder.near should not take <tt>:limit</tt> or <tt>:offset</tt> options.
* DEPRECATION: Change argument format of all methods that take lat/lon as separate arguments. Now you must pass the coordinates as an array [lat,lon], but you may alternatively pass a address string (will look up coordinates) or a geocoded object (or any object that implements a to_coordinates method which returns a [lat,lon] array).

== 0.9.11 (2011 Mar 25)

* Add support for result caching.
Expand Down
157 changes: 103 additions & 54 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
= Geocoder

Geocoder is a complete geocoding solution for Ruby. With Rails it adds geocoding (by street or IP address), reverse geocoding (find street address based on given coordinates), and distance calculations for ActiveRecord objects. 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 a complete geocoding solution for Ruby. With Rails it adds geocoding (by street or IP address), reverse geocoding (find street address based on given coordinates), and distance queries. It's as simple as calling +geocode+ on your objects, and then using a scope like <tt>Venue.near("Billings, MT")</tt>.


== Compatibility

Geocoder has been successfully tested with Ruby (MRI) 1.8.7, 1.9.2, and JRuby 1.5.3.

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

Geocoder also works outside of Rails but you'll need to install either the +json+ (for MRI) or +json_pure+ (for JRuby) gem.
* Supports multiple Ruby versions: Ruby 1.8.7, 1.9.2, and JRuby.
* Supports multiple databases: MySQL, PostgreSQL, SQLite, and MongoDB (1.7.0 and higher).
* Supports Rails 3. If you need to use it with Rails 2 please see the <tt>rails2</tt> branch (no longer maintained, limited feature set).
* Works very well outside of Rails but you'll need to install either the +json+ (for MRI) or +json_pure+ (for JRuby) gem.


== Install
Expand All @@ -33,31 +32,69 @@ At the command prompt:

== Configure Object Geocoding

=== Required Attributes
In the below, note that addresses may be street or IP addresses.

=== ActiveRecord

*ActiveRecord:* 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):
Your model 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):

rails generate migration AddLatitudeAndLongitudeToModel latitude:float longitude:float
rake db:migrate

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

=== Model Behavior

In your model, tell Geocoder which method returns your object's full address:
Next, your model must tell Geocoder which method returns your object's geocodable address:

geocoded_by :full_street_address # can also be an IP address
after_validation :geocode # auto-fetch coordinates

For reverse geocoding, tell Geocoder which methods return latitude and longitude:
For reverse geocoding, tell Geocoder which attributes store latitude and longitude:

reverse_geocoded_by :lat, :lon
after_validation :reverse_geocode # auto-fetch address

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:
=== Mongoid

First, your model must have an array field for storing coordinates:

field :coordinates, :type => Array

You may also want an address field, like this:

field :address

but if you store address components (city, state, country, etc) in separate fields you can instead define a method called +address+ that combines them into a single string which will be used to query the geocoding service.

Once your fields are defined, include the <tt>Geocoder::Model::Mongoid</tt> module and then call <tt>geocoded_by</tt>:

include Geocoder::Model::Mongoid
geocoded_by :address # can also be an IP address
after_validation :geocode # auto-fetch coordinates

Reverse geocoding is similar:

include Geocoder::Model::Mongoid
reverse_geocoded_by :coordinates
after_validation :reverse_geocode # auto-fetch address

=== Bulk Geocoding

If you have just added geocoding to an existing application with a lot of objects you can use this Rake task to geocode them all:

rake geocode:all CLASS=YourModel

Geocoder will print warnings if you exceed the rate limit for your geocoding service.


== 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:

# returns Geocoder::Result object
result = request.location

See "Advanced Geocoding" below for more information about Geocoder::Result objects.


== Location-Aware Database Queries

Expand All @@ -70,8 +107,9 @@ To find objects by location, use the following scopes:

With geocoded objects you can do things like this:

obj.nearbys(30) # other objects within 30 miles
obj.distance_to(40.714, -100.234) # distance from object to arbitrary point
obj.nearbys(30) # other objects within 30 miles
obj.distance_from([40.714,-100.234]) # distance from arbitrary point to object
obj.bearing_to("Paris, France") # direction from object to arbitrary point

Some utility methods are also available:

Expand All @@ -80,50 +118,55 @@ Some utility methods are also available:
=> [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 )
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, [40.22,-73.99], city4 ])
Geocoder::Calculations.geographic_center(city1, city2, [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).


== Distance and Bearing

When you run a location-aware query the returned objects have two attributes added to them:
When you run a location-aware query the returned objects have two attributes added to them (only w/ ActiveRecord):

* <tt>obj.distance</tt> - number of miles from the search point to this object
* <tt>obj.bearing</tt> - direction from the search point to this object

You can take advantage of this to, for example, sort results by distance:

Place.near([54.2,2.1]).order("distance")

Bearing is given as a number (between 0 and 360): clockwise degrees from due north, for example:
Results are automatically sorted by distance from the search point, closest to farthest. Bearing is given as a number of clockwise degrees from due north, for example:

* +0+ - due north
* +180+ - due south
* +90+ - due east
* +270+ - due west
* +230.1+ - southwest
* +359.9+ - almost due north
* <tt>0</tt> - due north
* <tt>180</tt> - due south
* <tt>90</tt> - due east
* <tt>270</tt> - due west
* <tt>230.1</tt> - southwest
* <tt>359.9</tt> - almost due north

You can convert these numbers to compass point names by using the utility method provided:

Geocoder::Calculations.compass_point(355) # => "N"
Geocoder::Calculations.compass_point(45) # => "NE"
Geocoder::Calculations.compass_point(208) # => "SW"

<i>Note: when using SQLite +distance+ and +bearing+ values are provided for interface consistency only. They are not accurate.</i>
<i>Note: when using SQLite +distance+ and +bearing+ values are provided for interface consistency only. They are not very accurate.</i>

To calculate accurate distance and bearing with SQLite or Mongoid:

obj.distance_to([43.9,-98.6]) # distance from obj to point
obj.bearing_to([43.9,-98.6]) # bearing from obj to point
obj.bearing_from(obj2) # bearing from obj2 to obj

The <tt>bearing_from/to</tt> methods take a single argument which can be: a <tt>[lat,lon]</tt> array, a geocoded object, or a geocodable address (string). The <tt>distance_from/to</tt> methods also take a units argument (<tt>:mi</tt> or <tt>:km</tt>).


== 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+:
You are not stuck with using the +latitude+ and +longitude+ database column names (with ActiveRecord) or the +coordinates+ array (Mongoid) for storing coordinates. For example:

geocoded_by :address, :latitude => :lat, :longitude => :lon
geocoded_by :address, :latitude => :lat, :longitude => :lon # ActiveRecord
geocoded_by :address, :coordinates => :coords # Mongoid

The +address+ method can return any string you'd use to search Google Maps. For example, any of the following are acceptable:

Expand All @@ -141,30 +184,33 @@ If your model has +street+, +city+, +state+, and +country+ attributes you might

For reverse geocoding you can also specify an alternate name attribute where the address will be stored, for example:

reverse_geocoded_by :lat, :lon, :address => :location
reverse_geocoded_by :lat, :lon, :address => :location # ActiveRecord
reverse_geocoded_by :coordinates, :address => :loc # Mongoid


== Advanced 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:
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 an array of <tt>Geocoder::Result</tt> objects) in which you handle the parsed geocoding result any way you like, for example:

reverse_geocoded_by :lat, :lon do |obj,geo|
obj.city = geo.city
obj.zipcode = geo.postal_code
obj.country = geo.country_code
reverse_geocoded_by :lat, :lon do |obj,results|
if geo = results.first
obj.city = geo.city
obj.zipcode = geo.postal_code
obj.country = geo.country_code
end
end
after_validation :reverse_geocode

Every <tt>Geocoder::Result</tt> object, +result+, provides the following data:

* +result.latitude+ - float
* +result.longitude+ - float
* +result.coordinates+ - array of the above two
* +result.address+ - string
* +result.city+ - string
* +result.postal_code+ - string
* +result.country_name+ - string
* +result.country_code+ - string
* <tt>result.latitude</tt> - float
* <tt>result.longitude</tt> - float
* <tt>result.coordinates</tt> - array of the above two
* <tt>result.address</tt> - string
* <tt>result.city</tt> - string
* <tt>result.postal_code</tt> - string
* <tt>result.country_name</tt> - string
* <tt>result.country_code</tt> - string

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

Expand Down Expand Up @@ -283,14 +329,6 @@ 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!


== 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:

# returns Geocoder::Result object
result = request.location


== Use Outside of Rails

You can use Geocoder outside of Rails by calling the <tt>Geocoder.search</tt> method:
Expand All @@ -300,6 +338,17 @@ You can use Geocoder outside of Rails by calling the <tt>Geocoder.search</tt> me
This returns an array of <tt>Geocoder::Result</tt> objects with all information provided by the geocoding service. Please see above and in the code for details.


== Notes on Mongoid

=== The Near Method

Mongoid document classes have a built-in +near+ scope, but since it only works two-dimensions Geocoder overrides it with its own spherical +near+ method in geocoded classes.

=== Latitude/Longitude Order

Coordinates are generally printed and spoken as latitude, then logitude ([lat,lon]). Geocoder respects this convention and always expects method arguments to be given in [lat,lon] order. However, MongoDB requires that coordinates be stored in [lon,lat] order as per the GeoJSON spec (http://geojson.org/geojson-spec.html#positions), so internally they are stored "backwards." However, this does not affect order of arguments to methods when using Mongoid. I mention this only in case you notice it and freak out. Don't worry. Everything is going to be OK.


== Distance Queries in SQLite

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, along with inaccurate distance and bearing calculations.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.11
0.9.12
2 changes: 1 addition & 1 deletion geocoder.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Gem::Specification.new do |s|
s.homepage = "http://www.rubygeocoder.com"
s.date = Date.today.to_s
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. Designed for Rails but works with other Rack frameworks too."
s.description = "Provides object geocoding (by street or IP address), reverse geocoding (coordinates to street address), and distance queries for ActiveRecord and Mongoid. Designed for Rails but works with other Rack frameworks too."
s.files = `git ls-files`.split("\n") - %w[geocoder.gemspec Gemfile init.rb]
s.require_paths = ["lib"]
end

0 comments on commit 54329e5

Please sign in to comment.