How can calculate the real distance between two points with GeoDjango?
<pre><code>from django.contrib.gis.geos import Point

p1 = Point(36.74851779201058, -6.429006806692149, srid=4326)
p2 = Point(37.03254161520977, -8.98366068931684, srid=4326)
p1.distance(p2)
Out: 2.5703941316759376
</code></pre>

<p>But what is the unit of this float number?</p>

<p>If you calculate this distance, this is <a href="http://boulter.com/gps/distance/?from=36.74851779201058%2C-6.429006806692149&amp;to=37.03254161520977%2C%20-8.98366068931684&amp;units=k" rel="nofollow">229.88 Km</a>. You can get it too using geopy:</p>

<pre><code>from geopy.distance import distance
distance(p1, p2)
Out: Distance(229.883275249)
distance(p1, p2).km
Out: 229.88327524944066
</code></pre>

<p>I have read that you can get (so so) this, if you divide the previous number for 111:</p>

<pre><code>(2.5703941316759376 / 111) * 10000
Out: 231.5670388897241  # kilometers
</code></pre>

<p>Is there any way to get the real distance using only GeoDjango? Or should I use geopy?</p>

<p>Usually, all spatial calculations yield results in the same coordinate system as the input was given. In your case you should seek a calculation using the <em>SRID 4326</em> which is longitude/latitude polars in degrees from the prime meridian and equator.</p>

<p>Consequently, GeoDjango's distance calculation - if I get it correctly - is the Euclidean distance between the two pairs of coordinates. You are searching for the <strong>big circle distance</strong> (where your division by <code>111</code> is just a rough approximation that is only close to the actual big circle distance in certain ranges of latitude).</p>

<p><code>geopy</code> should use the big circle distance for <em>SRID 4326</em> implicitly, yielding the correct result.</p>

<p>You now have a few different options:</p>

<h2>A: Implement big circle on your own</h2>

<p>Google for <code>haversine</code> formula, you can punch in two pairs of lat/lon coordinates and you should get a good approximation of the actual big circle distance. However, this depends on the mercator approximation that is used -- remember that Earth is not a sphere. You may run into problems near the poles with this.</p>

<h2>B: Transform to a metric (local) coordinate system</h2>

<p>If you transform your two locations to another coordinate system that is measured in meters, calculating the Euclidean distance will yield the correct result. However, such coordinate systems (call them <em>planar systems</em>) are different for various regions on the globe. There are different projections for different countries, as the approximation of the Earth's irregularly curved surface as a plane is errorneous -- especially not uniquely errorneous for any location on its surface.</p>

<p>This is only applicable if all points among which you wish to calculate distances are in the same geographical region. </p>

<h2>C: Use a library for this</h2>

<p>Use <code>geopy</code> or <code>shapely</code> or any other qualified library that can calculate the actual big circle distance based on the <em>SRID</em> your points are given in. Remember that all coordinates are just approximations due to Earth's irregularity.</p>

python django gis geodjango