简体   繁体   中英

PostGIS ActiveRecord Adapter not works

I followed the setup link want to integrate PostGIS ActiveRecord Adapter into my project, my Rails project has installed postgis already by postgis-and-rails-a-simple-approach

I followed those steps to Working With Spatial Data

And launch Rails console

c = Spatial.new()

c.lonlat = 'POINT(-122,48)'

2.1.0 :004 > c
+----+--------+------------+------------+-------+------+------+-----+-----+
| id | lonlat | created_at | updated_at | shap1 | shp2 | path | lon | lat |
+----+--------+------------+------------+-------+------+------+-----+-----+
|    |        |            |            |       |      |      |     |     |
+----+--------+------------+------------+-------+------+------+-----+-----+

database.yml

 16 #
 17 development:
 18   adapter: postgis
 19   encoding: unicode
 20   database: goodshot_development
 21   schema_search_path: public,postgis

migration file

class CreateSpatials < ActiveRecord::Migration
  def change
    create_table :spatials do |t|
      t.column :shap1, :geometry
      t.geometry :shp2
      t.line_string :path, :srid => 3785
      t.point :lonlat, :geographic => true
      t.point :lon
      t.point :lat
      t.index :lonlat, :spatial => true
      t.timestamps
    end
  end
end

The link you provided shows a very similar example of what not to do, which is what you did.

That is, to provide invalid WKT :

record.lonlat = 'POINT(-122, 47)'

Which fails to parse, and silently sets the value to nil. (Valid WKT looks like POINT(-122 47) ).

The link describes a few alternative ways to construct a geometry, such as:

factory.point(-122, 47)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM