使用posgis适配器在st_距离和几何中面临的问题

yzckvree  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(338)

我试图从rails控制台运行查询,但出现错误
第一个问题

Location.order( ST_Distance(
    'SRID=4326;POINT(-72.1235 42.3521)',
    'SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'
  )).first

主对象的未定义方法“st_距离”
第二个问题

longitude_latitude = '0101000020E61000002DB29DEFA7C651C0B81E85EB51284540'

  Location.order('acc_rank desc', ST_Distance(
    'SRID=4326;POINT(-72.1235 42.3521)'::geometry,
    longitude_latitude
  )).first

“srid=4326”的未定义方法“几何体”;点(-72.1235 42.3521)”:字符串
如果我尝试从postgres控制台运行,我可以运行这两个查询

select  ST_Distance(
aniket_t(#     'SRID=4326;POINT(-72.1235 42.3521)'::geometry,
aniket_t(#     '0101000020E61000002DB29DEFA7C651C0B81E85EB51284540'
aniket_t(#   );
   st_distance    
------------------
 1.02017481835224
(1 row)

select ST_Distance(
aniket_t(#     'SRID=4326;POINT(-72.1235 42.3521)',
aniket_t(#     'SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'
aniket_t(#   );
     st_distance     
---------------------
 0.00150567726382822
(1 row)

对于postgis的设置,我首先安装了gem
gem“activerecord postgis适配器”
但后来我发现了一些答案,我也安装了这个宝石
宝石“rgeo”
gem'rgeo activerecord'
不确定运行上述查询是否需要rgeo和rgeo activerecord gem
在my database.yml中,我已明确指定了适配器

default: &default
  adapter: postgis
  encoding: unicode
  pool: 10

另外,通过阅读有关stackoverflow的一些答案,我还将rgeo.rb添加到config/initializers/rgeo.rb中

RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  # By default, use the GEOS implementation for spatial columns.
  config.default = RGeo::Geos.factory_generator

  # But use a geographic implementation for point columns.
  config.register(RGeo::Geographic.spherical_factory(srid: 4326), geo_type: "point")
end

但对我来说什么都不管用。在从rails控制台运行这些查询时,如果有任何帮助,我们将不胜感激
轨道:6.0.3.7
pg gem:1.2.3
postges版本:10.17

wlsrxk51

wlsrxk511#

我通过向st_distance函数添加引号来修复查询。此外,不需要“rgeo”和“rgeo activerecord”宝石

Location.order( "ST_Distance(
    'SRID=4326;POINT(-72.1235 42.3521)',
    'SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)'
  )").first

相关问题