<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html><body>
<p>On 21.05.2012 20:27, Micha Silver wrote:</p>
<blockquote type="cite" style="padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%"><!-- html ignored --><!-- head ignored --><!-- meta ignored -->Hi Mike:<br /><br /> ... spatialite solution ...</blockquote>
<blockquote type="cite" style="padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%"><br />On 05/21/2012 12:59 PM, M.E.Dodd wrote:
<blockquote type="cite" style="padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size: 10pt; color: blue;">Each row in table has two sets of coordinates and I want to find distance between these two points. </span></p>
<p class="MsoNormal"><span style="font-size: 10pt; color: blue;"> </span></p>
</div>
</blockquote>
</blockquote>
<div class="WordSection1">
<p>As it seems you have a table with those four columns: Lon1, Lat1, Lon2, Lat2, an easy way is to use TableManager (if you do not want to convert your shapefile to spatialite).</p>
<p>Make sure you have created a column to hold the distance: 'column_distance' below.</p>
<p>In TableManager, enter edit mode and click the calculator.</p>
<p>Check 'Update existing field' and select the distance column (column_distance) from the dropdown list.</p>
<p>In the 'Expression' box type a formula similar to the one below:</p>
</div>
<div>
<p>sqrt( (Lon1 - Lon2)^2 + (Lat1 - Lat2)^2 )</p>
<p>to compute the Cartesian distance (deg) between the two points.</p>
<p>Or, if you want the great circle distance (in km, on a spherical earth) use the expression below:</p>
atan2(sqrt((cos((ATAN(1.0)/45.0)*Lat2)*sin((ATAN(1.0)/45.0)*Lon2-(ATAN(1.0)/45.0)*Lon1))^2 + (cos((ATAN(1.0)/45.0)*Lat1)*sin((ATAN(1.0)/45.0)*Lat2)-sin((ATAN(1.0)/45.0)*Lat1)*cos((ATAN(1.0)/45.0)*Lat2)*cos((ATAN(1.0)/45.0)*Lon2-(ATAN(1.0)/45.0)*Lon1))^2), sin((ATAN(1.0)/45.0)*Lat1)*sin((ATAN(1.0)/45.0)*Lat2) + cos((ATAN(1.0)/45.0)*Lat1)*cos((ATAN(1.0)/45.0)*Lat2)*cos((ATAN(1.0)/45.0)*Lon2-(ATAN(1.0)/45.0)*Lon1)) * 6372.8</div>
<div>Notes:<br />
<p> - The value of 6372.8 km is as given by Wikipedia [0] while Spatialite uses a radius of 6371.01 km.<br /> - '(ATAN(1.0)/45.0)' is the conversion factor from degrees to radians.<br /> - The trig functions are valid for QGIS 1.8 master, I do not know if they are present in QGIS 1.7 or below.</p>
<p>Results are shown below:</p>
<p>id Lon1 Lat1 Lon2 Lat2 great_circle_dist column_distance<br />1 0.01 51.5 -2.5 51.4 174.329616346525 2.51199124202295<br />2 -1.8 52.3 -1.5 53.6 145.984860951622 1.33416640641264<br />3 -0.1 50.8 1 50.9 78.0412798596044 1.10453610171873<br />4 -86.67 36.12 -118.4 33.94 2887.25995060711 31.8047999522085</p>
<p> </p>
<p>[0]: http://en.wikipedia.org/wiki/Great-circle_distance</p>
</div>
<div>HTH<br />Benoit</div>
<table style="width: 64px;" border="0" cellspacing="0" cellpadding="0"><colgroup><col width="64" /></colgroup>
<tbody>
<tr>
<td style="height: 15pt; width: 48pt;" align="right" width="64" height="20">6371009</td>
</tr>
</tbody>
</table>
</body></html>