本文介绍了使用Rails将几何字段添加到PostGIS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,其中有一个称为地址的表.我想添加一个几何字段.我可以在pgAdmin中做到这一点,但不确定它在Rails中如何发挥作用.

I have a database with a table called addresses. I want to add a geometry field. I could do it in pgAdmin, but not sure how it would play with Rails.

我认为sql是:

ALTER TABLE addressesADD geom geometry(Point,4326)

ALTER TABLE addressesADD geom geometry(Point,4326)

然后我要运行

UPDATE addresses SET geom = ST_SetSRID(ST_MakePoint(longitude,latitude),4326);

我可以在Rails中执行此操作,还是必须在pgAdmin(或psql)中执行此操作?

Can I do that in Rails or must I do it in pgAdmin (or psql)?

谢谢.新手迷上了合并多个应用程序的复杂性.

Thanks. Newbie getting lost in the complications of mashing several applications.

推荐答案

您是否正在使用此gem activerecord -postgis-adapter ?如果没有,则可以使用它,然后可以在迁移文件中添加geometry列:

Are you using this gem activerecord-postgis-adapter? If not, you can use that and then you can add a geometry column in your migration file:

create_table :my_spatial_table do |t|
  t.column :shape1, :geometry
end

或:

create_table :my_spatial_table do |t|
  t.geometry :shape2
end

activerecord-postgis-adapter扩展了ActiveRecord的迁移语法,以支持某些空间类型,例如geometry.因此,在使用此gem时,您可以像其他内置类型(stringdateinteger等)一样无缝地在Rails迁移中使用geometry类型.

The activerecord-postgis-adapter extends ActiveRecord's migration syntax to support some spatial types such as geometry. So, you can use geometry type in your Rails migration seamlessly like other built-in types (string, date, integer etc.) while using this gem.

请参阅gem文档中的创建空间表部分,一些更多的信息和示例.

See Creating Spatial Tables section from the gem's documentation for some more information and examples.

这篇关于使用Rails将几何字段添加到PostGIS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:16