Calculating distance between latitude and longitude coordinates ?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Calculating distance between latitude and longitude coordinates ?

Justin Meyer-3

Hi SPSS list,

I am trying to calculate the distance between two latitude/longitude points in this form: 40.687870, -89.680280. I found syntax to do this here: http://www.listserv.uga.edu/cgi-bin/wa?A2=ind0810&L=spssx-l&P=53744, shown below. This syntax produces errors. I can avoid all the errors but one by creating the scratch variables manually but I am still having a problem with the last part. SPSS does not recognize the “arcos” in

compute distance = EarthRad*

                     arcos(sin(lat1/AngleCvt)*sin(lat2/AngleCvt)

                         + cos(lat1/AngleCvt)*cos(lat2/AngleCvt

                           *cos(lon2/AngleCvt-lon1/AngleCvt)).

I think “arcos” must be arccosine, if there is such a thing, but I can’t find the word for this in the command syntax reference. Can anyone tell me what to replace this with?

If I can’t make this work, how inaccurate would this be by just using asquared +bsquared=csquared and somehow converting to miles? My distances will be 50 miles or less.

Syntax I’m basing this on:

*  Initialize constants  ............  .

DO IF $CASENUM EQ 1.

*  #EarthRad is the Earth's radius in whatever units you please; .

*  the calculated distance will be in those units. Radius (3):   .

*              6,372.7976 km,

*              3,959.873  statute miles,

*              3,441.035  nautical miles.

.  COMPUTE

    #EarthRad = 6,372.7976 /* km */.

 

*  #AngleCvt is the number of your angle units (degrees) in      .

*  one of SPSS's angle units (radians). It uses that ARCTAN(1)  .

*  is PI/4 radians.                                              .

    #AngleCvt =  360  /* Number of input units in a full circle */.

               /(8*ARCTAN(1)).

 

END IF.

 

 

*  Compute distance between points with coordinates              .

*  (lat1,lon1) and (lat2,lon2)                                   .

 

compute distance = #EarthRad*

                     arcos(sin(lat1/#AngleCvt)*sin(lat2/#AngleCvt)

                         + cos(lat1/#AngleCvt)*cos(lat2/#AngleCvt

                           *cos(lon2/#AngleCvt-lon1/#AngleCvt)).

 

 

____________________________________

Justin Meyer

Researcher

Rowland Reading Foundation

6120 University Avenue

Middleton, WI 53562

phone: 866-370-7323  fax: 608-204-3846

www.rowlandreading.org

____________________________________

 

Reply | Threaded
Open this post in threaded view
|

Re: Calculating distance between latitude and longitude coordinates ?

Mark Casazza-2
SPSS doesn't have a function for arc cosine, but you can compute it with:
ArcCos(x) = PI/2-ArcSin(x).
or
ArcCos(x) = ArcTan(1)-ArcSin(x).
If you take a look in the archives there should be quite a few postings on this (and at least one where I cribbed the above code and saved it).

Mark


"A government big enough to give you everything you want, is strong enough to take everything you have."
- attributed to Thomas Jefferson


Mark Casazza
Director of Academic Information
The City University of New York
555 West 57th Street, Suite 1240
New York, NY 10019

Phone: 212.541.0396
Fax: 212.541.0392


Justin Meyer wrote:

Hi SPSS list,

I am trying to calculate the distance between two latitude/longitude points in this form: 40.687870, -89.680280. I found syntax to do this here: http://www.listserv.uga.edu/cgi-bin/wa?A2=ind0810&L=spssx-l&P=53744, shown below. This syntax produces errors. I can avoid all the errors but one by creating the scratch variables manually but I am still having a problem with the last part. SPSS does not recognize the “arcos” in

compute distance = EarthRad*

                     arcos(sin(lat1/AngleCvt)*sin(lat2/AngleCvt)

                         + cos(lat1/AngleCvt)*cos(lat2/AngleCvt

                           *cos(lon2/AngleCvt-lon1/AngleCvt)).

I think “arcos” must be arccosine, if there is such a thing, but I can’t find the word for this in the command syntax reference. Can anyone tell me what to replace this with?

If I can’t make this work, how inaccurate would this be by just using asquared +bsquared=csquared and somehow converting to miles? My distances will be 50 miles or less.

Syntax I’m basing this on:

*  Initialize constants  ............  .

DO IF $CASENUM EQ 1.

*  #EarthRad is the Earth's radius in whatever units you please; .

*  the calculated distance will be in those units. Radius (3):   .

*              6,372.7976 km,

*              3,959.873  statute miles,

*              3,441.035  nautical miles.

.  COMPUTE

    #EarthRad = 6,372.7976 /* km */.

 

*  #AngleCvt is the number of your angle units (degrees) in      .

*  one of SPSS's angle units (radians). It uses that ARCTAN(1)  .

*  is PI/4 radians.                                              .

    #AngleCvt =  360  /* Number of input units in a full circle */.

               /(8*ARCTAN(1)).

 

END IF.

 

 

*  Compute distance between points with coordinates              .

*  (lat1,lon1) and (lat2,lon2)                                   .

 

compute distance = #EarthRad*

                     arcos(sin(lat1/#AngleCvt)*sin(lat2/#AngleCvt)

                         + cos(lat1/#AngleCvt)*cos(lat2/#AngleCvt

                           *cos(lon2/#AngleCvt-lon1/#AngleCvt)).

 

 

____________________________________

Justin Meyer

Researcher

Rowland Reading Foundation

6120 University Avenue

Middleton, WI 53562

phone: 866-370-7323  fax: 608-204-3846

www.rowlandreading.org

____________________________________

 

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Calculating distance between latitude and longitude coordinates ?

Frank H. Millard
In reply to this post by Justin Meyer-3

You might need to consider the coordinate system associated with the lat/lon; since the earth is not flat points (lat/lon) are projected by a coordinate system.  If lat/lon was taken by GPS, then that position is in terms of a coordinate system (e.g., WGS 84).  Distance accuracy may be influenced if lat/lon are taken in different coordinate systems.  There are two common methods of determining distance Great Circle (http://en.wikipedia.org/wiki/Great_circle) and Rhumb line (http://en.wikipedia.org/wiki/Rhumb_line).

Reply | Threaded
Open this post in threaded view
|

Correlation for three or more variables

cbautista
Hi,

In SPSS, we use the Pearson's coefficient to estimate the correlation
between two variables, so, is there any way in SPSS to get this
coefficient for 3 or more variables?

Thanks,

/Christian

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Correlation for three or more variables

statisticsdoc
Christian,

Do you want to run a 3 by 3 correlation matrix, or are you interested in a multiple regression with two independent and one dependent variable?
Cheers,

Steve

www.StatisticsDoc.com
---- Christian Bautista <[hidden email]> wrote:

> Hi,
>
> In SPSS, we use the Pearson's coefficient to estimate the correlation
> between two variables, so, is there any way in SPSS to get this
> coefficient for 3 or more variables?
>
> Thanks,
>
> /Christian
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD

--
For personalized and experienced consulting in statistics and research design, visit www.statisticsdoc.com

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Correlation for three or more variables

Marta Garcia-Granero
Besides Steve's suggestion, maybe yo should consider intraclass
correlation coefficient.

HTH,
Marta


> Do you want to run a 3 by 3 correlation matrix, or are you interested in a multiple regression with two independent and one dependent variable?
>
> Steve
>
>
> ---- Christian Bautista <[hidden email]> wrote:
>
>> In SPSS, we use the Pearson's coefficient to estimate the correlation
>> between two variables, so, is there any way in SPSS to get this
>> coefficient for 3 or more variables?
>>
>>
>>
>
>
>


--
For miscellaneous statistical stuff, visit:
http://gjyp.nl/marta/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Calculating distance between latitude and longitude coordinates ?

Garry Gelade
In reply to this post by Justin Meyer-3
Hi Justin
 
I wrote a macro to do this. I extract the relevant calculation bits here.
 

COMPUTE d2r = 3.14159/180 /* degrees to radians */.

 COMPUTE cr1 = COS( d2r * ( 90 - lat1 ) ) .

 COMPUTE cr2 = COS( d2r * ( 90 - lat2 ) ) .

 COMPUTE sr1 = SIN( d2r * ( 90 - lat1 ) ) .

 COMPUTE sr2 = SIN( d2r * ( 90 - lat2 ) ) .

 COMPUTE cr3 = COS( d2r * ( long1long2 ) ).

 COMPUTE e = ARSIN( (cr1*cr2) + (sr1*sr2*cr3) ).

 COMPUTE greatcircledist = 6377 * ( (3.14159 / 2) - e ).

EXE.
 
If I have 'de-macroised' the syntax properly, it should work. If not, let me know and I'll send you the macro for you to adapt.
 
Garry Gelade
Business Analytic Ltd.


From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Justin Meyer
Sent: 27 March 2009 16:07
To: [hidden email]
Subject: Calculating distance between latitude and longitude coordinates ?

Hi SPSS list,

I am trying to calculate the distance between two latitude/longitude points in this form: 40.687870, -89.680280. I found syntax to do this here: http://www.listserv.uga.edu/cgi-bin/wa?A2=ind0810&L=spssx-l&P=53744, shown below. This syntax produces errors. I can avoid all the errors but one by creating the scratch variables manually but I am still having a problem with the last part. SPSS does not recognize the “arcos” in

compute distance = EarthRad*

                     arcos(sin(lat1/AngleCvt)*sin(lat2/AngleCvt)

                         + cos(lat1/AngleCvt)*cos(lat2/AngleCvt

                           *cos(lon2/AngleCvt-lon1/AngleCvt)).

I think “arcos” must be arccosine, if there is such a thing, but I can’t find the word for this in the command syntax reference. Can anyone tell me what to replace this with?

If I can’t make this work, how inaccurate would this be by just using asquared +bsquared=csquared and somehow converting to miles? My distances will be 50 miles or less.

Syntax I’m basing this on:

*  Initialize constants  ............  .

DO IF $CASENUM EQ 1.

*  #EarthRad is the Earth's radius in whatever units you please; .

*  the calculated distance will be in those units. Radius (3):   .

*              6,372.7976 km,

*              3,959.873  statute miles,

*              3,441.035  nautical miles.

.  COMPUTE

    #EarthRad = 6,372.7976 /* km */.

 

*  #AngleCvt is the number of your angle units (degrees) in      .

*  one of SPSS's angle units (radians). It uses that ARCTAN(1)  .

*  is PI/4 radians.                                              .

    #AngleCvt =  360  /* Number of input units in a full circle */.

               /(8*ARCTAN(1)).

 

END IF.

 

 

*  Compute distance between points with coordinates              .

*  (lat1,lon1) and (lat2,lon2)                                   .

 

compute distance = #EarthRad*

                     arcos(sin(lat1/#AngleCvt)*sin(lat2/#AngleCvt)

                         + cos(lat1/#AngleCvt)*cos(lat2/#AngleCvt

                           *cos(lon2/#AngleCvt-lon1/#AngleCvt)).

 

 

____________________________________

Justin Meyer

Researcher

Rowland Reading Foundation

6120 University Avenue

Middleton, WI 53562

phone: 866-370-7323  fax: 608-204-3846

www.rowlandreading.org

____________________________________

 



__________ NOD32 3969 (20090327) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com
Reply | Threaded
Open this post in threaded view
|

Re: Correlation for three or more variables

Art Kendall
In reply to this post by Marta Garcia-Granero
Please tell us more about what you are asking.

The output from RELIABILITY would have many potentially interesting outputs.
the simple correlation matrix, the internal consistency of the
sum(average) across the variables,
the squared multiple correlation of each variable with the other
variables, the correlations of each variable with the sum of the other
variables, etc.

Art Kendall
Social Research Consultants

Marta García-Granero wrote:

> Besides Steve's suggestion, maybe yo should consider intraclass
> correlation coefficient.
>
> HTH,
> Marta
>
>
>> Do you want to run a 3 by 3 correlation matrix, or are you interested
>> in a multiple regression with two independent and one dependent
>> variable?
>>
>> Steve
>>
>>
>> ---- Christian Bautista <[hidden email]> wrote:
>>
>>> In SPSS, we use the Pearson's coefficient to estimate the correlation
>>> between two variables, so, is there any way in SPSS to get this
>>> coefficient for 3 or more variables?
>>>
>>>
>>>
>>
>>
>>
>
>
> --
> For miscellaneous statistical stuff, visit:
> http://gjyp.nl/marta/
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Calculating distance between latitude and longitude

Richard Ristow
In reply to this post by Justin Meyer-3
At 12:06 PM 3/27/2009, Justin Meyer wrote:

I am trying to calculate the distance between two latitude/longitude points in this form: 40.687870, -89.680280. I found syntax to do this here: http://www.listserv.uga.edu/cgi-bin/wa?A2=ind0810&L=spssx-l&P=53744 . This syntax produces errors.

That posting is mine, and indeed it isn't right.  As you wrote, correctly

SPSS does not recognize the “arcos” in [the distance formula].

Quite right. I posted that code untested; take warning from my sad case! The corrected, tested version is hard to find. (It's in a thread called "zip code distance".)

This may be worth having as a FAQ. So, please let me know of any problems with this draft.


FAQ: Computing distance from latitude and longitude (DRAFT)

Sections below are
===== (1) Solution in native SPSS transformation code
===== (2) Using Python code from Developer Central
===== (3) Test data and test run of native SPSS code


===== (1) Native SPSS code =====================================
From Richard Ristow, "Re: zip code distance", to SPSSX-L Tue, 28 Oct 2008 20:40:17 -0400.  Earth-radius values are from
http://en.wikipedia.org/wiki/Earth_radius; see also
http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html.

*  ...............   Initialize constants      ................. .


*  The following code >REQUIRES< that angles in the base system  .
*  (SPSS) be in radians, so that the trigonometric distance is   .
*  in radians, and can be multiplied directly by the Earth's     .
*  radius.                                                       .

DO IF $CASENUM EQ 1.
*  These initializations >MUST< be performed:                    .
*  #EarthRad is the Earth's radius in whatever units you please; .
*  the calculated distance will be in those units:               .
*              6,372.7976    km,                                 .
*              3,959.873     statute miles,                      .
*              3,441.035     nautical miles.                     .
.  COMPUTE
   #EarthRad =  3959.873  /* statute miles */.

*  #AngleCvt is the number of your angle units (degrees, here)   .
*  in one of SPSS's angle units (radians). It uses that          .
*  ARCTAN(1)is PI/4 radians or (in any angle measure) 1/8 circle .
.
.  COMPUTE
   #AngleCvt =  360  /* Number of input units in a full circle */
              /(8*ARTAN(1)).

END IF.

*  ...............   Compute distance          ................  .
*  Compute distance between points with coordinates              .
*  (lat1,lon1) and (lat2,lon2)                                   .

compute distance = #EarthRad*
    (2*artan(1)-arsin(      sin(lat1/#AngleCvt)   /* (sin(lat1)  */
                           *sin(lat2/#AngleCvt)   /* .sin(lat2)  */
                         +  cos(lat1/#AngleCvt)   /* +cos(lat1)  */
                           *cos(lat2/#AngleCvt)   /* .cos(lat2)  */
                           *cos(lon2/#AngleCvt    /* .cos(long2  */
                               -lon1/#AngleCvt)   /*     -long1))*/
                      )).

FORMAT    Distance (F7.2).


===== (2) Using Python code ====================================
From Peck, Jon, "Re: Function for arc cosine", to SPSSX-L Thu, 7 Jun 2007 09:53:06 -0500""

"In the extendedTransforms module on SPSS Developer Central ( www.spss.com/devcentral), there are two functions that implement distance calculations on Earth latitude and longitude coordinates.

sphDist:     calculate distance between two points on earth using spherical approximation
ellipseDist: calculate distance between two points on earth using ellipsoidal approximation

"Here is a simple usage example for just a single distance pair.
.............

begin program.
import spss
import extendedTransforms

fromloc = (41.90, 87.65)
toloc = (41.73, 71.43)
dist1 = extendedTransforms.ellipseDist(fromloc[0], fromloc[1], toloc[0], toloc[1], inradians=False)
dist2 = extendedTransforms.sphDist(fromloc[0], fromloc[1], toloc[0], toloc[1], inradians=False)
print dist1, dist2

end program.

(3) =====  Test data, and test run  =================================
From Richard Ristow, loc cit. The 'given' values which are compared with the calculation are,

. Providence to Chicago distance, from Jon Peck's posting "Re: Function for arc cosine", Thu, 7 Jun 2007 09:53:06 -0500

. Others, arbitrary test points with distance calculated at site http://www.movable-type.co.uk/scripts/latlong.html. It's not clear why tiny discrepancies remain.


DATA LIST LIST /
   City1    lat1    lon1 City2    lat2    lon2   GivenDist
   (A4,     F6.2,   F6.2,A4,      F6.2,   F6.2,  F7.2).
BEGIN DATA
   Pvd     41.90   87.65 Chi     41.73   71.43   836.27
   A1      42.00   80.00 A2      39.00   70.00   564.33
   B1      44.00   70.00 B2      49.00   85.00   791.00
END DATA.

.  /*--  LIST /*-*/.


*  ...............   Initialize constants      ................. .

*  The following code >REQUIRES< that angles in the base system  .
*  (SPSS) be in radians, so that the trigonometric distance is   .
*  in radians, and can be multiplied directly by the Earth's     .
*  radius.                                                       .

DO IF $CASENUM EQ 1.
*  These initializations >MUST< be performed:                    .
*  #EarthRad is the Earth's radius in whatever units you please; .
*  the calculated distance will be in those units:               .
*              6,372.7976    km,                                 .
*              3,959.873     statute miles,                      .
*              3,441.035     nautical miles.                     .
.  COMPUTE
   #EarthRad =  3959.873  /* statute miles */.

*  #AngleCvt is the number of your angle units (degrees, here)   .
*  in one of SPSS's angle units (radians). It uses that          .
*  ARCTAN(1)is PI/4 radians or (in any angle measure) 1/8 circle .
.
.  COMPUTE
   #AngleCvt =  360  /* Number of input units in a full circle */
              /(8*ARTAN(1)).

END IF.

*  ...............   Compute distance          ................  .
*  Compute distance between points with coordinates              .
*  (lat1,lon1) and (lat2,lon2)                                   .

compute distance = #EarthRad*
    (2*artan(1)-arsin(      sin(lat1/#AngleCvt)   /* (sin(lat1)  */
                           *sin(lat2/#AngleCvt)   /* .sin(lat2)  */
                         +  cos(lat1/#AngleCvt)   /* +cos(lat1)  */
                           *cos(lat2/#AngleCvt)   /* .cos(lat2)  */
                           *cos(lon2/#AngleCvt    /* .cos(long2  */
                               -lon1/#AngleCvt)   /*     -long1))*/
                      )).

FORMAT    GivenDist(F7.2).

COMPUTE   DeltaPct = 100*(Distance/GivenDist-1).
FORMATS   DeltaPct (PCT7.2).

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |29-MAR-2009 00:55:20       |
|-----------------------------|---------------------------|
City1    lat1    lon1 City2    lat2    lon2 GivenDist distance DeltaPct

Pvd     41.90   87.65 Chi     41.73   71.43   836.27    834.33    -.23%
A1      42.00   80.00 A2      39.00   70.00   564.33    564.52     .03%
B1      44.00   70.00 B2      49.00   85.00   791.00    791.03     .00%

Number of cases read:  3    Number of cases listed:  3

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Calculating distance between latitude and longitude

Peck, Jon

Don't forget that the Python extendedTransforms module on Developer Central (www.spss.com/devcentral) has functions for this using both spherical and elliptical approximations.

-Jon Peck


From: SPSSX(r) Discussion <[hidden email]>
To: [hidden email] <[hidden email]>
Sent: Sun Mar 29 00:57:59 2009
Subject: Re: [SPSSX-L] Calculating distance between latitude and longitude

At 12:06 PM 3/27/2009, Justin Meyer wrote:

I am trying to calculate the distance between two latitude/longitude points in this form: 40.687870, -89.680280. I found syntax to do this here: http://www.listserv.uga.edu/cgi-bin/wa?A2=ind0810&L=spssx-l&P=53744 . This syntax produces errors.

That posting is mine, and indeed it isn't right.  As you wrote, correctly

SPSS does not recognize the arcos in [the distance formula].

Quite right. I posted that code untested; take warning from my sad case! The corrected, tested version is hard to find. (It's in a thread called "zip code distance".)

This may be worth having as a FAQ. So, please let me know of any problems with this draft.


FAQ: Computing distance from latitude and longitude (DRAFT)

Sections below are
===== (1) Solution in native SPSS transformation code
===== (2) Using Python code from Developer Central
===== (3) Test data and test run of native SPSS code


===== (1) Native SPSS code =====================================
From Richard Ristow, "Re: zip code distance", to SPSSX-L Tue, 28 Oct 2008 20:40:17 -0400.  Earth-radius values are from
http://en.wikipedia.org/wiki/Earth_radius; see also
http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html.

*  ...............   Initialize constants      ................. .


*  The following code >REQUIRES< that angles in the base system  .
*  (SPSS) be in radians, so that the trigonometric distance is   .
*  in radians, and can be multiplied directly by the Earth's     .
*  radius.                                                       .

DO IF $CASENUM EQ 1.
*  These initializations >MUST< be performed:                    .
*  #EarthRad is the Earth's radius in whatever units you please; .
*  the calculated distance will be in those units:               .
*              6,372.7976    km,                                 .
*              3,959.873     statute miles,                      .
*              3,441.035     nautical miles.                     .
.  COMPUTE
   #EarthRad =  3959.873  /* statute miles */.

*  #AngleCvt is the number of your angle units (degrees, here)   .
*  in one of SPSS's angle units (radians). It uses that          .
*  ARCTAN(1)is PI/4 radians or (in any angle measure) 1/8 circle .
.
.  COMPUTE
   #AngleCvt =  360  /* Number of input units in a full circle */
              /(8*ARTAN(1)).

END IF.

*  ...............   Compute distance          ................  .
*  Compute distance between points with coordinates              .
*  (lat1,lon1) and (lat2,lon2)                                   .

compute distance = #EarthRad*
    (2*artan(1)-arsin(      sin(lat1/#AngleCvt)   /* (sin(lat1)  */
                           *sin(lat2/#AngleCvt)   /* .sin(lat2)  */
                         +  cos(lat1/#AngleCvt)   /* +cos(lat1)  */
                           *cos(lat2/#AngleCvt)   /* .cos(lat2)  */
                           *cos(lon2/#AngleCvt    /* .cos(long2  */
                               -lon1/#AngleCvt)   /*     -long1))*/
                      )).

FORMAT    Distance (F7.2).


===== (2) Using Python code ====================================
From Peck, Jon, "Re: Function for arc cosine", to SPSSX-L Thu, 7 Jun 2007 09:53:06 -0500""

"In the extendedTransforms module on SPSS Developer Central ( www.spss.com/devcentral), there are two functions that implement distance calculations on Earth latitude and longitude coordinates.

sphDist:     calculate distance between two points on earth using spherical approximation
ellipseDist: calculate distance between two points on earth using ellipsoidal approximation

"Here is a simple usage example for just a single distance pair.
.............

begin program.
import spss
import extendedTransforms

fromloc = (41.90, 87.65)
toloc = (41.73, 71.43)
dist1 = extendedTransforms.ellipseDist(fromloc[0], fromloc[1], toloc[0], toloc[1], inradians=False)
dist2 = extendedTransforms.sphDist(fromloc[0], fromloc[1], toloc[0], toloc[1], inradians=False)
print dist1, dist2

end program.

(3) =====  Test data, and test run  =================================
From Richard Ristow, loc cit. The 'given' values which are compared with the calculation are,

. Providence to Chicago distance, from Jon Peck's posting "Re: Function for arc cosine", Thu, 7 Jun 2007 09:53:06 -0500

. Others, arbitrary test points with distance calculated at site http://www.movable-type.co.uk/scripts/latlong.html. It's not clear why tiny discrepancies remain.


DATA LIST LIST /
   City1    lat1    lon1 City2    lat2    lon2   GivenDist
   (A4,     F6.2,   F6.2,A4,      F6.2,   F6.2,  F7.2).
BEGIN DATA
   Pvd     41.90   87.65 Chi     41.73   71.43   836.27
   A1      42.00   80.00 A2      39.00   70.00   564.33
   B1      44.00   70.00 B2      49.00   85.00   791.00
END DATA.

.  /*--  LIST /*-*/.


*  ...............   Initialize constants      ................. .

*  The following code >REQUIRES< that angles in the base system  .
*  (SPSS) be in radians, so that the trigonometric distance is   .
*  in radians, and can be multiplied directly by the Earth's     .
*  radius.                                                       .

DO IF $CASENUM EQ 1.
*  These initializations >MUST< be performed:                    .
*  #EarthRad is the Earth's radius in whatever units you please; .
*  the calculated distance will be in those units:               .
*              6,372.7976    km,                                 .
*              3,959.873     statute miles,                      .
*              3,441.035     nautical miles.                     .
.  COMPUTE
   #EarthRad =  3959.873  /* statute miles */.

*  #AngleCvt is the number of your angle units (degrees, here)   .
*  in one of SPSS's angle units (radians). It uses that          .
*  ARCTAN(1)is PI/4 radians or (in any angle measure) 1/8 circle .
.
.  COMPUTE
   #AngleCvt =  360  /* Number of input units in a full circle */
              /(8*ARTAN(1)).

END IF.

*  ...............   Compute distance          ................  .
*  Compute distance between points with coordinates              .
*  (lat1,lon1) and (lat2,lon2)                                   .

compute distance = #EarthRad*
    (2*artan(1)-arsin(      sin(lat1/#AngleCvt)   /* (sin(lat1)  */
                           *sin(lat2/#AngleCvt)   /* .sin(lat2)  */
                         +  cos(lat1/#AngleCvt)   /* +cos(lat1)  */
                           *cos(lat2/#AngleCvt)   /* .cos(lat2)  */
                           *cos(lon2/#AngleCvt    /* .cos(long2  */
                               -lon1/#AngleCvt)   /*     -long1))*/
                      )).

FORMAT    GivenDist(F7.2).

COMPUTE   DeltaPct = 100*(Distance/GivenDist-1).
FORMATS   DeltaPct (PCT7.2).

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |29-MAR-2009 00:55:20       |
|-----------------------------|---------------------------|
City1    lat1    lon1 City2    lat2    lon2 GivenDist distance DeltaPct

Pvd     41.90   87.65 Chi     41.73   71.43   836.27    834.33    -.23%
A1      42.00   80.00 A2      39.00   70.00   564.33    564.52     .03%
B1      44.00   70.00 B2      49.00   85.00   791.00    791.03     .00%

Number of cases read:  3    Number of cases listed:  3

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD