Focal Point
Distance Calculator

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/2261009012

February 01, 2007, 05:03 PM
Glenda
Distance Calculator
We need to compare a given zip code to several zip codes in a file to determine which of these zips are within 40 miles of this zip code.

Since WebFOCUS was not created to support scientific or analytical applications, how do we create our own higher math functions (trig functions) to employ the distance calculator.

We have never utilized User-Written Subroutine route for the trig functions and would love any guidance anyone has to offer on how to set one up and employ it.

We are operating on a Unix platform.


Glenda

In FOCUS Since 1990
Production 8.2 Windows
February 01, 2007, 09:51 PM
susannah
i'm with you, Glenda. we've asked and asked and asked for the basic trig functions. I've even asked Gerry. ignored. thankfully, i don't need that degree of accuracy, ...so i just use Pythagoras, figuring that accounting for the curvature of the earth is overkill, given that I only have latlong (which i xlate into radians) for zip centroids anyway.. the old gray 'user written subroutines' manual has morphed into the 'functions' manual (#4 in the standard set) and there's nothing in there to help. Remember ANALYZE FILE from mainframe days?..if they would just give us sin cos tan and a few arcs, bliss.
the official answer would probably have something to do with the SPSS partnership.

This message has been edited. Last edited by: susannah,




In Focus since 1979///7706m/5 ;wintel 2008/64;OAM security; Oracle db, ///MRE/BID
February 01, 2007, 10:21 PM
mgrackin
Try looking at DEFINE FUNCTION syntax. I created a DEFINE FUNCTION that determines if a date is during day light savings time or not.

See the discussion and DEFINE FUNCTION code here:

https://forums.informationbuilders.com/eve/forums/a/tpc/...931092861#8931092861


Thanks!

Mickey

FOCUS/WebFOCUS 1990 - 2011
February 02, 2007, 07:49 AM
mgrackin
Glenda,

I wanted to clarify my last post. I was trying to suggest that you use the DEFINE FUNCTION syntax to create your own trig functions. If you know the equations that you need then you can build a function with DEFINE FUNCTION.

Sorry for the confusion of my last post. I normally do not respond to FocalPoint that late at night Razzer


Thanks!

Mickey

FOCUS/WebFOCUS 1990 - 2011
February 02, 2007, 08:45 AM
susannah
mick..would that it were that simple...alas, define function still limits us to the basic arithmentic available with focus operators..
the option of loading up trig tables if you need that degree of accuracy...probably available somewhere...
hey look, i found 'em
oh rats now i have to go rewrite my store locator

This message has been edited. Last edited by: susannah,




In Focus since 1979///7706m/5 ;wintel 2008/64;OAM security; Oracle db, ///MRE/BID
February 02, 2007, 12:52 PM
Tony A
Well actually Susannah ........ you can, using basic maths functions -

This will give you a value for the sine of an angle from 0 to 90, and if you take the angle from 90 you can get the cosine -

DEFINE FUNCTION F_SINE(angle/I3)
PI/D20.8  = 3.141592;
Rads/D20.8 = angle / 180 * PI;
F_SINE/D20.8 = Rads - ((Rads ** 3)/(1*2*3))
                    + ((Rads ** 5)/(1*2*3*4*5))
                    + ((Rads ** 7)/(1*2*3*4*5*6*7))
                    + ((Rads ** 9)/(1*2*3*4*5*6*7*8*9));
END
-RUN

TABLE FILE CAR
SUM COMPUTE ANGLE/I3 = LAST ANGLE + 5;
    COMPUTE Sine/D20.6 = F_SINE(ANGLE);
    COMPUTE Cosine/D20.6 = F_SINE(90-ANGLE);
BY MODEL NOPRINT
END
-RUN

Keep smiling and have a great weekend everyone

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
February 02, 2007, 02:30 PM
Edward Wolfgram
You can use the defines below to find sin or cosin for any value between 0 and pi/4 radians. The results (SIN or COSIN) are very nearly accurate to a double, and should be perfectly adequate for any commercial application. You can
use trig identities to get any other angle you need. The input XVAL is a double.

XSQ/D31.29 = XVAL *XVAL ;
RESID/D31.29 = XSQ * 0.5 ;
COSIA/D31.29 = 1 - RESID ;
RESIDA/D31.29 = XSQ * .0833333333333333 * RESID ;
RESIDB/D31.29 = XSQ * .0333333333333333 * RESIDA ;
RESIDC/D31.29 = XSQ * .0178571428571429 * RESIDB ;
RESIDD/D31.29 = XSQ * .0111111111111111 * RESIDC ;
RESIDE/D31.29 = XSQ * .00757575757575758 * RESIDD ;
RESIDF/D31.29 = XSQ * .00549450549450550 * RESIDE ;
RESIDG/D31.29 = XSQ * .00416666666666667 * RESIDF ;
RESIDH/D31.29 = XSQ * .00326797385620915 * RESIDG ;
RESIDI/D31.29 = XSQ * .00263157894736842 * RESIDH ;
RESIDT/D31.29 = -RESIDI +
RESIDH-RESIDG+RESIDF-RESIDE+RESIDD-RESIDC+RESIDB- RESIDA;
COSIN/D31.25 = COSIA - RESIDT ;
COSINS/D31.25 = COSIN * COSIN ;
SIN/D31.25 = SQRT(1.0 - COSINS) ;


IBI Development
February 02, 2007, 06:41 PM
susannah
super, thanks T, and welcome to the Point, Edward.
the F_SIN works great, T, thanks.
I still find i need the arccosin , in algebra,..to finish the distance equation( or the arctan if we use the haversine eq )
so i'm still stuck with pythagoras .. which means i'm telling my production people that the shortest distance between the warehouse and the factory in china is thru the middle of the earth..but hey..Wink

here is a source for lat long for zipcodes (us)
free!

NB: zipcodes change. every month a little, but every November the major changes for the year.
so their zipcentroids shift..so when you get a list from ..wherever...keep it updated, and when you find two lists with different numbers, that's the reason.

This message has been edited. Last edited by: susannah,




In Focus since 1979///7706m/5 ;wintel 2008/64;OAM security; Oracle db, ///MRE/BID
February 05, 2007, 07:59 AM
mgrackin
Susannah,

Put the DEFINE FUNCTION in the EDASPROF.PRF file. That's where I have all of mine.

Another way to do it would be to set the _site_profile in the WF Client SITE.WFS file to -INCLUDE a focexec containing the DEFINE FUNCTION code. The only gotcha with this method is that they will not be available for Report Caster.


Thanks!

Mickey

FOCUS/WebFOCUS 1990 - 2011
February 05, 2007, 11:51 AM
Glenda
Susannah,

tangent(angle) = sin(angle)/cos(angle)
tangent(angle) = sin(angle)/sin(90-angle)

cotangent(angle) = tangent(90-angle)

Hope this helps.

Glenda


Glenda

In FOCUS Since 1990
Production 8.2 Windows
February 06, 2007, 09:45 AM
Glenda
My production people might accept the route through the middle of the earth, but the state board of insurance wouldn't like it one bit. So, I must find the arctan2 equation, in algegra, in order to use the haversine equation. The arccosin , in algebra, would be my second choice to finish the distance equation. After search all over the internet, the only thing I've come up with is a headache. I've been out of school too many years to remember much about logs and the few formulas I've seen are greek to me. Maybe I'm just getting too old for this and need to move over and let you younger ones move in.

But since I'm stubborn, I'll keep on reading until I understand it or one of you can explain it. Anyone have any ideas?


Glenda

In FOCUS Since 1990
Production 8.2 Windows
February 06, 2007, 09:58 AM
Prarie
quote:
Maybe I'm just getting too old for this and need to move over and let you younger ones move in.


You must be tired...."too old" Never!


In Focus since 1993. WebFOCUS 7.7.03 Win 2003
Glenda,

For approximate disantances (say, < 100 miles) you can do the following:

Use the distance formula

dist = sqrt( (x1-x2)**2 + (y1-y2)**2) )

