17

there is a table with an RGeo column in Rails 4.2.4 and activerecord-postgis-adapter 3.1.2

class CreateAddresses < ActiveRecord::Migration
  def change
    create_table :addresses do |t|
      t.st_point :coordinates,   geographic: true, srid: 4326
    end

    add_index :addresses, :coordinates, using: :gist
  end
end

and a method that groups objects by their location

def self.group_by_coords
    includes(:address).
    joins(:address).
    group('addresses.coordinates::geometry').
    pluck(
      'array_agg(realties.id) as ids, addresses.coordinates::geometry'
    )
  end

with the corresponding test:

describe 'group_by_coords' do
  it 'correctly group realties' do
    # create samples
    expect(Realty.group_by_coords).to eq(
      [[[r1.id, r2.id], r1.address.coordinates], [[r3.id], r3.address.coordinates]]
    )
  end
end

the problem is that pluck returns RGeo::Geos::CAPIPointImpl instead of RGeo::Geographic::SphericalPointImpl

expected: [[[1670, 1671], #<RGeo::Geographic::SphericalPointImpl:0x3fd37e9b8a20 "POINT (106.0 10.0)">], [[1672], #<RGeo::Geographic::SphericalPointImpl:0x3fd37ab2dddc "POINT (106.5 10.5)">]]
got: [[[1671, 1670], #<RGeo::Geos::CAPIPointImpl:0x3fd37a335a44 "POINT (106.0 10.0)">], [[1672], #<RGeo::Geos::CAPIPointImpl:0x3fd37a33560c "POINT (106.5 10.5)">]]

I believe in order to fix that there must be specified a correct factory. I tried to specify it like this

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

but it then deserializes all points as RGeo::Geos::CAPIPointImpl which is more generic and incompatible with existing codebase.

So the question is how to make all points deserialized as RGeo::Geographic::SphericalPointImpl ?

Matheus Santana
  • 581
  • 1
  • 6
  • 22
lessless
  • 866
  • 10
  • 27

1 Answers1

0

What is likely here is probably simplier than what you expect let's just look at the first few characters.

expected: [[[1670, 1671],...
got: [[[1671, 1670],...

Why would those be different either way?

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468