title: PostGIS ST_Distance_Spheroid or Haversine
body: <p>I used <code>ST_Distance_Spheroid</code> in PostgreSQL (with Postgis) to calculate the distance between Woking and Edinburgh like as follows:</p>
  
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE pointsTable (
      id serial NOT NULL,
      name varchar(255) NOT NULL,
      location Point NOT NULL,
      PRIMARY KEY (id)
  );
  
  INSERT INTO pointsTable (name, location) VALUES 
  ( 'Woking', '(51.3168, -0.56)' ),
  ( 'Edinburgh', '(55.9533, -3.1883)' );
  
  SELECT ST_Distance_Spheroid(geometry(a.location), geometry(b.location), 'SPHEROID["WGS 84",6378137,298.257223563]')
  FROM pointsTable a, pointsTable b
  WHERE a.id=1 AND b.id=2;
  </code></pre>
  
  <p>I got a result of 592km (592,053.100454442 meters).</p>
  
  <p>Unfortunately, when I used various sources on the web to make the same calculation I consistently got around the 543km mark which is different by 8.2%.</p>
  
  <ul>
  <li><a href="http://tjpeiffer.com/crowflies.html" rel="nofollow noreferrer">source 1</a> - 338 miles (543.958 km)</li>
  <li><a href="https://www.freemaptools.com/how-far-is-it-between.htm" rel="nofollow noreferrer">source 2</a> - 544.410km</li>
  <li><a href="https://www.movable-type.co.uk/scripts/latlong.html" rel="nofollow noreferrer">source 3</a> - 543.8km</li>
  </ul>
  
  <p>Luckily, the third source clarified that they were using the haversine formula. I am not sure about the other two sources. </p>
  
  <p>Did I do something wrong in my queries or is this down to a difference in the formulas used? If so, which calculation is closest to the shortest distance a crow could fly, keeping a constant elevation?</p>