[英]Saving a point value using activerecord-postgis-adapter
I am using activerecord-postgis-adapter in my rails app and I have a column to store a point but I can't work out how to save a value for the point.我在我的 rails 应用程序中使用activerecord-postgis-adapter并且我有一个列来存储一个点,但我无法弄清楚如何为该点保存一个值。
In my schema I have在我的架构中,我有
create_table "privacy_zones", force: :cascade do |t|
t.bigint "user_id"
t.geography "original_point", limit: {:srid=>4326, :type=>"st_point", :geographic=>true}, null: false
t.geography "random_in_range", limit: {:srid=>4326, :type=>"st_point", :geographic=>true}, null: false
t.integer "range", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_privacy_zones_on_user_id"
end
and in my controller I have在我的控制器中我有
def create
@privacy_zone = PrivacyZone.new(
range: params[:privacy_zone][:range],
original_point: "ST_POINT(#{params[:privacy_zone][:original_point][0]}, #{params[:privacy_zone][:original_point][1]})",
random_in_range: "ST_POINT(#{params[:privacy_zone][:random_in_range][0]}, #{params[:privacy_zone][:random_in_range][1]})",
user: current_user
)
p @privacy_zone
end
When I post to it I see this in the server log当我发布到它时,我在服务器日志中看到了这个
Started POST "/profile/privacy_zone" for 127.0.0.1 at 2019-03-02 16:47:24 +1030
Processing by PrivacyZoneController#create as HTML
Parameters: {"privacy_zone"=>{"original_point"=>[35.87006446264988, 107.75390625], "random_in_range"=>[35.87185877011052, 107.75633485018554], "range"=>400}}
User Load (4.4ms) SELECT "users".* FROM "users" WHERE "users"."uid" = $1 LIMIT $2 [["uid", "test@example.com"], ["LIMIT", 1]]
#<PrivacyZone id: nil, user_id: 1, original_point: nil, random_in_range: nil, range: 400, created_at: nil, updated_at: nil>
original_point and random_in_range are nil so the record will not save. original_point 和 random_in_range 为零,因此不会保存记录。 What is the proper way to insert data to st_point columns?
将数据插入 st_point 列的正确方法是什么?
My code was generating values in the form ST_POINT(42.14603262169405, 69.43359375000001)
我的代码以
ST_POINT(42.14603262169405, 69.43359375000001)
形式生成值
The correct value is POINT(42.14603262169405 69.43359375000001)
(Without the ST_
and without ,
)正确的值是
POINT(42.14603262169405 69.43359375000001)
(没有ST_
也没有,
)
A spatial factory can be used to create a point object instead of raw string interpolation.空间工厂可用于创建点对象而不是原始字符串插值。 eg
例如
original_point: RGeo::Geographic.spherical_factory(srid: 4326).point(
params[:privacy_zone][:original_point][0],
params[:privacy_zone][:original_point][1]
)
Where SRID 4326 represents using a geographic spatial reference system with degrees as the units.其中SRID 4326 表示使用以度为单位的地理空间参考系统。 https://postgis.net/workshops/postgis-intro/geography.html
https://postgis.net/workshops/postgis-intro/geography.html
你可以试试这个
t.column 'point', 'point'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.