rgeo/activerecord-postgis-adapter

Joined select returns CAPIPointImpl instead of SphericalPointImpl

Closed this issue · 2 comments

Hello,

here are two queries, both of which are intended to select coordinates, but one returns SphericalPointImpl and other CAPIPointImpl.

rb(main):018:0> Address.select('coordinates::geometry').first.coordinates
  Address Load (1.3ms)  SELECT  coordinates::geometry FROM "addresses"  ORDER BY "addresses"."id" ASC LIMIT 1
=> #<RGeo::Geographic::SphericalPointImpl:0x3fe3d3af1d64 "POINT (106.0 10.0)">

rb(main):017:0> Realty.joins(:address).select('realties.id, addresses.coordinates::geometry').first.coordinates
  Realty Load (2.4ms)  SELECT  realties.id, addresses.coordinates::geometry FROM "realties" INNER JOIN "addresses" ON "addresses"."addressable_id" = "realties"."id" AND "addresses"."addressable_type" = $1  ORDER BY "realties"."id" ASC LIMIT 1  [["addressable_type", "Realty"]]
=> #<RGeo::Geos::CAPIPointImpl:0x3fe3d486985c "POINT (106.0 10.0)">

Might it be a bug?

This has been partially fixed by #334. In order to properly cast a geometry from a join, an attribute must be registered on the target table. In this example, you would have to do this.

class Realty < ActiveRecord::Base
  # ...
  attribute :coordinates, :st_point, srid: 4326, geographic: true
  # ...
end

and then the coordinates field from a join will be typecast to a geographic point.

Closing in favor of #336 since that is more explicit about the issue.