Projet

Général

Profil

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

root / class / site_point.class.php @ a1063f68

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_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
                    if (isset($params[self::$REF_KEY])) {
44
                            foreach ($params[self::$REF_KEY] as $ref => $vals) {
45
                                    $bits = explode(',',$vals);
46
                                    $this->params[self::$REF_KEY][$ref] = array(floatval($bits[0]),
47
                                                                                floatval($bits[1]));
48
                            }
49
                    }
50
                    if (isset($params['image_loop'])) {
51
                            $this->params['image_loop'] = (bool)($params['image_loop']);
52
                    }
53
                    return $this->params;
54
            }
55
    }
56
    return array();
57
  }
58

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

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

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

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

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

    
112
  public function get_name() {
113
    return basename($this->base_dir);
114
  }
115

    
116
  public function get_prefix() {
117
    return $this->prefix;
118
  }
119

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

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

    
151
    $dLat = $lat2-$lat1;
152
    $dLon = $lon2-$lon1;
153

    
154
    $a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);  //
155
    $angle = 2 * atan2(sqrt($a), sqrt(1-$a));
156
    $d = $angle * $rt;                    // distance du point en Kms
157

    
158
    $y = sin($dLon)*cos($lat2);
159
    $x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
160
    $cap = atan2($y, $x);                 // cap pour atteindre le point en radians
161

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

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

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

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

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

    
198
  public function get_map_url($cap=0) {
199
          $encoded_title = base64_encode($this->get_name());
200
          $script_name = 'show_capline.php';
201
          $lat = $this->get_params()['latitude'];
202
          $lon = $this->get_params()['longitude'];
203

    
204
          $o = sprintf('%s?title=%s&cap=%s&org_lat=%.5f&org_lon=%.5f&dist=120000',
205
                       $script_name, $encoded_title, $cap, $lat, $lon);
206
          return $o;
207
  }
208

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

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

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

    
245

    
246

    
247
  public static function get($name) {
248
          /** Instantiate a site_point, given its name
249
           */
250
          $pano_dir = PANORAMA_PATH.'/'.$name;
251
          return new site_point($pano_dir);
252
  }
253

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

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

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

    
286

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

    
295
          foreach ($panos as $pano_name) {
296
                  $pano =  site_point::get($pano_name);
297
                  if (! $only_with_params || $pano->has_params() ) {
298
                          $pano_instances[] = $pano;
299
                  }
300
          }
301
          return $pano_instances;
302
  }
303

    
304
}