commit ffebe8560a628d484da10f2a4068b57a5442dcb2
Author: Jocelyn Delande <jocelyn@crapouillou.net>
Date:   Mon Dec 8 23:28:54 2014 +0100

    do not display ref_points and panorama further than 25km from the current one (tunable in constant)

diff --git a/class/GeoPoint.class.php b/class/GeoPoint.class.php
new file mode 100644
index 0000000..5feb27c
--- /dev/null
+++ b/class/GeoPoint.class.php
@@ -0,0 +1,51 @@
+<?php
+require_once(dirname(__FILE__).'/utils.class.php');
+
+interface LatLon {
+  public function get_lat();
+  public function get_lon();
+}
+
+interface Collection {
+  public static function get_all();
+}
+
+abstract class GeoPoint implements LatLon, Collection {
+  /**  distance, by Vincenty formula
+       see http://stackoverflow.com/a/10054282
+   */
+  public function distance($other_point) {
+    $earthRadius = 6371;
+
+	// convert from degrees to radians
+	$latFrom = deg2rad($this->get_lat());
+	$lonFrom = deg2rad($this->get_lon());
+	$latTo = deg2rad($other_point->get_lat());
+	$lonTo = deg2rad($other_point->get_lon());
+
+	$lonDelta = $lonTo - $lonFrom;
+	$a = pow(cos($latTo) * sin($lonDelta), 2) +
+	  pow(cos($latFrom) * sin($latTo) -
+      sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
+	$b = sin($latFrom) * sin($latTo) +
+      cos($latFrom) * cos($latTo) * cos($lonDelta);
+
+	$angle = atan2(sqrt($a), $b);
+	return $angle * $earthRadius;
+  }
+
+  /** Gets only the points in a circle of $distance km arround $opint
+
+     @param $target a LatLon
+   */
+  public static function near_points($target, $distance) {
+    $near_points = array();
+    foreach (static::get_all() as $point) {
+      if ($point->distance($target) < $distance) {
+        $near_points[] = $point;
+      }
+    }
+    return $near_points;
+  }
+}
+?>
diff --git a/class/RefPoint.class.php b/class/RefPoint.class.php
index bf162ac..0c18df4 100644
--- a/class/RefPoint.class.php
+++ b/class/RefPoint.class.php
@@ -1,8 +1,8 @@
 <?php
 require_once(dirname(__FILE__).'/../constants.inc.php');
-
+require_once(dirname(__FILE__).'/GeoPoint.class.php');
 //
-class RefPoint {
+class RefPoint extends GeoPoint {
   static $all_ref_points_cache;
   public $name;
   public $lon;
@@ -16,6 +16,14 @@ class RefPoint {
     $this->ele = $values[2];
   }
 
+  public function get_lat() {
+    return $this->lat;
+  }
+
+  public function get_lon() {
+    return $this->lon;
+  }
+
   public static function load_if_needed() {
     if (!isset(self::$all_ref_points_cache)) {
       if (file_exists(REF_POINTS_PATH)) {
diff --git a/class/site_point.class.php b/class/site_point.class.php
index 6f5d0a1..15416cd 100644
--- a/class/site_point.class.php
+++ b/class/site_point.class.php
@@ -1,13 +1,14 @@
 <?php
 require_once(dirname(__FILE__).'/../constants.inc.php');
 require_once(dirname(__FILE__).'/utils.class.php');
+require_once(dirname(__FILE__).'/GeoPoint.class.php');
 //
 class PanoramaFormatException extends Exception {
 	/** If the files organization is not correct for a panorama, we can't let it go...
 	 */
 }
 
-class site_point {
+class site_point extends GeoPoint {
   /** Defines a point, with a panorama
   */
   private $base_dir;        // dir of tiles for that panorama
@@ -121,6 +122,16 @@ class site_point {
     return $this->prefix;
   }
 
+  public function get_lat() {
+	$p = $this->get_params();
+	return $p['latitude'];
+  }
+
+  public function get_lon() {
+	$p = $this->get_params();
+	return $p['longitude'];
+  }
+
   public function get_magnifications() {
     $dir_fd = opendir($this->base_dir);
     $zoom_array = array();
@@ -288,7 +299,7 @@ class site_point {
   }
 
 
-  public static function get_all($only_with_params=false) {
+  public static function get_all($only_with_params=true) {
 	  /**
 	   * @param $only_with_params : filters out the panoramas which
 	   *        are not parametrized
diff --git a/class/utils.class.php b/class/utils.class.php
index 8b0dde8..291cf9f 100644
--- a/class/utils.class.php
+++ b/class/utils.class.php
@@ -90,5 +90,4 @@ abstract class utils {
     header("Location: http://$host$uri/$extra");
   }
 }
-
 ?>
diff --git a/constants.inc.php b/constants.inc.php
index 1a4befb..de2f27f 100644
--- a/constants.inc.php
+++ b/constants.inc.php
@@ -3,5 +3,7 @@ define('CELUTZ_PATH', realpath(dirname(__FILE__)));
 define('PANORAMA_FOLDER', 'tiles' );
 define('PANORAMA_PATH', CELUTZ_PATH.'/'.PANORAMA_FOLDER );
 define('UPLOAD_PATH', CELUTZ_PATH.'/upload' );
+// Do not display points further than that (in kms)
+define('MAX_SEEING_DISTANCE', 25);
 define('REF_POINTS_PATH', CELUTZ_PATH.'/'.'ref_points.local.php');
 ?>
diff --git a/index.php b/index.php
index 1a1e061..026f20e 100644
--- a/index.php
+++ b/index.php
@@ -24,7 +24,7 @@ if(isset($_GET['dir']) && is_dir($_GET['dir'])) {
 
 $dir = new sites_dir($base_dir);
 try {
-  $sites_list = site_point::get_all();
+  $sites_list = site_point::get_all(false);
 
   echo "<ul id=\"pano-list\">\n";
 
diff --git a/panorama.php b/panorama.php
index 34c4d5b..2e35cb6 100644
--- a/panorama.php
+++ b/panorama.php
@@ -12,17 +12,17 @@
     'to_ele'     => array('numeric'),
     'to_zoom'     => array('numeric')
   );
-  
+
   $validator = new FormValidator($fields_spec);
   $is_valid = $validator->validate($_GET);
-  
+
   if ($is_valid) {
     $input = $validator->sane_values();
   } else {
     $validator->print_errors();
     die();//fixme, could be cleaner
   }
-  
+
    $form_extpoint = file_get_contents('html/form_extpoint.html');
 
    $form_param = file_get_contents('html/form_param.html');
@@ -57,7 +57,7 @@
      printf ("var img_prefix = '%s/%s';\n", $base_dir, $prefix);
      if (is_array($params)) $opt_vals = array_merge($params, $opt_vals);
      foreach(array('to_cap', 'to_ele', 'to_zoom', 'image_loop') as $val) {
-        if (isset($opt_vals[$val])) 
+        if (isset($opt_vals[$val]))
         printf ('var '.$val.' = '.utils::php2ini($opt_vals[$val]).";\n"); // correction du décalage angulaire par rapport au Nord
      }
   ?>
@@ -83,7 +83,7 @@
    $dir_list = new sites_dir($dir);
 
    $ipt = 0;
-  foreach(site_point::get_all() as $opt) {
+   foreach(site_point::near_points($pt, MAX_SEEING_DISTANCE) as $opt) {
      $prm = $opt->get_params();
      $oname = $opt->get_name();
      if (($oname != $name) && $opt->has_params()) {
@@ -94,10 +94,9 @@
      }
    }
 
-   $ref_points = RefPoint::get_all();
    $extra_names = array();
    $ref_names = array();
-   foreach ($ref_points as $ref) {
+   foreach (RefPoint::near_points($pt, MAX_SEEING_DISTANCE) as $ref) {
      $extra_names[] = $ref->name;
      list($dist, $cap, $ele) = $pt->coordsToCap($ref->lon, $ref->lat,
                                                 $ref->ele);
