Ruby on rails Rails-PostGIS+;postgis_适配器几何问题

Ruby on rails Rails-PostGIS+;postgis_适配器几何问题,ruby-on-rails,ruby,postgresql,postgis,Ruby On Rails,Ruby,Postgresql,Postgis,我在Ruby 1.9.2上与PostgreSQL 9.0.4、PostGIS 1.5.2和Rails 3.1.0一起使用。 正如postgis_适配器自述文件中所述,我尝试执行 Model.create(:geom => Point.from_x_y(10,20)) 博士后的反应是 ERROR: parse error - invalid geometry HINT: You must specify a valid OGC WKT geometry type such as POINT

我在Ruby 1.9.2上与PostgreSQL 9.0.4、PostGIS 1.5.2和Rails 3.1.0一起使用。 正如postgis_适配器自述文件中所述,我尝试执行

Model.create(:geom => Point.from_x_y(10,20))
博士后的反应是

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>
#

希望有人能想出主意。

我对Rails和Ruby的了解非常有限,但请尝试:

class Model < ActiveRecord::Base
end

pt = Model.new(:geom => Point.from_x_y(10,20))

pt.save
类模型点。从_x_y(10,20))
pt.save

执行摘要:如果您将其更改为:

Model.create(:你的geo列的名称=>Point.from\ux\uy(10,20))

更长的版本,很可能是Ruby howlers:我几乎没有写过rb一个字,但本周看到了太多伟大的项目,无法在无知的状态下继续下去。处理
irb
中的错误消息(该语言从0开始,但非常熟悉PostGIS):

因此,
需要“postgis\u适配器”
,但是:

PGError: ERROR:  relation "table_points" does not exist
这一定是我听说过的ActiveRecord中非常棒的命名约定。所以创建一个名为
table\u 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
然后:


由于我对Ruby缺乏基本的了解,所以我不太愿意将此作为答案发布,但上述方法(对我来说是明智的),您可以根据您的情况调整它。

添加空间参考系统标识符(SRID)为我解决了这个问题:

Model.create(:geom => Point.from_x_y(10,20, 4326))

如果您使用的是谷歌地图,4326是正确的SRID。其他地图和供应商可能会使用其他东西

我在将工作应用程序从Rails-3.0升级到3.1时遇到了类似的问题。在插入期间,ActiveRecord类型的较新版本似乎将您的记录强制转换为一个GeoRuby对象。正确处理几何体列的类型转换(可能是EWKT)。今天我花了很多时间来研究这个问题,非常有帮助


要解决您的问题,请升级您的Gemfile和database.yml以使用
activerecord postgis适配器

m=Model.new:attrs
+
m.save
实际上与
Model.create:attrs
根据我对postgis的了解,错误意味着到达数据库的不是有效的几何体。您与PostgreSQL的接口工作是否具有几何体列?@pex因此,发生的情况(我也发生过这种错误)是,到达PostGIS的不是有效的几何体。我不知道Ruby是如何插入几何体的。
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, @with_m=false, @x=10, @m=0.0, @with_z=false, @z=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, @with_m=false, @x=37, @m=0.0, @with_z=false, @z=0.0, @srid=-1>, geom: #<GeoRuby::SimpleFeatures::Point:0x1020410c0 @y=121, @with_m=false, @x=38, @m=0.0, @with_z=false, @z=0.0, @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)
Model.create(:geom => Point.from_x_y(10,20, 4326))