Distance calculations are off compared to google maps

134 views
Skip to first unread message

Nathan Samson

unread,
Feb 11, 2016, 10:39:06 AM2/11/16
to RGeo-Users
Hi,

I am trying to get distance calculations working within rgeo.
I am using MapQuest to convert an addresses to a longitude / latitude.

When calculating the distance (both with ST_DISTANCE with postgis and rgeo's distance method) myself I get an answer around 2.8km.

When doing the same thing on https://www.daftlogic.com/projects-google-maps-distance-calculator.htm I get an answer of approximatly 1.7km.
This is a huge relative difference.

I assume this is because I am using the wrong geometry types, but I am not sure which I should be using...


Initializer
RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  config.default = RGeo::Geographic.spherical_factory(srid: 4326)
end


Migration
 t.st_point :latlon, geographic: true

 
latlon1.distance(latlon2) => 2825

latlon1 => #<RGeo::Geographic::SphericalPointImpl:0x2ad8aa0df180 "POINT (51.219323 4.425553)">
latlon2 => #<RGeo::Geographic::SphericalPointImpl:0x2ad8a9fc770c "POINT (51.221067 4.400229)">

I also tried with the simple mercator projection factory, but that gave very similar results.


Regards,
Nathan

Nathan Samson

unread,
Feb 11, 2016, 10:39:40 AM2/11/16
to RGeo-Users
Hi,

(Sorry if I am double posting, but my first attempt didn't seem to go through)

I am trying to calculate distances between 2 points.
I received the longitude / latitude from a MapQuest query.

When doing the calculation via https://www.daftlogic.com/projects-google-maps-distance-calculator.htm?route=51.219323,4.425553|51.221067,4.400229 I get an answer of 1.7km

When doing the calculation via ST_DISTANCE in postgresql or rgeo distance method, I get an answer of around 2.8km which is relatively a huge difference.




Migration
t.st_point :latlon, geographic: true

Initializer (I've tried with an alternative of the mercator factory, and doing the calculations on the projected points, but that did not give a relevant difference).

RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  config.default = RGeo::Geographic.spherical_factory(srid: 4326)
end


latlon1.distance(latlon2) => 2825
latlon1 => #<RGeo::Geographic::SphericalPointImpl:0x2ad8aa0df180 "POINT (51.219323 4.425553)">
latlon2 => #<RGeo::Geographic::SphericalPointImpl:0x2ad8a9fc770c "POINT (51.221067 4.400229)">
 

Can soneone help me to explain the difference in distances calculated, and how I can get closer to the google maps result?

Regards,
Nathan

Nathan Samson

unread,
Mar 3, 2016, 12:54:52 PM3/3/16
to RGeo-Users
Finally figiured out what I was doing wrong...

The normal / intuitive way to describe a location is latitude, longitude. But in a point implementation the normal way is x, y which is the opposite way....

Long story short, once I swap my parameters I get the expected results...

Warnign to future self, and othres who might have this problem: The correct order of parameters is longitude, latitude....


Regards,
Nathan
Reply all
Reply to author
Forward
0 new messages