Different coordinates precision when querying by model and by SQL
Closed this issue · 2 comments
pmichna commented
I have a Location model, which in schema.rb looks like this:
create_table "locations", force: :cascade do |t|
t.geometry "lonlat", limit: {:srid=>0, :type=>"point"}
end
When I query the DB using ActiveRecord model, I get this:
Location.find(20)
Location Load (1.1ms) SELECT "locations".* FROM "locations" WHERE "locations"."id" = $1 LIMIT 1 [["id", 20]]
=> #<Location id: 20, lonlat: #<RGeo::Geos::CAPIPointImpl:0x3ffb552d4944 "POINT (20.983908008784056 52.23228199246873)">>
However, I need to do some more advanced query and I'm using raw SQL. I've noticed, that this way returns returns less precise values:
sql = "SELECT id, ST_X(lonlat) AS lon, ST_Y(lonlat) AS lat FROM locations WHERE id=20"
=> "SELECT id, ST_X(lonlat) AS lon, ST_Y(lonlat) AS lat FROM locations WHERE id=20"
2.2.1 :043 > ActiveRecord::Base.connection.execute(sql).first
(1.3ms) SELECT id, ST_X(lonlat) AS lon, ST_Y(lonlat) AS lat FROM locations WHERE id=20
=> {"id"=>"20", "lon"=>"20.9839080087841", "lat"=>"52.2322819924687"}
Why is that? What should I do to get the same precision using ActiveRecord::Base.connection.execute(sql)
?
Gems' versions from Gemfile.lock:
activerecord-postgis-adapter (3.1.4)
activerecord (~> 4.2)
rgeo-activerecord (>= 4.0.4)
teeparham commented
The difference between these two coordinates appears to be extremely small (I got 0.0000000001 cm
). Does it matter?
pmichna commented
No, it doesn't. :) You can close/delete the issue.