See github.com/fragility/spatial_adapter for the original version that supports mysql as well.
This is the Spatial Adapter for ActiveRecord. It enhances ActiveRecord to handle spatial datatypes in Postgresql
The following gems are required:
-
GeoRuby
-
ActiveRecord (version 2.2.2 and up)
-
PostGIS version 1.4.0 or higher should be installed in your database
gem install spatial_adapter
In a Rails 2.x app, you can add a gem dependency in environment.rb:
config.gem 'spatial_adapter'
In a Rails 3 app, add a gem dependency to Gemfile:
gem 'spatial_adapter'
Geometric columns in your ActiveRecord models now appear just like any other column of other basic data types. They can also be dumped in ruby schema mode and loaded in migrations the same way as columns of basic types.
Here is an example of code for the creation of a table with a geometric column in PostGIS, along with the addition of a spatial index on the column:
ActiveRecord::Schema.define do create_table :table_points, :force => true do |t| t.string :data t.point :geom, :null => false, :srid => 123, :with_z => true end add_index :table_points, :geom, :spatial => true end
Create your ActiveRecord models normally. Spatial Adapter will automatically handle spatial columns, converting them to the appropriate GeoRuby type.
class TablePoint < ActiveRecord::Base end
Here is an example of row creation and access, using the model and the table defined above:
pt = TablePoint.new( :data => "Hello!", :geom => Point.from_x_y_z(-1.6, 2.8, -3.4, 123)) pt.save pt = TablePoint.find_first puts pt.geom.x #access the geom column like any other
If you use fixtures for your unit tests, at some point, you will want to input a geometry. You could transform your geometries to a form suitable for YAML yourself every time but Spatial Adapter provides a method to do it for you: to_fixture_format
. You would use it like this, if the geometric column is a point:
fixture: id: 1 data: HELLO geom: <%= Point.from_x_y(123.5,321.9).to_fixture_format %>
Enhancements to find_by_* and friends has been removed from this version of Spatial Adapter until a cleaner implementation can be made. (The previous implementation made adapter-specific modifications to ActiveRecord::Base, which prevented multiple adapters from being loaded at once.)
Ruby geometric datatypes are currently made available only through the GeoRuby library (georuby.rubyforge.org/): This is where the Point.from_x_y
in the example above comes from.
-
Since ActiveRecord seems to keep only the string values directly returned from the database, it translates from these to the correct types everytime an attribute is read, which is probably ok for simple types, but might be less than efficient for geometries, since the EWKB string has to be parsed everytime. Also it means you cannot modify the geometry object returned from an attribute directly:
place = Place.find_first place.the_geom.y=123456.7 # this doesn't work
Since the translation to a geometry is performed every time the_geom is read, the change to y will not be saved! You would have to do something like this:
place = Place.find_first the_geom = place.the_geom the_geom.y=123456.7 place.the_geom = the_geom
The Spatial Adapter for ActiveRecord is released under the MIT license.
Any questions, enhancement proposals, bug notifications or corrections can be made via the project page at github.com/dougcole/spatial_adapter
The gem depdencencies can be installed with ‘bundle install`.
You will need to set up an empty database named ‘spatial_adapter`
Tests are partitioned by adapter and can be run using separate rake task.
bundle exec rake spec