where x is longitude and y is latitude.

both x and y must be converted from HHMMSS as

deg = deg + (min/60) + (sec/3600)


The longitude (x) must be adjusted according to the circumference of the latitude line: use

adjust x: x = a*x; where a = 1/(sin(90-avelatitude)) ;

where, of course, avelatitude is (y1+y2)/2

The final result is in equator degrees, which is

(circumference of earth)/360

By the way, don't use ** to just square a number: just multiply x*x


Cheers,

Edward

This message has been edited. Last edited by: Edward Wolfgram,


IBI Development
Susannah,

A slight mistake in the function I gave above which results in inaccuracies for the angles 60 degrees and higher.

This is the corrected one, notice the negative sign before the Rads^7/7! as it should be and not the positive that I had originally. Frowner

DEFINE FUNCTION F_SINE(angle/I3)
PI/D20.8  = 3.141592;
Rads/D20.8 = angle / 180 * PI;
F_SINE/D20.8 = Rads - ((Rads ** 3)/(1*2*3))
                    + ((Rads ** 5)/(1*2*3*4*5))
-* The next line is now correct!!
                    - ((Rads ** 7)/(1*2*3*4*5*6*7))
                    + ((Rads ** 9)/(1*2*3*4*5*6*7*8*9));
END
-RUN


