ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Calculate The Distance Between Two Latitudes / Longitudes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculate The Distance Between Two Latitudes / Longitudes

    This demo program will calculate the distance between two points on Earth, using the latitude and longitude positions.

    The main calculation routine is the following code;

    PHP Code:
            distance =
               
    Math_ArcCosine(
               
    Math_Sine(   Math_DegreesToRadiansLatitude1 )) *
               
    Math_Sine(   Math_DegreesToRadiansLatitude2 )) +
               
    Math_CoSineMath_DegreesToRadiansLatitude1 )) *
               
    Math_CoSineMath_DegreesToRadiansLatitude2 )) *
               
    Math_CoSineMath_DegreesToRadiansLongitude1 Longitude2 ))) *
               
    3959
    This calculation returns the distance between two points in miles. If you would prefer for it to return kilometers as a default, then change the 3959 number to 6371. These two numbers are the radius of the Earth. ( 3,959 miles / 6,371 kM)

    This routine can return a highly accurate value, less than a tenth of an inch. One way to test this out is to use google earth to find a football stadium that has a very high resolution image of it, so that you can easily measure the distance from one end zone to the other. I used this on the Liberty Bowl stadium in memphis, and consistently get 300.25 feet. Although the typical application for this is to get the distance between zipcodes or cities, its fun to go through Egypt and measure the distance of the sides of a pyramid.

    If you want to use Google Earth to try it out, remember to set Google Earths latitude and longitude display to decimal. ( click tools, then options, then set the lat/lon section to decimal latitude)

    The demo program checks the distance value, and if it is less than one mile, converts the value into feet.

    The default start up positions are for New York City and Los Angeles.

    Program - Distance
    Code:
          * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
          *
          *  D i s t a n c e   D e m o   P r o g r a m
          *
          *          @copyrite 1997, 2009 Michael Catalani
          *           ProvatoSys  www.ProvatoSys.com
          *           mcatalani@aol.com
          *           901.581.8791
          *
          *  Get the distance in miles (or feet if less than one mile) for two
          *  latitude / longitude coordinates.
          *
          *
          *  Note: Latitude and Longitude coordiantes are in decimal form.  You
          *        can use Google Earth to get the decimal coordinates for any
          *        location.  To force Google Earth to display decimal lat / long,
          *        click the tools tab, then the options tab, then set the "show
          *        lat/lon" section to "decimal degrees."
          *
          *        By using the floating point data type, a highly accurate
          *        distance calculation can be performed, to less than 1/10 of
          *        an inch.
          *
          *        To test out the accuracy, use google earth to locate a football
          *        stadium that has a high resolution.  Click the inside corners
          *        of the endzone line on the same side of the field, and key in
          *        latitudes and longitudes. You should get a value very close to
          *        300 feet if the image resolution was really good, you are
          *        accurately positioning the pointer in google earth, and the
          *        guy who lined the football field was not drunk.
          *
          *
          * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    
         h dftactgrp(*no)
    
         fDistanceD cf   e             workstn
          * Program Variables
         d latitude1       s              8f
         d longitude1      s              8f
         d latitude2       s              8f
         d longitude2      s              8f
         d TotalMiles      s              7p 2
         d TotalFeet       s              7p 2
         d Miles           s              8f
    
          * Constants
         d pi              c                   3.14159265358979323846
    
          * ProtoTypes
         d Distance_PointToPoint...
         d                 pr             8f
         d  Latitude1                     8f   const
         d  Longitude1                    8f   const
         d  Latitude2                     8f   const
         d  Longitude2                    8f   const
    
         d Math_ArcCosine  pr             8f   Extproc( 'acos' )
         d  VariableX                     8f   value
    
         d Math_Cosine     pr             8f   Extproc( 'cos' )
         d  VariableX                     8f   value
    
         d Math_Sine       pr             8f   Extproc( 'sin' )
         d  VariableX                     8f   value
    
         d Math_DegreesToRadians...
         d                 pr             8f
         d   AngleDegrees                 8f   const
    
    
          /free
    
             // Set the default coordinates for New York and Los Angeles
             dsplat1 = 40.7167;
             dsplon1 = -74.0167;
             dsplat2 = 34.05;
             dsplon2 = -118.233;
    
             dow *in03 = *off;
    
               exfmt screen01;
    
               if *in03 = *on;
                 *inlr = *on;
                 return;
               endif;
    
               Latitude1= dsplat1;
               Longitude1= dsplon1;
               Latitude2= dsplat2;
               Longitude2= dsplon2;
    
               Miles = distance_PointToPoint(  Latitude1
                                             : Longitude1
                                             : Latitude2
                                             : Longitude2 );
    
               if Miles >= 1;
                 TotalMiles = Miles;
                 Distance = %editc( TotalMiles : '1' ) + ' Miles';
               else;
                 TotalFeet = Miles * 5280;
                 Distance = %editc( ( TotalFeet )  : '1' ) + ' Feet';
               endif;
    
             enddo;
          /end-free
          * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
          *
          *  D i s t a n c e _ P o i n t T o P o i n t
          *
          *          Finds The Distance Between Two Latitude and Longitude Points
          *
          *          Input Field - Latitude 1        ( floating point )
          *                        Longitude 1       ( floating point )
          *                        Latitude 2        ( floating point )
          *                        Longitude 2       ( floating point )
          *          Returns     - Distance in Miles ( floating point )
          *
          * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
         p Distance_PointToPoint...
         p                 b
         d Distance_PointToPoint...
         d                 pi             8f
         d  Latitude1                     8f   const
         d  Longitude1                    8f   const
         d  Latitude2                     8f   const
         d  Longitude2                    8f   const
    
         d Distance        s              8f
    
          /free
    
            distance =
               Math_ArcCosine(
               Math_Sine(   Math_DegreesToRadians( Latitude1 )) *
               Math_Sine(   Math_DegreesToRadians( Latitude2 )) +
               Math_CoSine( Math_DegreesToRadians( Latitude1 )) *
               Math_CoSine( Math_DegreesToRadians( Latitude2 )) *
               Math_CoSine( Math_DegreesToRadians( Longitude1 - Longitude2 ))) *
               3959;
    
            Return Distance;
    
          /end-free
         p                 e
    
          * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
          *
          *  M a t h _ D e g r e e s T o R a d i a n s
          *
          *          Convert Degrees to Radians
          *
          *          Input Field - Degrees ( floating point )
          *          Returns     - Radians ( floating point )
          *
          * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
         p Math_DegreesToRadians...
         p                 b
         d Math_DegreesToRadians...
         d                 pi             8f
         d   AngleDegrees                 8f   const
         d  Radians        s              8f
    
          /free
    
            Radians  = pi * AngleDegrees / 180;
            Return Radians;
    
          /end-free
         p                 e
    Display File - DistanceD

    Code:
          A*%%TS  SD  20090324  100030  QPGMR       REL-V6R1M0  5761-WDS
         A*%%EC
         A                                      DSPSIZ(24 80 *DS3)
         A                                      CF03(03)
         A          R SCREEN01
         A*%%TS  SD  20090324  100030  QPGMR       REL-V6R1M0  5761-WDS
         A                                  1  2DATE
         A                                      EDTCDE(Y)
         A                                      COLOR(PNK)
         A                                  2  2TIME
         A                                      COLOR(PNK)
         A                                  1 70USER
         A                                      COLOR(PNK)
         A                                  7  3'Longitude 1..............:'
         A                                      COLOR(BLU)
         A                                  6  3'Latitude  1..............:'
         A                                      COLOR(BLU)
         A                                 23  3'F3-Exit'
         A                                      COLOR(WHT)
         A                                  8  3'Latitude  2..............:'
         A                                      COLOR(BLU)
         A                                  9  3'Longitude 2..............:'
         A                                      COLOR(BLU)
         A                                 11  3'Distance ................:'
         A                                      COLOR(BLU)
         A            DSPLAT1       15Y12B  6 30COLOR(WHT)
         A                                      EDTCDE(M)
         A            DSPLON1       15Y12B  7 30COLOR(WHT)
         A                                      EDTCDE(M)
         A            DSPLAT2       15Y12B  8 30COLOR(WHT)
         A                                      EDTCDE(M)
         A            DSPLON2       15Y12B  9 30COLOR(WHT)
         A                                      EDTCDE(M)
         A            DISTANCE      50A  O 11 30COLOR(PNK)
         A                                  1 13'Calculate Distance Between Two Lat-
         A                                      itudes / Longitudes'
         A                                      COLOR(BLU)
    Michael Catalani
    IS Director, eCommerce & Web Development
    Acceptance Insurance Corporation
    www.AcceptanceInsurance.com
    www.ProvatoSys.com

  • #2
    Re: Calculate The Distance Between Two Latitudes / Longitudes

    Nice ! Now add hotlink and we're gdone !
    Code:
    **  Generate a hotlink to GOOGLE maps and show map from City1 to City2                                             
    c                   IF        CITY1 <> *blanks   And                              
    c                             CITY2 <> *blanks                                    
    c                   EVAL      URL ='http://maps.google.com/maps?' + 
    c                                   'saddr=' +      
    c                                    &#37;TRIM(CITY1) +  
    c                                   '&daddr=' +                         
    c                                    %TRIM(CITY2)                                        
    c                   ENDIF

    Comment


    • #3
      Re: Calculate The Distance Between Two Latitudes / Longitudes

      Since the only dimension of the earth you mention is radius does this mean that you assume the earth is a perfect sphere? If this is the case then there is no way it can be accurate to a tenth of an inch! I believe Google use a spherical mercator co-ordinate projection system themselves so that's not really a scientific test.

      I've been working on the development of a GIS system recently and I can tell you that the error bounds are more likely to be about 3-5 meters rather than 0.1 of an inch. Still good enough for most applications it has to be said.
      Ben

      Comment


      • #4
        Re: Calculate The Distance Between Two Latitudes / Longitudes

        Originally posted by BenThurley View Post
        Since the only dimension of the earth you mention is radius does this mean that you assume the earth is a perfect sphere? If this is the case then there is no way it can be accurate to a tenth of an inch! I believe Google use a spherical mercator co-ordinate projection system themselves so that's not really a scientific test.

        I've been working on the development of a GIS system recently and I can tell you that the error bounds are more likely to be about 3-5 meters rather than 0.1 of an inch. Still good enough for most applications it has to be said.
        Correct, what I was referring to is that the calculations can return a highly accurate value of less than a tenth of an inch due to the use of floating point data types. This more reflects the precision of the number, however, its still only as good as the calculation itself.

        If you can get highly accurate lat/long positions, then 1/10 of an inch accuracy is possibly for shorter distances. ( ie: distances of hundreds of feet.) The further the distance, and the further the radius of the earth in that area varies from the mean radius, the more inaccurate the value. Distances of thousands of miles can certainly be off by meters to a couple of miles.

        Accuracies of a tenth of an inch wont be possible using Google Earth, because the images themselves aren't of a good enough resolution to accurately do this, even if their lat/longs were accurate to a tenth of an inch. I am surprised that the precision is as accurate as it appears to be on Google Earth, though.
        Michael Catalani
        IS Director, eCommerce & Web Development
        Acceptance Insurance Corporation
        www.AcceptanceInsurance.com
        www.ProvatoSys.com

        Comment


        • #5
          Re: Calculate The Distance Between Two Latitudes / Longitudes

          Okay -- I know Im a dinosaur, but exactly how do I compile this monster with the advance math functions...
          Ehat the Hell am I missing?

          Please send more COPS!
          Jamie
          All my answers were extracted from the "Big Dummy's Guide to the As400"
          and I take no responsibility for any of them.

          www.code400.com

          Comment


          • #6
            Re: Calculate The Distance Between Two Latitudes / Longitudes

            Originally posted by jamief View Post
            Okay -- I know Im a dinosaur, but exactly how do I compile this monster with the advance math functions...
            Ehat the Hell am I missing?

            Please send more COPS!
            Jamie

            If you are on V6, then it should compile as is. If you are back on V5, you need to change the "H" spec to include the QC2LE binding directory:

            h dftactgrp(*no) bnddir( 'QC2LE' )
            Michael Catalani
            IS Director, eCommerce & Web Development
            Acceptance Insurance Corporation
            www.AcceptanceInsurance.com
            www.ProvatoSys.com

            Comment


            • #7
              Re: Calculate The Distance Between Two Latitudes / Longitudes

              Michael,

              Thanks (I should have known that ) all fine now ---
              Cancel last order of COPS!


              Jamie
              All my answers were extracted from the "Big Dummy's Guide to the As400"
              and I take no responsibility for any of them.

              www.code400.com

              Comment

              Working...
              X