Rails - PostGIS + postgis_adapter Geometry Problem

I am using postgis_adapter along with PostgreSQL 9.0.4, PostGIS 1.5.2 and Rails 3.1.0 on Ruby 1.9.2. As described in postgis_adapter README, I tried to execute

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

Postgres is responsible

 ERROR: parse error - invalid geometry HINT: You must specify a valid OGC WKT geometry type such as POINT, LINESTRING or POLYGON 

The created GeoRuby object is as follows:

 #<GeoRuby::SimpleFeatures::Point:0x0000010420a620 @srid=4326, @with_z=false, @with_m=false, @y=20, @x=10, @z=0.0, @m=0.0> 

Hope someone got an idea.

+4
source share
4 answers

Summary: this may work if you change it to:

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

Longer version with a high probability of Rubin’s abuse: I barely wrote the word rb, but this week I saw too many wonderful projects to continue in a state of ignorance. Work with error messages in irb (starting from 0 in a language, but very familiar with 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 

So require 'postgis_adapter' , but then:

 PGError: ERROR: relation "table_points" does not exist 

This should be the amazing ActiveRecord naming convention I've heard of. So create a table called table_points because I don’t know what the database model / synchronization methods are.

 moveable=> create table table_points(data text, geo_something geometry); 

Note that I used geometry , not geography , because my first instinct regarding your problem was that the modeling methods at the database adapter level created point types. Not at all. Then again in irb ,

 pt = TablePoint.new(:geom => Point.from_x_y(1,2)) ActiveRecord::UnknownAttributeError: unknown attribute: geom 

No attribute named geom? To see what happens again in psql :

 moveable=> alter table table_points add column geom geometry; ALTER TABLE 

Then:

 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 

Unbelievable! What if I did:

 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 

Even more incredible!

 moveable=> select * from table_points; data | geo_something | geom --------+-----------------+-------- | | 0101000000000 | 010100000000000 | | 010100000000000 | ...ble? | 00005E400000000 | 010000405E40 (4 rows) 

I hesitate to publish this as an answer due to the fundamental lack of familiarity with Ruby, but the above works (reasonably, for me), and you can adapt it to your business.

+1
source

My knowledge of Rails and Ruby is very limited, but try instead:

 class Model < ActiveRecord::Base end pt = Model.new(:geom => Point.from_x_y(10,20)) pt.save 
0
source

Adding a spatial reference system identifier (SRID) fixed this problem for me:

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

If you use Google Maps, 4326 is the correct SRID. Other cards and providers may use something else.

0
source

I ran into a similar problem using spaces_adapter when upgrading a working application from Rails-3.0 to 3.1. Newer versions of the ActiveRecord type seem to pass your record to the GeoRuby object during insertion. activerecord-postgis-adapter correctly handles the fill type of your geometry column (presumably for EWKT). I spent a lot of time chasing it today, and Aaron Patterson's answer in this post was very helpful.

To fix the problem, update the Gemfile and database.yml to use activerecord-postgis-adapter .

0
source

All Articles