Projet

Général

Profil

Paste
Télécharger (8,95 ko) Statistiques
| Branche: | Révision:

root / class / site_point.class.php @ 6156a9b6

1
<?php
2
require_once(dirname(__FILE__).'/../constants.inc.php');
3
require_once(dirname(__FILE__).'/utils.class.php');
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_url_prefix() {
31
          return PANORAMA_FOLDER.'/'.$this->prefix;
32
  }
33

    
34
  public function tiles_path() {
35
          return $this->base_dir;
36
  }
37

    
38
  public function tiles_prefix() {
39
          return $this->base_dir.'/'.$this->get_prefix();
40
  }
41

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

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

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

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

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

    
103
  public function has_tiles(){
104
          if (file_exists($this->tiles_path())) {
105
         $pano_files = scandir($this->tiles_path());
106
        foreach($pano_files as $filename) {
107
          if (preg_match('/.*\.jpg/', $filename)) {
108
                  return true;
109
          }
110
        }
111
      } else {
112
              return false;
113
      }
114
  }
115

    
116
  public function get_name() {
117
    return basename($this->base_dir);
118
  }
119

    
120
  public function get_prefix() {
121
    return $this->prefix;
122
  }
123

    
124
  public function get_magnifications() {
125
    $dir_fd = opendir($this->base_dir);
126
    $zoom_array = array();
127
    while (false !== ($file = readdir($dir_fd))) {                // extraction des paramètres de grossissement par le serveur
128
       //echo $file;
129
       if (preg_match('/(.*)_([0-9]+)_([0-9]+)_([0-9]+)\.jpg$/', $file, $reg)) {
130
         $prefix = $reg[1];
131
         if ($prefix == $this->prefix) {
132
           $zoom = (int)$reg[2];
133
           $posx = (int)$reg[3]+1;
134
           $posy = (int)$reg[4]+1;
135
           if (!isset($zoom_array[$zoom]['nx']) || $zoom_array[$zoom]['nx'] < $posx) $zoom_array[$zoom]['nx'] = $posx;
136
           if (!isset($zoom_array[$zoom]['ny']) || $zoom_array[$zoom]['ny'] < $posy) $zoom_array[$zoom]['ny'] = $posy;
137
         }
138
       }
139
    }
140
    $this->zooms = $zoom_array;
141
    return $this->zooms;
142
  }
143

    
144
  public function coordsToCap($lat, $lon, $alt) {
145
    $params = $this->get_params();
146
    if (!isset($params['latitude']) || !isset($params['longitude'])) return false;
147
    $rt = 6371;  // Rayon de la terre
148
    $alt1 = isset($params['altitude']) ? $params['altitude'] : $alt;
149
    $lat1 = $params['latitude']*M_PI/180;
150
    $lon1 = $params['longitude']*M_PI/180;
151
    $alt2 = $alt;
152
    $lat2 = $lat * M_PI/180;
153
    $lon2 = $lon * M_PI/180;
154

    
155
    $dLat = $lat2-$lat1;
156
    $dLon = $lon2-$lon1;
157

    
158
    $a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);  //
159
    $angle = 2 * atan2(sqrt($a), sqrt(1-$a));
160
    $d = $angle * $rt;                    // distance du point en Kms
161

    
162
    $y = sin($dLon)*cos($lat2);
163
    $x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
164
    $cap = atan2($y, $x);                 // cap pour atteindre le point en radians
165

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

    
171
  public function get_generate_url($source_file) {
172
          /**
173
           * @param $source_file : the name of the source file within the upload dir.
174
           */
175
          return sprintf('genererPano.php?wizard=1&name='.$source_file);
176
  }
177

    
178
  public function src_path(){
179
          /** @returns the basename of the src image, or false if it's no longer available
180
           */
181
          $extensions = array('jpg', 'tif', 'png', 'bmp', 'jpeg', 'pnm',
182
                              'JPG', 'TIF', 'PNG', 'BMP', 'JPEG', 'PNM');
183

    
184
          foreach ($extensions as $ext) {
185
                  $tried_name = sprintf('%s/%s.%s', UPLOAD_PATH, $this->get_name(),$ext);
186
                  if (file_exists($tried_name)) {
187
                          return $tried_name;
188
                  }
189
          }
190
          return false;
191
  }
