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.
[
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;
]