Ruby on Rails PostGIS - 将多边形记录插入DB

Asa*_*saf 6 ruby postgis ruby-on-rails rgeo

我正在使用RoR和PostGIS来存储位置数据.我正在尝试使用圆形存储估计的位置(例如,带有半径的中心点).

我尝试过类似的东西,但它不起作用:

@location = Location.new(:place_id => place.id,
                         :circle => %{ST_Buffer(ST_MakePoint(#{latitude}, #{longitude})::geography, #{accuracy})})
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用RGeo和它的工厂,但不确定如何使用它.

任何帮助将不胜感激.谢谢.

编辑1:我取得了一些进展.

factory = RGeo::Cartesian.factory
center_point = factory.point(latitude, longitude)
circle = center_point.buffer(accuracy)

@location = Location.new(:place_id => place.id,
                         :circle => circle)
Run Code Online (Sandbox Code Playgroud)

但是 - 现在它抛出以下异常:

can't cast RGeo::Cartesian::Polygon Impl to string
Run Code Online (Sandbox Code Playgroud)

再次,任何帮助将不胜感激.

小智 1

看起来表circle中命名的列locations是文本列而不是几何列。您的架构是什么样的?

您可能还想设置您的 SRID。

circle = RGeo::Cartesian.factory.point(0, 0).buffer(20)
@location = Location.new(:place_id => place.id, :circle => circle)
@locaiton.save
Run Code Online (Sandbox Code Playgroud)

另一个可能更好的选择是只存储确切的位置并查询具有一定距离的位置。您可以使用距离运算符(http://postgis.net/docs/manual-2.1/geometry_distance_centroid.html)或重叠运算符(http://postgis.net/docs/manual-2.1/geometry_overlaps.html)。