192

    
193
  public function get_url($cap=false, $ele=false) {
194
          $o = sprintf('panorama.php?dir=%s&panorama=%s',
195
                          PANORAMA_FOLDER, $this->get_name());
196
          if ($cap && $ele) {
197
                  $o .= sprintf("&to_cap=%.3f&to_ele=%.3f", $cap, $ele);
198
          }
199
          return $o;
200
  }
201

    
202
  public function get_map_url($cap=0) {
203
          $encoded_title = base64_encode($this->get_name());
204
          $script_name = 'show_capline.php';
205
          $lat = $this->get_params()['latitude'];
206
          $lon = $this->get_params()['longitude'];
207

    
208
          $o = sprintf('%s?title=%s&cap=%s&org_lat=%.5f&org_lon=%.5f&dist=120000',
209
                       $script_name, $encoded_title, $cap, $lat, $lon);
210
          return $o;
211
  }
212

    
213
  public function set_reference($ref_point, $x, $y) {
214
          /**
215
           * Registers (for saving) the position of a reference point within a
216
           * panorama. It sets or overwrite a reference.
217
           *
218
           * @param $ref_point a RefPoint instance
219
           * @param $x the relative x position of the RefPoint
220
           * @param $x the relative y position of the RefPoint
221
           */
222
          $p = $this->get_params();
223

    
224
          if (!isset($this->params[self::$REF_KEY]) ||
225
              !is_array($this->params[self::$REF_KEY])) {
226
                  $this->params[self::$REF_KEY] = array();
227
          }
228
          $ref_name = $ref_point->name;
229
          $dict = $this->params[self::$REF_KEY];
230
          //$dddd = $this->params[self::$REF_KEY][$ref_name];
231
          $this->params[self::$REF_KEY][$ref_name] = array($x, $y);
232
  }
233

    
234
  public function unset_reference($ref_point) {
235
          /**
236
           * Unregisters a reference, within a panorama.
237
           * does nothing if the RefPoint is not registered.
238
           *
239
           * @param $ref_point a RefPoint instance
240
           */
241
          $p = $this->get_params();
242
          $ref_name = $ref_point->name;
243
          if (isset($p[self::$REF_KEY]) &&
244
              isset($p[self::$REF_KEY][$ref_name])) {
245
                  unset($this->params[self::$REF_KEY][$ref_name]);
246
          }
247
  }
248

    
249

    
250

    
251
  public static function get($name) {
252
          /** Instantiate a site_point, given its name
253
           */
254
          $pano_dir = PANORAMA_PATH.'/'.$name;
255
          return new site_point($pano_dir);
256
  }
257

    
258
  public static function create($filepath) {
259
          /** creates a new panorama, given its name, from an uploaded file.
260
           */
261
          $name = utils::strip_extension(basename($filepath));
262
          $pano_dir = PANORAMA_PATH.'/'.$name;
263
          $pano = new site_point($pano_dir);
264
          if (!mkdir($pano->tiles_path())) {
265
                  return false;
266
          } else {
267
                  return $pano;
268
          }
269
  }
270

    
271
  public function to_geoJSON() {
272
          $prm = $this->get_params();
273
                $name = $this->get_name();
274
                $lat = floatval($prm['latitude']);
275
                $lon = floatval($prm['longitude']);
276
                //$alt = $prm['altitude'];
277
                //$title = $prm['titre'];
278

    
279
                return array("type" => "Feature",
280
                             "geometry" => array(
281
                                                 "type" => "Point",
282
                                                 "coordinates" => array($lon, $lat)
283
                                                 ),
284
                             "properties" => array("name" => $name,
285
                                                   "type" => 'pano_point',
286
                                                   "view_url"  => $this->get_url())
287
                             );
288
  }
289

    
290

    
291
  public static function get_all($only_with_params=false) {
292
          /**
293
           * @param $only_with_params : filters out the panoramas which
294
           *        are not parametrized
295
           */
296
          $panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
297
          $pano_instances = array();
298

    
299
          foreach ($panos as $pano_name) {
300
                  $pano =  site_point::get($pano_name);
301
                  if (! $only_with_params || $pano->has_params() ) {
302
                          $pano_instances[] = $pano;
303
                  }
304
          }
305
          return $pano_instances;
306
  }
307

    
308
}