我将postgis_adapter与PostgreSQL 9.0.4、PostGIS 1.5.2和Ruby1.9.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>希望有人能有个主意。
发布于 2011-05-13 07:11:15
执行摘要:如果您将其更改为:
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所以,require 'postgis_adapter',但是接下来:
PGError: ERROR: relation "table_points" does not exist这一定是我听说过的ActiveRecord中令人惊叹的命名约定。所以创建一个名为table_points的表,因为我不知道数据库模型/同步方法是什么。
moveable=> create table table_points(data text, geo_something geometry);注意,这里我使用了geometry而不是geography,因为我对您的问题的第一反应是数据库适配器层中的建模方法创建了点类型。实际上,一点也不。然后在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, @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)我不愿把这篇文章作为一个答案,因为我根本不熟悉Ruby,但上面的方法是有效的(请记住,对我来说),你也许能够根据你的情况来调整它。
发布于 2011-05-11 18:58:42
我对Rails和Ruby的了解非常有限,但请尝试:
class Model < ActiveRecord::Base
end
pt = Model.new(:geom => Point.from_x_y(10,20))
pt.save发布于 2012-03-18 05:11:25
添加空间参考系统标识符( Spatial Reference System Identifier,SRID)为我解决了此问题:
Model.create(:geom => Point.from_x_y(10,20, 4326))如果您使用的是Google Maps,那么4326是正确的SRID。其他地图和供应商可能会使用其他东西。
https://stackoverflow.com/questions/5956315
复制相似问题