ruby-on-rails – Rails – PostGIS postgis_adapter几何问题
我在
Ruby 1.9.2上使用
postgis_adapter和PostgreSQL 9.0.4,PostGIS 1.5.2和Rails 3.1.0.
正如postgis_adapter自述文件中所述,我试图执行 Model.create(:geom => Point.from_x_y(10,20)) Postgres回应 ERROR: parse error - invalid geometry HINT: You must specify a valid OGC WKT geometry type such as POINT,LINESTRING or POLYGON 创建的GeoRuby对象如下所示: #<GeoRuby::SimpleFeatures::Point:0x0000010420a620 @srid=4326,@with_z=false,@with_m=false,@y=20,@x=10,@z=0.0,@m=0.0> 希望有人有个主意. 解决方法
执行摘要:如果您将其更改为:
Model.create(:the_name_of_your_geo_column => Point.from_x_y(10,20)) 更长的版本,Ruby咆哮的概率很高:我几乎没有写过rb这个词,但本周看到一个太多伟大的项目继续处于无知状态.处理irb中的错误消息(从语言0开始,但非常熟悉PostGIS): ActiveRecord::Base.establish_connection(:adapter=>'postgresql',:database=>'moveable') pt = TablePoint.new(:data => "Hello!",:geom => Point.from_x_y(1,2)) NameError: uninitialized constant Point 所以,需要’postgis_adapter’,但是: PGError: ERROR: relation "table_points" does not exist 这必须是我听说过的ActiveRecord中令人敬畏的命名约定.因此,创建一个名为table_points的表,因为我不知道数据库模型/同步方法是什么. moveable=> create table table_points(data text,geo_something geometry); 请注意,我使用几何而不是地理,因为我对你的问题的第一直觉是数据库适配器层中的建模方法创建了点类型.实际上根本没有.然后在irb, pt = TablePoint.new(:geom => Point.from_x_y(1,2)) ActiveRecord::UnknownAttributeError: unknown attribute: geom 没有名为geom的属性?只是为了看看会发生什么,再次在psql中: moveable=> alter table table_points add column geom geometry; ALTER TABLE 然后: irb(main):014:0> pt = TablePoint.new(:geom => Point.from_x_y(10,20)) => #<TablePoint data: nil,geo_something: nil,geom: #<GeoRuby::SimpleFeatures::Point:0x1022555f0 @y=20,@m=0.0,@srid=-1>> irb(main):015:0> pt.save => true 难以置信的!如果我做了怎么办: pt = TablePoint.new(:data => 'is this even possible?',:geom => Point.from_x_y(38,121),:geo_something => Point.from_x_y(37,120)) => #<TablePoint data: "is this even possible?",geo_something: #<GeoRuby::SimpleFeatures::Point:0x102041098 @y=120,@x=37,@srid=-1>,geom: #<GeoRuby::SimpleFeatures::Point:0x1020410c0 @y=121,@x=38,@srid=-1>> irb(main):023:0> pt.save => true 更难以置信! moveable=> select * from table_points; data | geo_something | geom --------+-----------------+-------- | | 0101000000000 | 010100000000000 | | 010100000000000 | ...ble? | 00005E400000000 | 010000405E40 (4 rows) 我不愿意将此作为答案发布,因为基本上缺乏对Ruby的熟悉但是上面的工作(在心里,对我而言),你可能能够适应你的情况. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |