Project

General

Profile

Paste
Download (6.26 KB) Statistics
| Branch: | Revision:

root / class / site_point.class.php @ c5f89cd9

1
<?php
2
require_once(dirname(__FILE__).'/../constants.inc.php');
3

    
4
//
5
class PanoramaFormatException extends Exception {
6
        /** If the files organization is not correct for a panorama, we can't let it go...
7
         */
8
}
9

    
10
class site_point {
11
  /** Defines a point, with a panorama
12
  */
13
  private $base_dir;        // dir of tiles for that panorama
14
  private $name = false;
15
  private $prefix = false;
16
  private $params = false;
17
  private $zooms;
18

    
19
  public static $REF_KEY = 'reference';
20

    
21
  public function __construct($dir) {
22
    $this->base_dir = $dir;
23
    $this->prefix = basename($dir);
24
  }
25

    
26
  public function params_path() {
27
          return $this->base_dir.'/'.$this->prefix.'.params';
28
  }
29

    
30
  public function tiles_path() {
31
          return $this->base_dir;
32
  }
33

    
34
  public function tiles_prefix() {
35
          return $this->base_dir.'/'.$this->get_prefix();
36
  }
37

    
38
  private function parse_and_cache_params() {
39
    if (is_file($this->params_path())) {
40
            $params = parse_ini_file($this->params_path());
41
            if ($params) {
42
                    $this->params = $params;
43
                    return $params;
44
            }
45
    }
46
    return array();
47
  }
48

    
49
  public function get_params() {
50
          // the params are cached
51
          if (isset($this->params) && $this->params) {
52
                  return $this->params;
53
          } else {
54
                  return $this->parse_and_cache_params();
55
          }
56
  }
57

    
58
  public function save_params() {
59
          $o = '';
60
          $p = $this->get_params();
61
          foreach ($this->get_params() as $k => $v) {
62
                  if ($k == self::$REF_KEY) {
63
                          foreach ($v as $refk => $refv) {
64
                                  $o.= sprintf("%s[\"%s\"] = %.5f,%.5f\n",
65
                                               self::$REF_KEY, $refk,
66
                                               $refv[0], $refv[1]);
67
                          }
68
                  } else {
69
                          $o.= "$k = $v\n";
70
                  }
71
          }
72
          file_put_contents($this->params_path(), $o);
73
  }
74

    
75
  public function set_param($key, $value) {
76
          $p = $this->get_params();
77
          $this->params[$key] = $value;
78
          if ($key == 'titre') {
79
                  $this->name = $value;
80
          }
81
  }
82

    
83
  public function has_params(){
84
          $p = $this->get_params();
85
          return (isset($p['latitude'], $p['longitude'],
86
                        $p['altitude'], $p['titre']));
87
  }
88

    
89
  public function get_name() {
90
    return basename($this->base_dir);
91
  }
92

    
93
  public function get_prefix() {
94
    return $this->prefix;
95
  }
96

    
97
  public function get_magnifications() {
98
    $dir_fd = opendir($this->base_dir);
99
    $zoom_array = array();
100
    while (false !== ($file = readdir($dir_fd))) {                // extraction des paramètres de grossissement par le serveur
101
       //echo $file;
102
       if (preg_match('/(.*)_([0-9]+)_([0-9]+)_([0-9]+)\.jpg$/', $file, $reg)) {
103
         $prefix = $reg[1];
104
         if ($prefix == $this->prefix) {
105
           $zoom = (int)$reg[2];
106
           $posx = (int)$reg[3]+1;
107
           $posy = (int)$reg[4]+1;
108
           if (!isset($zoom_array[$zoom]['nx']) || $zoom_array[$zoom]['nx'] < $posx) $zoom_array[$zoom]['nx'] = $posx;
109
           if (!isset($zoom_array[$zoom]['ny']) || $zoom_array[$zoom]['ny'] < $posy) $zoom_array[$zoom]['ny'] = $posy;
110
         }
111
       }
112
    }
113
    $this->zooms = $zoom_array;
114
    return $this->zooms;
115
  }
116

    
117
  public function coordsToCap($lat, $lon, $alt) {
118
    $params = $this->get_params();
119
    if (!isset($params['latitude']) || !isset($params['longitude'])) return false;
120
    $rt = 6371;  // Rayon de la terre
121
    $alt1 = isset($params['altitude']) ? $params['altitude'] : $alt;
122
    $lat1 = $params['latitude']*M_PI/180;
123
    $lon1 = $params['longitude']*M_PI/180;
124
    $alt2 = $alt;
125
    $lat2 = $lat * M_PI/180;
126
    $lon2 = $lon * M_PI/180;
127

    
128
    $dLat = $lat2-$lat1;
129
    $dLon = $lon2-$lon1;
130

    
131
    $a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);  //
132
    $angle = 2 * atan2(sqrt($a), sqrt(1-$a));
133
    $d = $angle * $rt;                    // distance du point en Kms
134

    
135
    $y = sin($dLon)*cos($lat2);
136
    $x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
137
    $cap = atan2($y, $x);                 // cap pour atteindre le point en radians
138

    
139
    $e = atan2(($alt2 - $alt1)/1000 - $d*$d/(2*$rt), $d);  // angle de l'élévation en radians
140
    //    printf("%s, %s, %s, %s\n",$lat1, $params['latitude'], $lat, $dLat);
141
    return array($d, $cap*180/M_PI, $e*180/M_PI);   // les résultats sont en degrés
142
  }
