2

I'm trying to create a basic app using the activerecord-postgis-adapter gem and am following the instructions in the readme.

According to the readme, a point is supposed to use the spherical factory, but I get a CAPIPointImpl type instead of a SphericalPointImpl for a point. This means that a distance calculation doesn't work.

Here is what I am trying:

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

I am using ruby 2.3.1, rails 5, postgres 9.6 and postgis 2.3

This is my schema:

 create_table "my_spatial_tables", force: :cascade do |t|
    t.geography "lonlat",       limit: {:srid=>4326, :type=>"point", :geographic=>true}

    t.index ["lonlat"], name: "index_my_spatial_tables_on_lonlat", using: :gist
  end

My initialization:

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

And my database config:

default: &default
  adapter: postgis
  encoding: unicode
  schema_search_path: public, postgis
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %
Ilya Lavrov
  • 2,810
  • 3
  • 20
  • 37
wildrhombus
  • 113
  • 7

2 Answers2

2

I solved it by explicitly creating a spherical factory and creating the point from it - so

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

It does seem like there is a bug, either in the activerecord-postgis-adapter gem or the rgeo-activerecord gem. I couldn't figure the bug out though. The SpatialFactoryStore looked like it was setup correctly, but it just wasn't being used.

wildrhombus
  • 113
  • 7
1

Looks like it's a bug in activerecord-postgis-adapter. Only solution i found - set spherical factory as default factory in initializer:

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

This works for me.

Ilya Lavrov
  • 2,810
  • 3
  • 20
  • 37