【问题标题】:Rails with PostGIS带有 PostGIS 的 Rails
【发布时间】:2011-07-15 00:04:33
【问题描述】:

首先,我将 Rails3 与 Ruby 1.9.2 一起使用。

我在使用带有 PostGIS 的 PostgreSQL 时遇到问题。我尝试了两种宝石:

问题是,来自 nofxx 的第一个运行良好,但它不提供 rake 任务,因此 postgis.sql 和 spatial_ref_sys.sql 被插入到数据库中。我需要这个,因为使用测试(自动创建数据库,插入 postgis sql 并在运行“rake test”时执行迁移)很舒服且必不可少。奇怪的是,在 github 上的 README 中,作者写了一些关于 PostGIS 助手和 rake 任务的内容。但是它们没有实现(或者我是盲人)。

上面列出的第二个 gem 完美地为 rake 提供了这个功能!但是我遇到了一个我无法解决的问题。我按照自述文件中的说明进行操作,但每次尝试在空间列上设置位置时,都会得到以下输出:

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 

如您所见,我使用 WKT(众所周知的文本)设置了一个位置,但它始终为零。我也尝试将其保存到数据库中,但它也为零。 我发现,当我尝试使用 RGeo 提供的对象时

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

它说,工厂是零。所以,创建RGeo提供的工厂肯定有问题。如 activerecord-postgis-adapter 的 README 中所述,我在 Venue 模型中使用以下行来提供此支持:

self.rgeo_factory_generator = RGeo::Geos.factory_generator

但它不起作用。

所以要找到解决办法,有两种方法:想办法让rake任务进入nofxx的georuby gem,或者用activerecord-postgis-adapter的工厂解决问题。

希望有人可以帮助我, 谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 postgresql postgis


    【解决方案1】:

    最后我自己解决了。我在玩 rake 任务时遇到了一些让我很恼火的问题(比如,你不能使用 psql 提供脚本中的密码......)。 但我发现,您可以为 database.yml 文件提供 template 选项。所以,我只是写了:

    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
    

    现在,每次我创建我的数据库(在测试等期间)时,都会添加所有 PostGIS 内容!对!

    【讨论】:

    • 您可以将“script_dir”变量添加到您的数据库中,该变量将执行两个必需的 sql 脚本以使用 postgis。在 activerecord-postgis-adapter 中搜索“script_dir”。我使用它并且效果很好。不需要模板。
    【解决方案2】:

    我花了大约 4 个小时来解决“activerecord-postgis-adapter”的相同问题。答案很简单:“永远不要在 WKT 中使用 COMAS”。在您的情况下,它必须是v = Venue.new :location =&gt; "POINT(102.0 47.2)" 然后一切正常!

    【讨论】:

      猜你喜欢
      • 2021-08-21
      • 2013-11-07
      • 2013-06-06
      • 2012-08-17
      • 2013-11-14
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      • 2013-08-06
      相关资源
      最近更新 更多