开发者

Rails with PostGIS

开发者 https://www.devze.com 2023-02-16 13:28 出处:网络
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:

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 perfo开发者_开发问答rm 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!


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!


Finally I solved it on my own. I was playing around with rake tasks and I ran into some issues (like, you can't provide a password from script with psql...) that anoyed me. But i found out, that you can provide a template option to the database.yml file. So, I just wrote:

development:
  adapter: postgresql
  pool: 5
  encoding: unicode
  database: myDatabase
  username: root
  password: *****
  su_username: root
  su_password: *****
  port: 5432
  template: template_postgis

test:
  adapter: postgresql
  pool: 5
  encoding: unicode
  database: myDatabase_test
  username: root
  password: *****
  su_username: root
  su_password: *****
  port: 5432
  template: template_postgis

production:
  adapter: postgresql
  pool: 5
  encoding: unicode
  database: myDatabase_production
  username: root
  password: *****
  su_username: root
  su_password: *****
  port: 5432
  template: template_postgis

Now every time I create my database (during tests and so on) all the PostGIS stuff gets added! Yeah!

0

精彩评论

暂无评论...
验证码 换一张
取 消