Projet

Général

Profil

Paste
Télécharger (9,53 ko) Statistiques
| Branche: | Révision:

root / class / site_point.class.php @ master

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

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

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

    
22
  public static $REF_KEY = 'reference';
23

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

    
29
  public function params_path() {
30
          return $this->base_dir.'/site.params';
31
  }
32

    
33
  /** Look for a *.params file in the base_dir
34
   *
35
   *  Tries first the site.params, then a globbing *.params.
36
   *
37
   *  May be deprecated at a certain time (if we consider all files should be
38
   *  named site.params)
39
   *
40
   *  @returns false if not found, an abs path else.
41
   */
42
  private function look_for_params() {
43
        if (is_file($this->params_path())) {
44
          return $this->params_path();
45
        }
46
        $matches = glob($this->base_dir.'/*.params');
47
        if ($matches and count($matches) > 0) {
48
          return $matches[0];
49
        } else {
50
          return false;
51
        }
52
  }
53

    
54
  public function tiles_url_prefix() {
55
          return PANORAMA_FOLDER;
56
  }
57

    
58
  public function tiles_path() {
59
          return $this->base_dir;
60
  }
61

    
62
  private function parse_and_cache_params() {
63
        $params_path = $this->look_for_params();
64

    
65
    if ($params_path) {
66
            $params = parse_ini_file($params_path);
67
            if ($params) {
68
                    $this->params = $params;
69
                    if (isset($params[self::$REF_KEY])) {
70
                            foreach ($params[self::$REF_KEY] as $ref => $vals) {
71
                                    $bits = explode(',',$vals);
72
                                    $this->params[self::$REF_KEY][$ref] = array(floatval($bits[0]),
73
                                                                                floatval($bits[1]));
74
                            }
75
                    }
76
                    if (isset($params['image_loop'])) {
77
                            $this->params['image_loop'] = (bool)($params['image_loop']);
78
                    }
79
                    return $this->params;
80
            }
81
    }
82
    return array();
83
  }
84

    
85
  public function get_params() {
86
          // the params are cached
87
          if (isset($this->params) && $this->params) {
88
                  return $this->params;
89
          } else {
90
                  return $this->parse_and_cache_params();
91
          }
92
  }
93

    
94
  public function save_params() {
95
          $o = '';
96
          $p = $this->get_params();
97
          foreach ($this->get_params() as $k => $v) {
98
                  if ($k == self::$REF_KEY) {
99
                          foreach ($v as $refk => $refv) {
100
                                  $o.= sprintf("%s[\"%s\"] = %.5f,%.5f\n",
101
                                               self::$REF_KEY, $refk,
102
                                               $refv[0], $refv[1]);
103
                          }
104
                  } else {
105
                          $o.= "$k = ".utils::php2ini($v)."\n";
106
                  }
107
          }
108
          file_put_contents($this->params_path(), $o);
109
  }
110

    
111
  public function set_param($key, $value) {
112
          $p = $this->get_params();
113
          $this->params[$key] = $value;
114
          if ($key == 'titre') {
115
                  $this->name = $value;
116
          }
117
  }
118

    
119
  public function has_params(){
120
          $p = $this->get_params();
121
          return (isset($p['latitude'], $p['longitude'],
122
                        $p['altitude'], $p['titre']));
123
  }
124

    
125
  public function has_tiles(){
126
          if (file_exists($this->tiles_path())) {
127
         $pano_files = scandir($this->tiles_path());
128
        foreach($pano_files as $filename) {
129
          if (preg_match('/.*\.jpg/', $filename)) {
130
                  return true;
131
          }
132
        }
133
      } else {
134
              return false;
135
      }
136
  }
137

    
138
  public function get_name() {
139
    return basename($this->base_dir);
140
  }
141

    
142
  public function get_prefix() {
143
    return $this->prefix;
144
  }
145

    
146
  public function get_lat() {
147
        $p = $this->get_params();
148
        return $p['latitude'];
149
  }
150

    
151
  public function get_lon() {
152
        $p = $this->get_params();
153
        return $p['longitude'];
154
  }
155

    
156
  public function get_magnifications() {
157
    //$dir_fd = opendir($this->base_dir);
158
    $zoom_array = array();
159

    
160
        // extraction des paramètres de grossissement par le serveur
161
        $stop = false;
162
        $zoom_level = 0;
163

    
164
        while (! $stop) {
165
          $files = glob(sprintf('%s/%03d_*.jpg', $this->base_dir, $zoom_level));
166
          sort($files);
167
          $last_file = end($files);
168
          if ($last_file) {
169
                $last_tile = Tile::from_file($last_file, $this);
170
                $zoom_array[$zoom_level] = array('nx' => $last_tile->x + 1,
171
                                                                                 'ny' => $last_tile->y + 1);
172
                $zoom_level++;
173
          } else {
174
                $stop = true;
175
          }
176
        }
177
    $this->zooms = $zoom_array;
178
    return $this->zooms;
179
  }
180

    
181
  public function coordsToCap($lat, $lon, $alt) {
182
    $params = $this->get_params();
183
    if (!isset($params['latitude']) || !isset($params['longitude'])) return false;
184
    $rt = 6371;  // Rayon de la terre
185
    $alt1 = isset($params['altitude']) ? $params['altitude'] : $alt;
186
    $lat1 = $params['latitude']*M_PI/180;
187
    $lon1 = $params['longitude']*M_PI/180;
188
    $alt2 = $alt;
189
    $lat2 = $lat * M_PI/180;
190
    $lon2 = $lon * M_PI/180;
191

    
192
    $dLat = $lat2-$lat1;
193
    $dLon = $lon2-$lon1;
194

    
195
    $a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);  //
