I am listing out nearby options from user location, in which distance plays a major role. I chose rgeo to filter me out those options within 10 kms radius using ST_DWithin. However, I am getting significant margins in rgeo computed distance with respect to google map results.
Tried with the following sample points so far, and my results:
Origin: (Lat, Long) - 12.918269, 77.635669
Table
RGeo Distance Lat, Long GMap
(in m) (in Kms)
4865.012768186506, 12.960146, 77.648496 7.4
4865.012768186506, 12.960146, 77.648496 7.4
2286.5466828101517, 12.936992, 77.627002 2.7
3672.943407131079, 12.949931, 77.645193 7.8
7913.722140288826, 12.848925, 77.651733 11.7
Model:
class Locate < ApplicationRecord
scope :nearby, -> (origin, dist_in_meters = 10000) {
where("ST_DWithin(ST_GeographyFromText('SRID=4326;#{origin}'), position, #{dist_in_meters})")
}
end
Controller:
def nearby
origin = geo_factory(77.635669,12.918269)
options = Locate.nearby(origin)
opt_dist = Array.new
options.each do |option|
opt_dist << origin.distance(option.position)
end
render json: { options: options, distance: opt_dist }, status: 200
end
def geo_factory(ulong,ulat)
RGeo::Geographic.spherical_factory(srid: 4326).point(ulong,ulat)
end
Schema:
create_table "locates", force: :cascade do |t|
t.integer "user_id"
t.geography "position", limit: {:srid=>4326, :type=>"st_point", :geographic=>true}
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Initializer (rgeo.rb)
RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
# By default, use the GEOS implementation for spatial columns.
config.default = RGeo::Geos.factory_generator
# But use a geographic implementation for point columns.
config.register(RGeo::Geographic.spherical_factory(srid: 4326), geo_type: "point")
end
Assuming distance calculated in rgeo represents shortest path between two points which might not be the case in reality, hence being differing with gmap results. Can someone share if they had any similar experience?
Thanks in advance!