[GIS] PostGIS – incorrect result distance between two points

postgispostgis-2.0postgresql

I am quite confused the way PostGIS is returning the result for this basic query.

So I am trying to find the distance between two points
45.2714,71.2087 and 42.3739,66.39

and the distance should be ~505 km

but when I ran the query (considering 1 degree = 111km)

select st_distance (st_setsrid(st_makepoint(71.2087,45.2714),4326), 
      st_setsrid(st_makepoint(66.39,42.3739),4326))*111 as d 

I get the result as 624 km.

Can anyone please tell what I am doing wrong here ?

Thanks.

Best Answer

One degree does not "equal 111km" in generality.

select st_distance(
  'POINT(71.2087 45.2714)'::geography, 
  'POINT(66.39 42.3739)'::geography) AS d;

Or

select st_distance_spheroid(
  'SRID=4326;POINT(71.2087 45.2714)'::geometry, 
  'SRID=4326;POINT(66.39 42.3739)'::geometry,
  'SPHEROID["WGS 84",6378137,298.257223563]') AS d;