RGeo column is deserialized as RGeo::Geos::CAPIPointImpl instead of RGeo::Geographic::SphericalPointImpl

80 views
Skip to first unread message

Yevhenii Kurtov

unread,
Feb 8, 2016, 1:04:37 PM2/8/16
to RGeo-Users
There is an `Address` table with coordinates column

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 method which is supposed to get all objects within same coordinates 

class Realty < ActiveRecord::Base
  def self.group_by_coords
    joins(:address).group('addresses.coordinates::geometry').
    pluck(
      'array_agg(realties.id) as ids, addresses.coordinates::geometry'
    )
  end
end

accompanied with following test:

    describe 'group_by_coords' do
      it 'correctly group realties' do
        r1 = FactoryGirl.create(:apartment, address: FactoryGirl.create(:address, coordinates: 'POINT(106.0 10.0)'))
        r2 = FactoryGirl.create(:house, address: FactoryGirl.create(:address, coordinates: 'POINT(106.0 10.0)'))
        r3 = FactoryGirl.create(:apartment, address: FactoryGirl.create(:address, coordinates: 'POINT(106.5 10.5)'))
        byebug
        expect(Realty.group_by_coords).to eq(
          [[[r1.id, r2.id], r1.address.coordinates], [[r3.id], r3.address.coordinates]]
        )
      end
    end
  end

the problem is that points are restored as RGeo::Geos::CAPIPointImpl  instead of being RGeo::Geographic::SphericalPointImpl (the later seems to be an reachier object)


1) Realty scopes group_by_coords correctly group realties
     Failure/Error:
       expect(Realty.group_by_coords).to eq(
         [[[r1.id, r2.id], r1.address.coordinates], [[r3.id], r3.address.coordinates]]
       )

       expected: [[[956, 957], #<RGeo::Geographic::SphericalPointImpl:0x3fe1bc6ecd2c "POINT (106.0 10.0)">], [[958], #<RGeo::Geographic::SphericalPointImpl:0x3fe1b95f569c "POINT (106.5 10.5)">]]
            got: [[[956, 957], #<RGeo::Geos::CAPIPointImpl:0x3fe1bd8f3868 "POINT (106.0 10.0)">], [[958], #<RGeo::Geos::CAPIPointImpl:0x3fe1bd8f3444 "POINT (106.5 10.5)">]]

       (compared using ==)

I think I need to specify a proper factory to fix this, right? 

Reply all
Reply to author
Forward
0 new messages