Project

General

Profile

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

root / class / site_point.class.php @ 3d8d154e

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

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

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

    
20
  public static $REF_KEY = 'reference';
21

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

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

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

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

    
39
  private function parse_and_cache_params() {
40
    if (is_file($this->params_path())) {
41
            $params = parse_ini_file($this->params_path());
42
            if ($params) {
43
                    $this->params = $params;
44
                    if (isset($params[self::$REF_KEY])) {
45
                            foreach ($params[self::$REF_KEY] as $ref => $vals) {
46
                                    $bits = explode(',',$vals);
47
                                    $this->params[self::$REF_KEY][$ref] = array(floatval($bits[0]),
48
                                                                                floatval($bits[1]));
49
                            }
50
                    }
51
                    if (isset($params['image_loop'])) {
52
                            $this->params['image_loop'] = (bool)($params['image_loop']);
53
                    }
54
                    return $params;
55
            }
56
    }
57
    return array();
58
  }
59

    
60
  public function get_params() {
61
          // the params are cached
62
          if (isset($this->params) && $this->params) {
63
                  return $this->params;
64
          } else {
65
                  return $this->parse_and_cache_params();
66
          }
67
  }
68

    
69
  public function save_params() {
70
          $o = '';
71
          $p = $this->get_params();
72
          foreach ($this->get_params() as $k => $v) {
73
                  if ($k == self::$REF_KEY) {
74
                          foreach ($v as $refk => $refv) {
75
                                  $o.= sprintf("%s[\"%s\"] = %.5f,%.5f\n",
76
                                               self::$REF_KEY, $refk,
77
                                               $refv[0], $refv[1]);
78
                          }
79
                  } else {
80
                          $o.= "$k = ".utils::php2ini($v)."\n";
81
                  }
82
          }
83
          file_put_contents($this->params_path(), $o);
84
  }
85

    
86
  public function set_param($key, $value) {
87
          $p = $this->get_params();
88
          $this->params[$key] = $value;
89
          if ($key == 'titre') {
90
                  $this->name = $value;
91
          }
92
  }
93

    
94
  public function has_params(){
95
          $p = $this->get_params();
96
          return (isset($p['latitude'], $p['longitude'],
97
                        $p['altitude'], $p['titre']));
98
  }
99

    
100
  public function get_name() {
101
    return basename($this->base_dir);
102
  }
103

    
104
  public function get_prefix() {
105
    return $this->prefix;
106
  }
107

    
108
  public function get_magnifications() {
109
    $dir_fd = opendir($this->base_dir);
110
    $zoom_array = array();
111
    while (false !== ($file = readdir($dir_fd))) {                // extraction des paramètres de grossissement par le serveur
112
       //echo $file;
113
       if (preg_match('/(.*)_([0-9]+)_([0-9]+)_([0-9]+)\.jpg$/', $file, $reg)) {
114
         $prefix = $reg[1];
115
         if ($prefix == $this->prefix) {
116
           $zoom = (int)$reg[2];
117
           $posx = (int)$reg[3]+1;
118
           $posy = (int)$reg[4]+1;
119
           if (!isset($zoom_array[$zoom]['nx']) || $zoom_array[$zoom]['nx'] < $posx) $zoom_array[$zoom]['nx'] = $posx;
120
           if (!isset($zoom_array[$zoom]['ny']) || $zoom_array[$zoom]['ny'] < $posy) $zoom_array[$zoom]['ny'] = $posy;
121
         }
122
       }
123
    }
124
    $this->zooms = $zoom_array;
125
    return $this->zooms;
126
  }
127

    
128
  public function coordsToCap($lat, $lon, $alt) {
129
    $params = $this->get_params();
130
    if (!isset($params['latitude']) || !isset($params['longitude'])) return false;
131
    $rt = 6371;  // Rayon de la terre
132
    $alt1 = isset($params['altitude']) ? $params['altitude'] : $alt;
133
    $lat1 = $params['latitude']*M_PI/180;
134
    $lon1 = $params['longitude']*M_PI/180;
135
    $alt2 = $alt;
136
    $lat2 = $lat * M_PI/180;
137
    $lon2 = $lon * M_PI/180;
138

    
139
    $dLat = $lat2-$lat1;
140
    $dLon = $lon2-$lon1;
141

    
142
    $a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);  //
