Projet

Général

Profil

Paste
Télécharger (1,28 ko) Statistiques
| Branche: | Révision:

root / class / GeoPoint.class.php @ b6a05986

1
<?php
2
require_once(dirname(__FILE__).'/utils.class.php');
3

    
4
interface LatLon {
5
  public function get_lat();
6
  public function get_lon();
7
}
8

    
9
interface Collection {
10
  public static function get_all();
11
}
12

    
13
abstract class GeoPoint implements LatLon, Collection {
14
  /**  distance, by Vincenty formula
15
       see http://stackoverflow.com/a/10054282
16
   */
17
  public function distance($other_point) {
18
    $earthRadius = 6371;
19

    
20
        // convert from degrees to radians
21
        $latFrom = deg2rad($this->get_lat());
22
        $lonFrom = deg2rad($this->get_lon());
23
        $latTo = deg2rad($other_point->get_lat());
24
        $lonTo = deg2rad($other_point->get_lon());
25

    
26
        $lonDelta = $lonTo - $lonFrom;
27
        $a = pow(cos($latTo) * sin($lonDelta), 2) +
28
          pow(cos($latFrom) * sin($latTo) -
29
      sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
30
        $b = sin($latFrom) * sin($latTo) +
31
      cos($latFrom) * cos($latTo) * cos($lonDelta);
32

    
33
        $angle = atan2(sqrt($a), $b);
34
        return $angle * $earthRadius;
35
  }
36

    
37
  /** Gets only the points in a circle of $distance km arround $opint
38

39
     @param $target a LatLon
40
   */
41
  public static function near_points($target, $distance) {
42
    $near_points = array();
43
    foreach (static::get_all() as $point) {
44
      if ($point->distance($target) < $distance) {
45
        $near_points[] = $point;
46
      }
47
    }
48
    return $near_points;
49
  }
50
}
51
?>