Distance Matrix from Haversines Formula

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

CharlieEVIEWS
Posts: 202
Joined: Tue Jul 17, 2012 9:47 am

Distance Matrix from Haversines Formula

Postby CharlieEVIEWS » Sat Feb 21, 2015 8:20 pm

A program to compute (symmetric) distance matrices (diag=NA) from Haversines formula with two input series of (decimal) latitude and longtitudes:

Code: Select all

if @obs(latitude)<>@obs(longitude) then @uiprompt("Error: Lat and Long not same size") endif matrix(@obs(latitude),@obs(longitude)) distance_haversine genr lat_rad = latitude*(@acos(-1)/180) genr lon_rad = longitude*(@acos(-1)/180) for !b = 1 to @obs(latitude) for !a = 1 to @obs(latitude) if !a=!b then distance_haversine(!b,!a)=NA else !dlat = lat_rad(!a)-lat_rad(!b) !dlon = lon_rad(!a)-lon_rad(!b) !temp = ((@sin(!dlat/2))^2)+(@cos(lat_rad(!a))*@cos(lat_rad(!b))*(@sin(!dlon/2))^2) !temp2 = 2*@atan((({!temp})^0.5)/((1-{!temp})^0.5)) !r=6371 'radius of eath in KM distance_haversine(!b,!a)=!r*!temp2 endif next next
Any questions, please don't hestiate to ask. I intend to post a few more spatial routines in the future (next up - various distance matrices): if anybody has any requests (on distance matrices or spatial estimators), please post below any of the threads or send a PM through this forum.

Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 2 guests