196
    $angle = 2 * atan2(sqrt($a), sqrt(1-$a));
197
    $d = $angle * $rt;                    // distance du point en Kms
198

    
199
    $y = sin($dLon)*cos($lat2);
200
    $x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
201
    $cap = atan2($y, $x);                 // cap pour atteindre le point en radians
202

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

    
208
  public function get_generate_url($source_file) {
209
          /**
210
           * @param $source_file : the name of the source file within the upload dir.
211
           */
212
          return sprintf('genererPano.php?wizard=1&name='.$source_file);
213
  }
214

    
215
  public function src_path(){
216
          /** @returns the basename of the src image, or false if it's no longer available
217
           */
218
          $extensions = array('jpg', 'tif', 'png', 'bmp', 'jpeg', 'pnm',
219
                              'JPG', 'TIF', 'PNG', 'BMP', 'JPEG', 'PNM');
220

    
221
          foreach ($extensions as $ext) {
222
                  $tried_name = sprintf('%s/%s.%s', UPLOAD_PATH, $this->get_name(),$ext);
223
                  if (file_exists($tried_name)) {
224
                          return $tried_name;
225
                  }
226
          }
227
          return false;
228
  }
229

    
230
  public function get_url($cap=false, $ele=false) {
231
          $o = sprintf('panorama.php?dir=%s&panorama=%s',
232
                          PANORAMA_FOLDER, $this->get_name());
233
          if ($cap && $ele) {
234
                  $o .= sprintf("&to_cap=%.3f&to_ele=%.3f", $cap, $ele);
235
          }
236
          return $o;
237
  }
238

    
239
  public function get_map_url($cap=0) {
240
          $encoded_title = base64_encode($this->get_name());
241
          $script_name = 'show_capline.php';
242
          $lat = $this->get_params()['latitude'];
243
          $lon = $this->get_params()['longitude'];
244

    
245
          $o = sprintf('%s?title=%s&cap=%s&org_lat=%.5f&org_lon=%.5f&dist=120000',
246
                       $script_name, $encoded_title, $cap, $lat, $lon);
247
          return $o;
248
  }
249

    
250
  public function set_reference($ref_point, $x, $y) {
251
          /**
252
           * Registers (for saving) the position of a reference point within a
253
           * panorama. It sets or overwrite a reference.
254
           *
255
           * @param $ref_point a RefPoint instance
256
           * @param $x the relative x position of the RefPoint
257
           * @param $x the relative y position of the RefPoint
258
           */
259
          $p = $this->get_params();
260

    
261
          if (!isset($this->params[self::$REF_KEY]) ||
262
              !is_array($this->params[self::$REF_KEY])) {
263
                  $this->params[self::$REF_KEY] = array();
264
          }
265
          $ref_name = $ref_point->name;
266
          $dict = $this->params[self::$REF_KEY];
267
          //$dddd = $this->params[self::$REF_KEY][$ref_name];
268
          $this->params[self::$REF_KEY][$ref_name] = array($x, $y);
269
  }
270

    
271
  public function unset_reference($ref_point) {
272
          /**
273
           * Unregisters a reference, within a panorama.
274
           * does nothing if the RefPoint is not registered.
275
           *
276
           * @param $ref_point a RefPoint instance
277
           */
278
          $p = $this->get_params();
279
          $ref_name = $ref_point->name;
280
          if (isset($p[self::$REF_KEY]) &&
281
              isset($p[self::$REF_KEY][$ref_name])) {
282
                  unset($this->params[self::$REF_KEY][$ref_name]);
283
          }
284
  }
285

    
286

    
287

    
288
  public static function get($name) {
289
          /** Instantiate a site_point, given its name
290
           */
291
          $pano_dir = PANORAMA_PATH.'/'.$name;
292
          return new site_point($pano_dir);
293
  }
294

    
295
  public static function create($filepath) {
296
          /** creates a new panorama, given its name, from an uploaded file.
297
           */
298
          $name = utils::strip_extension(basename($filepath));
299
          $pano_dir = PANORAMA_PATH.'/'.$name;
300
          $pano = new site_point($pano_dir);
301
          if (!mkdir($pano->tiles_path())) {
302
                  return false;
303
          } else {
304
                  return $pano;
305
          }
306
  }
307

    
308
  public function to_geoJSON() {
309
          $prm = $this->get_params();
310
                $name = $this->get_name();
311
                $lat = floatval($prm['latitude']);
312
                $lon = floatval($prm['longitude']);
313
                //$alt = $prm['altitude'];
314
                //$title = $prm['titre'];
315

    
316
                return array("type" => "Feature",
317
                             "geometry" => array(
318
                                                 "type" => "Point",
319
                                                 "coordinates" => array($lon, $lat)
320
                                                 ),
321
                             "properties" => array("name" => $name,
322
                                                   "type" => 'pano_point',
323
                                                   "view_url"  => $this->get_url())
324
                             );
325
  }
326

    
327

    
328
  public static function get_all($only_with_params=true) {
329
          /**
330
           * @param $only_with_params : filters out the panoramas which
331
           *        are not parametrized
332
           */
333
          $panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
334
          $pano_instances = array();
335

    
336
          foreach ($panos as $pano_name) {
337
                  $pano =  site_point::get($pano_name);
338
                  if (! $only_with_params || $pano->has_params() ) {
339
                          $pano_instances[] = $pano;
340
                  }
341
          }
342
          return $pano_instances;
343
  }
344

    
345
}