Postgresql 使用activerecord postgis适配器时,点不会强制转换为球形

Postgresql 使用activerecord postgis适配器时,点不会强制转换为球形,postgresql,activerecord,rubygems,postgis,rgeo,Postgresql,Activerecord,Rubygems,Postgis,Rgeo,我正在尝试使用activerecord postgis适配器gem创建一个基本应用程序,并遵循自述文件中的说明 根据自述文件,一个点应该使用球形工厂,但是我得到了一个CAPIPointImpl类型,而不是一个点的sphereicalpointimpl。这意味着距离计算不起作用 以下是我正在尝试的: record = MySpatialTable.create record.lonlat = 'POINT(-122 47)' record.lonlat returns #<RGeo::Geo

我正在尝试使用activerecord postgis适配器gem创建一个基本应用程序,并遵循自述文件中的说明

根据自述文件,一个点应该使用球形工厂,但是我得到了一个CAPIPointImpl类型,而不是一个点的sphereicalpointimpl。这意味着距离计算不起作用

以下是我正在尝试的:

record = MySpatialTable.create
record.lonlat = 'POINT(-122 47)'
record.lonlat
returns #<RGeo::Geos::CAPIPointImpl:0x3ff57d7645c8 "POINT (-122.0 47.0)">
我的初始化:

RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  config.default = RGeo::Geos.factory_generator
  config.register(RGeo::Geographic.spherical_factory(srid: 4326), geo_type: "point")
end
和我的数据库配置:

default: &default
  adapter: postgis
  encoding: unicode
  schema_search_path: public, postgis
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %
default:&default
适配器:postgis
编码:unicode
模式搜索路径:公共,postgis

池:看起来像是activerecord postgis适配器中的一个bug。我找到的唯一解决方案-在初始值设定项中将球形工厂设置为默认工厂:

RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  config.default = RGeo::Geographic.spherical_factory(srid: 4326)
  config.register(RGeo::Geographic.spherical_factory(srid: 4326), geo_type: "point")
end

这对我很有用。

我通过显式创建一个球形工厂并从中创建点来解决它-所以

 geofactory = RGeo::Geographic.spherical_factory(srid: 4326)
 MySpatialTable.lonlat = geofactory.point(user.longitude, user.latitude)
activerecord postgis适配器gem或rgeo activerecord gem中似乎确实存在bug。不过,我还是没能找出这个漏洞。SpatialFactoryStore看起来设置正确,但它只是没有被使用

 geofactory = RGeo::Geographic.spherical_factory(srid: 4326)
 MySpatialTable.lonlat = geofactory.point(user.longitude, user.latitude)