A couple of days ago I used Rays code for tracking hurricane distance from your local zip code to add tracking of Hurricane Hanna to my family blog. Then my father in law was kind enough to point out that it was wrong. Way wrong! It was reporting distances of over 3,000 miles, when we're really less than 300 miles from it.
Closer inspection of the code showed that Ray accidentally swapped the order of latitude and longitude assignments from the NHC feed; and wasn't accounting for Southern and Western hemispheres being represented as a negative number. Ray posted about some changes he made in a comment on his post to fix these problems (if only I had seen those before now!).
Ray is going to post his updated script soon, but mine is below.
In addition to the same fixes, I've added a touch of encapsulation (though I did remove the zip lookup stuff once I knew the lat/long for my zip -- because this was easily the slowest part of the whole script) and setup caching with a forced refresh, and refreshing if the previous attempt had any problems. Here's my entire script:
#application.hannaData#
Updated: #timeFormat(application.hannaDataUpdated, "h:MM tt")#
/**
* Calculates the distance between two latitudes and longitudes.
* This funciton uses forumlae from Ed Williams Aviation Foundry website at http://williams.best.vwh.net/avform.htm.
*
* @param lat1 Latitude of the first point in degrees. (Required)
* @param lon1 Longitude of the first point in degrees. (Required)
* @param lat2 Latitude of the second point in degrees. (Required)
* @param lon2 Longitude of the second point in degrees. (Required)
* @param units Unit to return distance in. Options are: km (kilometers), sm (statute miles), nm (nautical miles), or radians. (Required)
* @return Returns a number or an error string.
* @author Tom Nunamaker (tom@toshop.com)
* @version 1, May 14, 2002
*/
function LatLonDist(lat1,lon1,lat2,lon2,units)
{
// Check to make sure latitutdes and longitudes are valid
if(lat1 GT 90 OR lat1 LT -90 OR
lon1 GT 180 OR lon1 LT -180 OR
lat2 GT 90 OR lat2 LT -90 OR
lon2 GT 280 OR lon2 LT -280) {
Return ("Incorrect parameters");
}
lat1 = lat1 * pi()/180;
lon1 = lon1 * pi()/180;
lat2 = lat2 * pi()/180;
lon2 = lon2 * pi()/180;
UnitConverter = 1.150779448; //standard is statute miles
if(units eq 'nm') {
UnitConverter = 1.0;
}
if(units eq 'km') {
UnitConverter = 1.852;
}
distance = 2*asin(sqr((sin((lat1-lat2)/2))^2 + cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2)); //radians
if(units neq 'radians'){
distance = UnitConverter * 60 * distance * 180/pi();
}
Return (distance);
}
select rsslink, content, title
from results
where title like '%HANNA Public Advisory Number%'
Distance: " & numberFormat(dst,',9.99') & " miles
As of:
#replaceNoCase(pa.title, 'Tropical Storm HANNA Public ', '')#
" />
Hanna Long: #long# #longdir#" />
in
ColdFusion
Posted 2008-09-06 02:19
1 response:
Leave a comment: