Projet

Général

Profil

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

root / class / site_point.class.php @ ea08a2a7

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

    
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 {
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_path() {
32
          return $this->base_dir;
33
  }
34

    
35
  public function tiles_prefix() {
36
          return $this->base_dir.'/'.$this->get_prefix();
37
  }
38

    
39
  private function parse_and_cache_params() {
40
    if (is_file($this->params_path())) {
41
            $params = parse_ini_file($this->params_path());
42
            if ($params) {
43
                    $this->params = $params;
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
                    if (isset($params['image_loop'])) {
50
                            $this->params['image_loop'] = (bool)($params['image_loop']);
51
                    }
52
                    return $params;
53
            }
54
    }
55
    return array();
56
  }
57

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

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

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

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

    
98
  public function get_name() {
99
    return basename($this->base_dir);
100
  }
101

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

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

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

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

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

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

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

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

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

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

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

    
198

    
199

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

    
207

    
208
  public function to_geoJSON() {
209
          $prm = $this->get_params();
210
                $name = $this->get_name();
211
                $lat = floatval($prm['latitude']);
212
                $lon = floatval($prm['longitude']);
213
                //$alt = $prm['altitude'];
214
                //$title = $prm['titre'];
215

    
216
                return array("type" => "Feature",
217
                             "geometry" => array(
218
                                                 "type" => "Point",
219
                                                 "coordinates" => [$lon, $lat]
220
                                                 ),
221
                             "properties" => array("name" => $name,
222
                                                   "type" => 'pano_point',
223
                                                   "view_url"  => $this->get_url())
224
                             );
225
  }
226

    
227

    
228
  public static function get_all($only_with_params=false) {
229
          /**
230
           * @param $only_with_params : filters out the panoramas which are not parametrized
231
           */
232
          $panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
233
          $pano_instances = array();
234

    
235
          foreach ($panos as $pano_name) {
236
                  $pano =  site_point::get($pano_name);
237
                  if (! $only_with_params || $pano->has_params() ) {
238
                          $pano_instances[] = $pano;
239
                  }
240
          }
241
          return $pano_instances;
242
  }
243

    
244
}