Projet

Général

Profil

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

root / class / site_point.class.php @ ffebe856

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
//
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 extends GeoPoint {
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_url_prefix() {
32
          return PANORAMA_FOLDER.'/'.$this->prefix;
33
  }
34

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

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

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

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

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

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

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

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

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

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

    
125
  public function get_lat() {
126
        $p = $this->get_params();
127
        return $p['latitude'];
128
  }
129

    
130
  public function get_lon() {
131
        $p = $this->get_params();
132
        return $p['longitude'];
133
  }
134

    
135
  public function get_magnifications() {
136
    $dir_fd = opendir($this->base_dir);
137
    $zoom_array = array();
138
    while (false !== ($file = readdir($dir_fd))) {                // extraction des paramètres de grossissement par le serveur
139
       //echo $file;
140
       if (preg_match('/(.*)_([0-9]+)_([0-9]+)_([0-9]+)\.jpg$/', $file, $reg)) {
141
         $prefix = $reg[1];
142
         if ($prefix == $this->prefix) {
143
           $zoom = (int)$reg[2];
144
           $posx = (int)$reg[3]+1;
145
           $posy = (int)$reg[4]+1;
146
           if (!isset($zoom_array[$zoom]['nx']) || $zoom_array[$zoom]['nx'] < $posx) $zoom_array[$zoom]['nx'] = $posx;
147
           if (!isset($zoom_array[$zoom]['ny']) || $zoom_array[$zoom]['ny'] < $posy) $zoom_array[$zoom]['ny'] = $posy;
148
         }
149
       }
150
    }
151
    $this->zooms = $zoom_array;
152
    return $this->zooms;
153
  }
154

    
155
  public function coordsToCap($lat, $lon, $alt) {
156
    $params = $this->get_params();
157
    if (!isset($params['latitude']) || !isset($params['longitude'])) return false;
158
    $rt = 6371;  // Rayon de la terre
159
    $alt1 = isset($params['altitude']) ? $params['altitude'] : $alt;
160
    $lat1 = $params['latitude']*M_PI/180;
161
    $lon1 = $params['longitude']*M_PI/180;
162
    $alt2 = $alt;
163
    $lat2 = $lat * M_PI/180;
164
    $lon2 = $lon * M_PI/180;
165

    
166
    $dLat = $lat2-$lat1;
167
    $dLon = $lon2-$lon1;
168

    
169
    $a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);  //
170
    $angle = 2 * atan2(sqrt($a), sqrt(1-$a));
171
    $d = $angle * $rt;                    // distance du point en Kms
172

    
173
    $y = sin($dLon)*cos($lat2);
174
    $x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
175
    $cap = atan2($y, $x);                 // cap pour atteindre le point en radians
176

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

    
182
  public function get_generate_url($source_file) {
183
          /**
184
           * @param $source_file : the name of the source file within the upload dir.
185
           */
186
          return sprintf('genererPano.php?wizard=1&name='.$source_file);
187
  }
188

    
189
  public function src_path(){
190
          /** @returns the basename of the src image, or false if it's no longer available
191
           */
192
          $extensions = array('jpg', 'tif', 'png', 'bmp', 'jpeg', 'pnm',
193
                              'JPG', 'TIF', 'PNG', 'BMP', 'JPEG', 'PNM');
194

    
195
          foreach ($extensions as $ext) {
196
                  $tried_name = sprintf('%s/%s.%s', UPLOAD_PATH, $this->get_name(),$ext);
197
                  if (file_exists($tried_name)) {
198
                          return $tried_name;
199
                  }
200
          }
201
          return false;
202
  }
203

    
204
  public function get_url($cap=false, $ele=false) {
205
          $o = sprintf('panorama.php?dir=%s&panorama=%s',
206
                          PANORAMA_FOLDER, $this->get_name());
207
          if ($cap && $ele) {
208
                  $o .= sprintf("&to_cap=%.3f&to_ele=%.3f", $cap, $ele);
209
          }
210
          return $o;
211
  }
212

    
213
  public function get_map_url($cap=0) {
214
          $encoded_title = base64_encode($this->get_name());
215
          $script_name = 'show_capline.php';
216
          $lat = $this->get_params()['latitude'];
217
          $lon = $this->get_params()['longitude'];
218

    
219
          $o = sprintf('%s?title=%s&cap=%s&org_lat=%.5f&org_lon=%.5f&dist=120000',
220
                       $script_name, $encoded_title, $cap, $lat, $lon);
221
          return $o;
222
  }
223

    
224
  public function set_reference($ref_point, $x, $y) {
225
          /**
226
           * Registers (for saving) the position of a reference point within a
227
           * panorama. It sets or overwrite a reference.
228
           *
229
           * @param $ref_point a RefPoint instance
230
           * @param $x the relative x position of the RefPoint
231
           * @param $x the relative y position of the RefPoint
232
           */
233
          $p = $this->get_params();
234

    
235
          if (!isset($this->params[self::$REF_KEY]) ||
236
              !is_array($this->params[self::$REF_KEY])) {
237
                  $this->params[self::$REF_KEY] = array();
238
          }
239
          $ref_name = $ref_point->name;
240
          $dict = $this->params[self::$REF_KEY];
241
          //$dddd = $this->params[self::$REF_KEY][$ref_name];
242
          $this->params[self::$REF_KEY][$ref_name] = array($x, $y);
243
  }
244

    
245
  public function unset_reference($ref_point) {
246
          /**
247
           * Unregisters a reference, within a panorama.
248
           * does nothing if the RefPoint is not registered.
249
           *
250
           * @param $ref_point a RefPoint instance
251
           */
252
          $p = $this->get_params();
253
          $ref_name = $ref_point->name;
254
          if (isset($p[self::$REF_KEY]) &&
255
              isset($p[self::$REF_KEY][$ref_name])) {
256
                  unset($this->params[self::$REF_KEY][$ref_name]);
257
          }
258
  }
259

    
260

    
261

    
262
  public static function get($name) {
263
          /** Instantiate a site_point, given its name
264
           */
265
          $pano_dir = PANORAMA_PATH.'/'.$name;
266
          return new site_point($pano_dir);
267
  }
268

    
269
  public static function create($filepath) {
270
          /** creates a new panorama, given its name, from an uploaded file.
271
           */
272
          $name = utils::strip_extension(basename($filepath));
273
          $pano_dir = PANORAMA_PATH.'/'.$name;
274
          $pano = new site_point($pano_dir);
275
          if (!mkdir($pano->tiles_path())) {
276
                  return false;
277
          } else {
278
                  return $pano;
279
          }
280
  }
281

    
282
  public function to_geoJSON() {
283
          $prm = $this->get_params();
284
                $name = $this->get_name();
285
                $lat = floatval($prm['latitude']);
286
                $lon = floatval($prm['longitude']);
287
                //$alt = $prm['altitude'];
288
                //$title = $prm['titre'];
289

    
290
                return array("type" => "Feature",
291
                             "geometry" => array(
292
                                                 "type" => "Point",
293
                                                 "coordinates" => array($lon, $lat)
294
                                                 ),
295
                             "properties" => array("name" => $name,
296
                                                   "type" => 'pano_point',
297
                                                   "view_url"  => $this->get_url())
298
                             );
299
  }
300

    
301

    
302
  public static function get_all($only_with_params=true) {
303
          /**
304
           * @param $only_with_params : filters out the panoramas which
305
           *        are not parametrized
306
           */
307
          $panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
308
          $pano_instances = array();
309

    
310
          foreach ($panos as $pano_name) {
311
                  $pano =  site_point::get($pano_name);
312
                  if (! $only_with_params || $pano->has_params() ) {
313
                          $pano_instances[] = $pano;
314
                  }
315
          }
316
          return $pano_instances;
317
  }
318

    
319
}