[Qgis-user] distance between points in a table

benoit-3 at bc-consult.com benoit-3 at bc-consult.com
Tue May 22 03:10:56 PDT 2012


  

On 21.05.2012 20:27, Micha Silver wrote: 

> Hi Mike:
> 
> ...
spatialite solution ...

> On 05/21/2012 12:59 PM, M.E.Dodd wrote: 
>

>> Each row in table has two sets of coordinates and I want to find
distance between these two points.

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). 

Make sure you have created a column to hold the distance:
'column_distance' below. 

In TableManager, enter edit mode and click
the calculator. 

Check 'Update existing field' and select the distance
column (column_distance) from the dropdown list. 

In the 'Expression'
box type a formula similar to the one below:  

sqrt( (Lon1 - Lon2)^2 +
(Lat1 - Lat2)^2 ) 

to compute the Cartesian distance (deg) between the
two points. 

Or, if you want the great circle distance (in km, on a
spherical earth) use the expression below:
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 
Notes:

 - The value of 6372.8 km is as given by Wikipedia
[0] while Spatialite uses a radius of 6371.01 km.
 - '(ATAN(1.0)/45.0)'
is the conversion factor from degrees to radians.
 - The trig functions
are valid for QGIS 1.8 master, I do not know if they are present in QGIS
1.7 or below. 

Results are shown below: 

id Lon1 Lat1 Lon2 Lat2
great_circle_dist column_distance
1 0.01 51.5 -2.5 51.4 174.329616346525
2.51199124202295
2 -1.8 52.3 -1.5 53.6 145.984860951622
1.33416640641264
3 -0.1 50.8 1 50.9 78.0412798596044 1.10453610171873
4
-86.67 36.12 -118.4 33.94 2887.25995060711 31.8047999522085 

[0]:
http://en.wikipedia.org/wiki/Great-circle_distance  
HTH
Benoit 


		6371009

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20120522/8c34a401/attachment.html>


More information about the Qgis-user mailing list