source: subversion/sites/namefinder/php/greatcircle.php @ 17445

Last change on this file since 17445 was 4134, checked in by david, 12 years ago

Main application files

File size: 1.5 KB
Line 
1<?php 
2
3include_once('named.php');
4
5class greatcircle {
6
7  /* This class provides helper functions for computing great circle distances */
8
9  var $fromid;
10  var $toid;
11  var $distance;
12
13  // --------------------------------------------------
14  /* static */ function earth_radius() {
15    return 6372.795;
16  }
17
18  // --------------------------------------------------
19  /* constructor */ function greatcircle($named1, $named2) {
20    /* Calculates the great circle distance between two objects of class named -
21       see http://en.wikipedia.org/wiki/Great-circle_distance */
22    $lat1 = deg2rad($named1->lat); $lat2 = deg2rad($named2->lat);
23    $lon1 = deg2rad($named1->lon); $lon2 = deg2rad($named2->lon);
24    $dlon = $lon2 - $lon1;
25    $this->distance =
26      greatcircle::earth_radius() *
27      atan2(
28            sqrt(
29                 pow(cos($lat2) * sin($dlon), 2) +
30                 pow(cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($dlon), 2)
31            )
32            ,
33            sin($lat1) * sin($lat2) + 
34            cos($lat1) * cos($lat2) * cos($dlon)
35      );
36    $this->fromid = $named1->id;
37    $this->toid = $named2->id;
38  }
39
40  // --------------------------------------------------
41  function xmlise() {
42    $fromid = canon::getosmid($this->fromid, $fromtype);
43    $toid = canon::getosmid($this->toid, $totype);
44    return sprintf(" <distance fromtype='%s' from='%d' totype='%s' to='%d'>%0.1f</distance>\n",
45                   $fromtype, $fromid, $totype, $toid, $this->distance);
46  }
47}
48
49?>
Note: See TracBrowser for help on using the repository browser.