T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
Using Edwards technique (thanks Edward), a simple FUNCTION can be created that should give you the distance you require. As Edward intimates, over short distances this should not be too inaccurate. If you are looking for distances over the entire globe then you will need an ARCTAN2 function (not got that working yet Frowner).

-DEFAULT &Pi = 3.1415926535897932384626433832795;

DEFINE FUNCTION F_SINE(angle/I3)
Rads/D33.12 = angle / 180 * &Pi ;
F_SINE/D20.12 = Rads - ((Rads ** 3)/(1*2*3))
                     + ((Rads ** 5)/(1*2*3*4*5))
                     - ((Rads ** 7)/(1*2*3*4*5*6*7))
                     + ((Rads ** 9)/(1*2*3*4*5*6*7*8*9));
END

DEFINE FUNCTION F_DIST(Lat1/D16.6, Long1/D16.6, Lat2/D16.6, Long2/D16.6)
-* Earth's mean radius in km
Earth_R/D8    = 6371;
dLat/D20.6    = Lat2 - Lat1;
dLong/D20.6   = Long2 - Long1;
F_DIST/D20.4 = SQRT(dLat * dLat + dLong * dLong) * 1/(F_SINE(90 - dLat / 2)) / 360 * (2 * &Pi * Earth_R);
END
-RUN

To call it just COMPUTE (or otherwise) the value using - Distance/D20.4 = F_DIST(86.51557,33.584132,86.959727,33.588437);

Using this FUNCTION I got a distance of 49.39 km between Acmar, Alabama and Adamsville, Alabama (basically the first two off of the link from Susannah) and using this link to double check using the haversine method, I got the same value.

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
Ah, found a bug in my post. ave latitude is (y1+y2)/2, not (y1-y2)/2. (of course Smiler)


IBI Development
Edward, tell us why you say 'don't use ** when x*x will do'? you're scaring me ... I use ** everywhere.




In Focus since 1979///7706m/5 ;wintel 2008/64;OAM security; Oracle db, ///MRE/BID
For performance reasons. Calling the exp function (x**y) is probably 100 times slower than a single multiplication.


IBI Development
Susannah,

I decided to give up my internet hunt for ATAN2 and send an email to the local community college. Less than an hour later I had my answer.

The atan2(y, x) function computes the principal value of the arc tangent of y/x


A_TAN2(y,x)= (y/x) - (((y/x) ** 3)/3)
+ (((y/x) ** 5)/5)
- (((y/x) ** 7)/7)
+ (((y/x) ** 9)/9)
- (((y/x) ** 11)/11)
+ (((y/x) ** 13)/13);

Haversine Distance equation it is.

Thank you all so much.


Glenda

In FOCUS Since 1990
Production 8.2 Windows
Oh I get it! Confused NOT!

I'm sure glad I don't have to do any of this but I know who to contact if ever I do need these equations. Big Grin


Thanks!

Mickey

FOCUS/WebFOCUS 1990 - 2011
That's right. Your local community college Math Professors.


Glenda

In FOCUS Since 1990
Production 8.2 Windows