[lp_math_GPSdistance]

Description

Link: [lp_math_GPSdistance]
Author: Bil Corry
Category: Math
Version: 8.x
License: Public Domain
Posted: Feb. 19, 2006
Updated: Jan. 01, 0001
More by this author...
Returns the distance between two given points.  Uses Haversine formula.

Unit Choices:
    km     = kilometers (default)
    m     = meters
    ft     = feet
    mi     = miles
    nm     = nautical miles

Requires [lp_math_degToRad] [lp_math_pi] [lp_decimal_precisionset]

Parameters

-lat1 decimal, required Latitude of first GPS point.
-lon1 decimal, required Longitude of first GPS point.
-lat2 decimal, required Latitude of second GPS point.
-lon2 decimal, required Longitude of second GPS point.
-units string, optional Units to return distance in. Default is kilometers.

Sample Usage

Example

Zip codes from "zip_loc.txt.gz" located here under "Optional Data Files":
	http://www.cryptnet.net/fsp/zipdy/

From El Cajon (92021) to San Diego (92101) in miles: [lp_math_GPSdistance: 32.82213, -116.88550, 32.72110, -117.17436, 'mi']<br>
<br>

[var:'radius' = (lp_math_gpsRadius: 32.82213, -116.88550, 5, 'mi')]
5 mile radius around El Cajon, CA (32.82213, -116.88550) <br>
Low Point: ([$radius->lat_low], [$radius->lon_low])<br>
High Point: ([$radius->lat_high], [$radius->lon_high])<br>
<br><br>
Radius Intersections<br>
(all four values below should be close to 5 -- this is a double-check that our radius search is returning correct coordinates)<br>
<br>
[lp_math_GPSdistance: 32.82213, -116.88550, $radius->lat_low, -116.88550, 'mi']<br>
[lp_math_GPSdistance: 32.82213, -116.88550, $radius->lat_high, -116.88550, 'mi']<br>
[lp_math_GPSdistance: 32.82213, -116.88550, 32.82213, $radius->lon_low, 'mi']<br>
[lp_math_GPSdistance: 32.82213, -116.88550, 32.82213, $radius->lon_high, 'mi']<br>


returns:

From El Cajon (92021) to San Diego (92101) in miles: 18.163618834130652

5 mile radius around El Cajon, CA (32.82213, -116.88550)
Low Point: (32.749716606418723, -116.97166976690821)
High Point: (32.89454339358128, -116.79933023309178)


Radius Intersections
(all four values below should be close to 5 -- this is a double-check that our radius search is returning correct coordinates)

4.999998778467452
4.999998778467452
4.999998778467452
4.999998778467452
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
[

define_tag:'lp_math_GPSdistance',
	-description='Returns the distance between two given points.',
	-priority='replace',
	-required='lat1',-copy,		// as signed decimal degrees
	-required='lon1',-copy,		// as signed decimal degrees
	-required='lat2',-copy,		// as signed decimal degrees
	-required='lon2',-copy,		// as signed decimal degrees
	-optional='units',-copy;	// default is kilometers

/*
	Uses Haversine formula:
	http://mathforum.org/dr.math/problems/neff.04.21.99.html

		dlon = lon2 - lon1
		dlat = lat2 - lat1
		a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
		c = 2 * atan2(sqrt(a), sqrt(1-a)) 
		d = R * c
	

	Default is metric kilometers.
	
		" Every time I return to the US from a country that uses the metric system,
		  I wonder what went wrong here. Why do we still use miles and pounds and quarts?
		  The old English system is arbitrary and inefficient, and it cuts us off from the rest
		  of the worldexcept for those powerhouses of trade and science Liberia and Myanmar,
		  the only remaining metric holdouts. Other than the US, every other country has seen
		  the light.  "
		 
		 from: http://www.robertsilvey.com/notes/2005/04/inching_along.html

	Unit Choices:
		km 	= kilometers
		m 	= meters
		ft 	= feet
		mi 	= miles
		nm 	= nautical miles
	
 */


	// get units
	select: (string:(local:'units'));
		case: 'ft'; // feet
			local:'units'=6076.00000;
		case: 'nm'; // nautical miles
			local:'units'=1.00000;
		case: 'mi'; // U.S. miles
			local:'units'=1.15080;
		case: 'm'; // meters
			local:'units'=1852.00000;
		case; // default kilometers
			local:'units'=1.85200;
	/select;

	// convert coordinates to radians
		local:'lat1'= decimal:(lp_math_degToRad:(decimal:#lat1));
		local:'lon1'= decimal:(lp_math_degToRad:(decimal:#lon1));
		local:'lat2'= decimal:(lp_math_degToRad:(decimal:#lat2));
		local:'lon2'= decimal:(lp_math_degToRad:(decimal:#lon2));

	local:'earth_radius'= decimal: ((10800.0 * #units) / lp_math_pi);

	local:'dlat'= decimal: (#lat2 - #lat1);
	local:'dlon'= decimal: (#lon2 - #lon1);

	local:'chord_length'= decimal: ((math_cos:#lat1) * (math_cos:#lat2) * (math_pow:(math_sin: (#dlon / 2.0)),2.0) + (math_pow:(math_sin: (#dlat / 2.0)),2.0));

	local:'great_circle_distance'= decimal: (2.0 * (math_atan2: (math_sqrt: #chord_length) , (math_sqrt: (1.0 - #chord_length))));

	local:'distance'= decimal: (#earth_radius * #great_circle_distance);

	return: (lp_decimal_precisionset:#distance);

/define_tag;

]

 

Related Tags



Comments

none

Email:


Password:



Newest

Most Popular