Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails with PostGIS

First of all, I'm using Rails3 with Ruby 1.9.2.

I have a problem using PostgreSQL with PostGIS. I've tried two gems:

  • https://github.com/nofxx/georuby
  • https://github.com/rgeo/activerecord-postgis-adapter

The problem is, that the first one from nofxx works well, but it doesn't provide rake tasks, so that the postgis.sql and spatial_ref_sys.sql get inserted into the database. I need this, because it's comfortable and essential using tests (create automatically the database, insert the postgis sql and perform the migrations when running "rake test"). The strange thing is, that in the README on github the author writes something about PostGIS helpers and rake tasks. But they are not implemented (or I'm blind).

The second gem listed above provides this functionality for rake perfectly! But I ran into an issue that I can't solve. I followed the instructions in the README, but everytime when I try to set a position on a spatial column, i get the following output:

ruby-1.9.2-p136 :010 > v = Venue.new :location => "POINT(102.0, 47.2)"
=> #<Venue id: nil, last_updated: nil, created_at: nil, updated_at: nil, location: nil>
ruby-1.9.2-p136 :011 > v.location
=> nil 

As you can see, I set a location using WKT (well known text), but it's always nil. I also tried to save it to the database, but it's also nil. I figured out, that when I try to use the Objects provided by RGeo

RGeo::Geos.factory.point(-122, 47)

it says, that the factory is nil. So, there must be a problem by creating the factory provided by RGeo. As mentioned in the README of the activerecord-postgis-adapter I used the following line in my Venue model to provide this support:

self.rgeo_factory_generator = RGeo::Geos.factory_generator

but it doesn't work.

So to find a solution, there are two ways: Finding a way to get the rake tasks into the georuby gem of nofxx, or solve the problem with the factory of the activerecord-postgis-adapter.

Hope someone can help me, thx!

like image 426
23tux Avatar asked Mar 14 '11 12:03

23tux


1 Answers

I'v spent about 4 hours on solving the same problem with "activerecord-postgis-adapter". The answer is simple : "NEVER USER COMAS IN WKT". In your situation it must be v = Venue.new :location => "POINT(102.0 47.2)" Then everything works just fine!

like image 134
Artem Avatar answered Oct 18 '22 11:10

Artem