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.