143

    
144
  public function get_url($cap=false, $ele=false) {
145
          $o = sprintf('panorama.php?dir=%s&panorama=%s',
146
                          PANORAMA_FOLDER, $this->get_name());
147
          if ($cap && $ele) {
148
                  $o .= sprintf("&to_cap=%.3f&to_ele=%.3f", $cap, $ele);
149
          }
150
          return $o;
151
  }
152

    
153
  public function set_reference($ref_point, $x, $y) {
154
          /**
155
           * Registers (for saving) the position of a reference point within a
156
           * panorama. It sets or overwrite a reference.
157
           *
158
           * @param $ref_point a RefPoint instance
159
           * @param $x the relative x position of the RefPoint
160
           * @param $x the relative y position of the RefPoint
161
           */
162
          $p = $this->get_params();
163

    
164
          if (!isset($this->params[self::$REF_KEY]) ||
165
              !is_array($this->params[self::$REF_KEY])) {
166
                  $this->params[self::$REF_KEY] = array();
167
          }
168
          $ref_name = $ref_point->name;
169
          $dict = $this->params[self::$REF_KEY];
170
          //$dddd = $this->params[self::$REF_KEY][$ref_name];
171
          $this->params[self::$REF_KEY][$ref_name] = array($x, $y);
172
  }
173

    
174
  public static function get($name) {
175
          /** Instantiate a site_point, given its name
176
           */
177
          $pano_dir = PANORAMA_PATH.'/'.$name;
178
          return new site_point($pano_dir);
179
  }
180

    
181

    
182
  public function to_geoJSON() {
183
          $prm = $this->get_params();
184
                $name = $this->get_name();
185
                $lat = floatval($prm['latitude']);
186
                $lon = floatval($prm['longitude']);
187
                //$alt = $prm['altitude'];
188
                //$title = $prm['titre'];
189

    
190
                return array("type" => "Feature",
191
                             "geometry" => array(
192
                                                 "type" => "Point",
193
                                                 "coordinates" => [$lon, $lat]
194
                                                 ),
195
                             "properties" => array("name" => $name,
196
                                                   "type" => 'pano_point',
197
                                                   "view_url"  => $this->get_url())
198
                             );
199
  }
200

    
201

    
202
  public static function get_all($only_with_params=false) {
203
          /**
204
           * @param $only_with_params : filters out the panoramas which are not parametrized
205
           */
206
          $panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
207
          $pano_instances = array();
208

    
209
          foreach ($panos as $pano_name) {
210
                  $pano =  site_point::get($pano_name);
211
                  if (! $only_with_params || $pano->has_params() ) {
212
                          $pano_instances[] = $pano;
213
                  }
214
          }
215
          return $pano_instances;
216
  }
217

    
218
}