Miscellaneous PostgreSQL-related changes to Spatial Adapter
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 3 commits ahead, 122 commits behind pdeffendol:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


=Spatial Adapter for Rails
This is the Spatial Adapter for Rails 0.1.1. It is a plugin for Rails which manages the MySql Spatial and PostGIS geometric columns in a transparent way (that is like the other base data type columns). It also provides a way to manage these columns in migrations. It replaces both the "PostGIS Adapter for Rails" and "MySql Spatial Adapter for Rails" plugins.

You need to install a version >= 0.1.1 of the GeoRuby gem (http://rubyforge.org/projects/georuby/) :
	gem install georuby

At the root of your Rails project, type :
       script\plugin install svn://rubyforge.org/var/svn/georuby/SpatialAdapter/trunk/spatial_adapter
You need to have SVN installed.
====== PostGIS SQL Scripts
This is only for PostgreSQL databases:

Place the following scripts in a folder named 'spatial' under the 'db' folder; For example:
These will be used when creating the Test database when running the Rake Test tasks.  These scripts should have been installed when the PostGIS libraries were installed.  Online reference: http://postgis.refractions.net/

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.

==== Migrations

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.column "data", :string
    	    t.column "geom", :point, :null=>false, :srid => 123, :with_z => true
	  add_index "table_points", "geom", :spatial=>true

Here is a related statement valid for MySql version <= 5.0.16 :
	ActiveRecord::Schema.define do
	  create_table "table_points", ;options=>"ENGINE=MyISAM", :force => true do |t|
    	    t.column "data", :string
    	    t.column "geom", :point, :null=>false
	  add_index "table_points", "geom", :spatial=>true
The differences with the PostGIS version are because of the following reasons :
- On all version of MySql, the :srid and :with_z would be ignored, since they are not supported.
- On MySql versions <= 5.0.16, you have to add <tt>:options => "ENGINE=MyISAM"</tt> to the create_table statetement, since only MyISAM tables can have geometric columns.

==== Model

Here is the model you would use, in both MySql and PostGIS:
	class TablePoint < ActiveRecord::Base
That was easy! As you see, there is no need to declare a column as geometric. The plugin will get this information by itself.

==== Access

Here is an example of PostGIS 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 = TablePoint.find_first
	puts pt.geom.x #access the geom column like any other

For MySQL, it is slightly different since it does not support Z dimension or SRID :
	pt = TablePoint.new(:data => "Hello!",:geom => Point.from_x_y(-1.6,2.8))
	pt = TablePoint.find_first
	puts pt.geom.x #access the geom column like any other

==== Fixtures

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 everytime but the spatial adapter provides a method to do it for you: +to_yaml+. It works for both MySQL and PostGIS (although the string returned is different for each database). You would use it like this, if the geometric column is a point:
	 id: 1
	 data: HELLO
	 geom: <%= Point.from_x_y(123.5,321.9).to_yaml %>

==== Find_by

find_by_[column] has been redefined when column is of a geometric type. Instead of using the Rails default '=' operator, for which I can't see a definition for MySql spatial datatypes and which performs a bounding box equality test in PostGIS, it uses a bounding box intersection: && in PostGIS and MBRIntersects in MySQL, which can both make use of a spatial index if one is present to speed up the queries. You could use this query, for example, if you need to display data from the database: You would want only the geometries which are in the screen rectangle and you could use a bounding box query for that. Since this is a common case, it is the default. You have 2 ways to use the find_by_[geom_column]: Either by passing a geometric object directly, or passing an array with the 2 opposite corners of a bounding box (with 2 or 3 coordinates depending of the dimension of the data).
In PostGIS, since you can only use operations with geometries with the same SRID, you can add a third element representing the SRID of the bounding box to the array. It is by default set to -1:

==== Geometric data types

Ruby geometric datatypes are currently made available only through the GeoRuby library (http://thepochisuperstarmegashow.com/ProjectsDoc/georuby-doc/index.html): This is where the <tt>Point.from_x_y</tt> in the example above comes from. It is a goal of a future release of the Spatial Adapter to support additional geometric datatype libraries, such as Ruby/GEOS, as long as they can support reading and writing of EWKB.

- If you use a version of MySQL before 5.0.16, only tables using the MyISAM engine can support geometric columns. After MySQL 5.0.16, any engine (incuding INNODB) is supported. 

- 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
- Since the translation to a geometry is performed everytime 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
       place.the_geom = the_geom

===Changes since last version
- The PostGIS adapter and the MySql Spatial adapter have been merged into one plugin. The correct files to load is determined using the type of connection defined in the environment.
- Geometric columns can now be dumped just like other base data types. This means you can use the ruby schema mode, even if you use the plugin.
- Support of M dimensions in migrations. The <tt>:dimension</tt> key in the column definition has disappeared and has been replaced by <tt>:with_z</tt> and <tt>:with_m</tt>.
- Addition of unit tests. At the plugin root, Run <tt>rake test:mysql</tt> to run the mysql tests and <tt>rake test:postgis</tt> for the postgis ones. You will need to configure your connection in <tt>test/db/database_mysql.yml</tt> and <tt>test/db/database_postgis.yml</tt>. If you get errors on your platform, please report to mailto:guilhem.vellut+georuby@gmail.com.
- Addition of a find_by methods with a special behaviour for geometries
- Addition of a to_yaml method to use inside a YAML fixture

- Support of other geometric datatype libraries in addition to GeoRuby
- Tutorials

The Spatial Adapter for Rails is released under the MIT license.

Any questions, enhancement proposals, bug notifications or corrections can be sent to mailto:guilhem.vellut+georuby@gmail.com.