Projet

Général

Profil

Paste
Télécharger (7,52 ko) Statistiques
| Branche: | Révision:

root / class / site_point.class.php @ d4e538ff

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 $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 get_name() {
100
    return basename($this->base_dir);
101
  }
102

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

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

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

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

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

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

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

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

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

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

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

    
199

    
200

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

    
208
  public static function create($filepath) {
209
          /** creates a new panorama, given its name, from an uploaded file.
210
           */
211
          $name = utils::strip_extension(basename($filepath));
212
          $pano_dir = PANORAMA_PATH.'/'.$name;
213
          $pano = new site_point($pano_dir);
214
          if (!mkdir($pano->tiles_path())) {
215
                  return false;
216
          } else {
217
                  return $pano;
218
          }
219
  }
220

    
221
  public function to_geoJSON() {
222
          $prm = $this->get_params();
223
                $name = $this->get_name();
224
                $lat = floatval($prm['latitude']);
225
                $lon = floatval($prm['longitude']);
226
                //$alt = $prm['altitude'];
227
                //$title = $prm['titre'];
228

    
229
                return array("type" => "Feature",
230
                             "geometry" => array(
231
                                                 "type" => "Point",
232
                                                 "coordinates" => array($lon, $lat)
233
                                                 ),
234
                             "properties" => array("name" => $name,
235
                                                   "type" => 'pano_point',
236
                                                   "view_url"  => $this->get_url())
237
                             );
238
  }
239

    
240

    
241
  public static function get_all($only_with_params=false) {
242
          /**
243
           * @param $only_with_params : filters out the panoramas which
244
           *        are not parametrized
245
           */
246
          $panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
247
          $pano_instances = array();
248

    
249
          foreach ($panos as $pano_name) {
250
                  $pano =  site_point::get($pano_name);
251
                  if (! $only_with_params || $pano->has_params() ) {
252
                          $pano_instances[] = $pano;
253
                  }
254
          }
255
          return $pano_instances;
256
  }
257

    
258
}