143
    $angle = 2 * atan2(sqrt($a), sqrt(1-$a));
144
    $d = $angle * $rt;                    // distance du point en Kms
145

    
146
    $y = sin($dLon)*cos($lat2);
147
    $x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
148
    $cap = atan2($y, $x);                 // cap pour atteindre le point en radians
149

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

    
155
  public function get_url($cap=false, $ele=false) {
156
          $o = sprintf('panorama.php?dir=%s&panorama=%s',
157
                          PANORAMA_FOLDER, $this->get_name());
158
          if ($cap && $ele) {
159
                  $o .= sprintf("&to_cap=%.3f&to_ele=%.3f", $cap, $ele);
160
          }
161
          return $o;
162
  }
163

    
164
  public function set_reference($ref_point, $x, $y) {
165
          /**
166
           * Registers (for saving) the position of a reference point within a
167
           * panorama. It sets or overwrite a reference.
168
           *
169
           * @param $ref_point a RefPoint instance
170
           * @param $x the relative x position of the RefPoint
171
           * @param $x the relative y position of the RefPoint
172
           */
173
          $p = $this->get_params();
174

    
175
          if (!isset($this->params[self::$REF_KEY]) ||
176
              !is_array($this->params[self::$REF_KEY])) {
177
                  $this->params[self::$REF_KEY] = array();
178
          }
179
          $ref_name = $ref_point->name;
180
          $dict = $this->params[self::$REF_KEY];
181
          //$dddd = $this->params[self::$REF_KEY][$ref_name];
182
          $this->params[self::$REF_KEY][$ref_name] = array($x, $y);
183
  }
184

    
185
  public function unset_reference($ref_point) {
186
          /**
187
           * Unregisters a reference, within a panorama.
188
           * does nothing if the RefPoint is not registered.
189
           *
190
           * @param $ref_point a RefPoint instance
191
           */
192
          $p = $this->get_params();
193
          $ref_name = $ref_point->name;
194
          if (isset($p[self::$REF_KEY]) &&
195
              isset($p[self::$REF_KEY][$ref_name])) {
196
                  unset($this->params[self::$REF_KEY][$ref_name]);
197
          }
198
  }
199

    
200

    
201

    
202
  public static function get($name) {
203
          /** Instantiate a site_point, given its name
204
           */
205
          $pano_dir = PANORAMA_PATH.'/'.$name;
206
          return new site_point($pano_dir);
207
  }
208

    
209

    
210
  public function to_geoJSON() {
211
          $prm = $this->get_params();
212
                $name = $this->get_name();
213
                $lat = floatval($prm['latitude']);
214
                $lon = floatval($prm['longitude']);
215
                //$alt = $prm['altitude'];
216
                //$title = $prm['titre'];
217

    
218
                return array("type" => "Feature",
219
                             "geometry" => array(
220
                                                 "type" => "Point",
221
                                                 "coordinates" => array($lon, $lat)
222
                                                 ),
223
                             "properties" => array("name" => $name,
224
                                                   "type" => 'pano_point',
225
                                                   "view_url"  => $this->get_url())
226
                             );
227
  }
228

    
229

    
230
  public static function get_all($only_with_params=false) {
231
          /**
232
           * @param $only_with_params : filters out the panoramas which are not parametrized
233
           */
234
          $panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
235
          $pano_instances = array();
236

    
237
          foreach ($panos as $pano_name) {
238
                  $pano =  site_point::get($pano_name);
239
                  if (! $only_with_params || $pano->has_params() ) {
240
                          $pano_instances[] = $pano;
241
                  }
242
          }
243
          return $pano_instances;
244
  }
245

    
246
}