Projet

Général

Profil

Paste
Télécharger (5,29 ko) Statistiques
| Branche: | Révision:

root / class / site_point.class.php @ e3ac9269

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

    
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 function __construct($dir) {
20
    $this->base_dir = $dir;
21
    $this->prefix = basename($dir);
22
  }
23

    
24
  public function params_path() {
25
          return $this->base_dir.'/'.$this->prefix.'.params';
26
  }
27

    
28
  public function tiles_path() {
29
          return $this->base_dir;
30
  }
31

    
32
  public function tiles_prefix() {
33
          return $this->base_dir.'/'.$this->get_prefix();
34
  }
35

    
36
  private function parse_and_cache_params() {
37
    if (is_file($this->params_path())) {
38
            $params = parse_ini_file($this->params_path());
39
            if ($params) {
40
                    $this->params = $params;
41
                    return $params;
42
            }
43
    }
44
    return array();
45
  }
46

    
47
  public function get_params() {
48
          // the params are cached
49
          if (isset($this->params) && $this->params) {
50
                  return $this->params;
51
          } else {
52
                  return $this->parse_and_cache_params();
53
          }
54
  }
55

    
56
  public function save_params() {
57
          $o = '';
58
          $p = $this->get_params();
59
          foreach ($this->get_params() as $k => $v) {
60
                  $o.= "$k = $v\n";
61
          }
62
          file_put_contents($this->params_path(), $o);
63
  }
64

    
65
  public function set_param($key, $value) {
66
          $p = $this->get_params();
67
          $this->params[$key] = $value;
68
          if ($key == 'titre') {
69
                  $this->name = $value;
70
          }
71
  }
72

    
73
  public function has_params(){
74
          $p = $this->get_params();
75
          return (isset($p['latitude'], $p['longitude'],
76
                        $p['altitude'], $p['titre']));
77
  }
78

    
79
  public function get_name() {
80
    return basename($this->base_dir);
81
  }
82

    
83
  public function get_prefix() {
84
    return $this->prefix;
85
  }
86

    
87
  public function get_magnifications() {
88
    $dir_fd = opendir($this->base_dir);
89
    $zoom_array = array();
90
    while (false !== ($file = readdir($dir_fd))) {                // extraction des paramètres de grossissement par le serveur
91
       //echo $file;
92
       if (preg_match('/(.*)_([0-9]+)_([0-9]+)_([0-9]+)\.jpg$/', $file, $reg)) {
93
         $prefix = $reg[1];
94
         if ($prefix == $this->prefix) {
95
           $zoom = (int)$reg[2];
96
           $posx = (int)$reg[3]+1;
97
           $posy = (int)$reg[4]+1;
98
           if (!isset($zoom_array[$zoom]['nx']) || $zoom_array[$zoom]['nx'] < $posx) $zoom_array[$zoom]['nx'] = $posx;
99
           if (!isset($zoom_array[$zoom]['ny']) || $zoom_array[$zoom]['ny'] < $posy) $zoom_array[$zoom]['ny'] = $posy;
100
         }
101
       }
102
    }
103
    $this->zooms = $zoom_array;
104
    return $this->zooms;
105
  }
106

    
107
  public function coordsToCap($lat, $lon, $alt) {
108
    $params = $this->get_params();
109
    if (!isset($params['latitude']) || !isset($params['longitude'])) return false;
110
    $rt = 6371;  // Rayon de la terre
111
    $alt1 = isset($params['altitude']) ? $params['altitude'] : $alt;
112
    $lat1 = $params['latitude']*M_PI/180;
113
    $lon1 = $params['longitude']*M_PI/180;
114
    $alt2 = $alt;
115
    $lat2 = $lat * M_PI/180;
116
    $lon2 = $lon * M_PI/180;
117

    
118
    $dLat = $lat2-$lat1;
119
    $dLon = $lon2-$lon1;
120

    
121
    $a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);  //
122
    $angle = 2 * atan2(sqrt($a), sqrt(1-$a));
123
    $d = $angle * $rt;                    // distance du point en Kms
124

    
125
    $y = sin($dLon)*cos($lat2);
126
    $x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
127
    $cap = atan2($y, $x);                 // cap pour atteindre le point en radians
128

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

    
134
  public function get_url($cap=false, $ele=false) {
135
          $o = sprintf('panorama.php?dir=%s&panorama=%s',
136
                          PANORAMA_FOLDER, $this->get_name());
137
          if ($cap && $ele) {
138
                  $o .= sprintf("&to_cap=%.3f&to_ele=%.3f", $cap, $ele);
139
          }
140
          return $o;
141
  }
142

    
143
  public static function get($name) {
144
          /** Instantiate a site_point, given its name
145
           */
146
          $pano_dir = PANORAMA_PATH.'/'.$name;
147
          return new site_point($pano_dir);
148
  }
149

    
150

    
151
  public function to_geoJSON() {
152
          $prm = $this->get_params();
153
                $name = $this->get_name();
154
                $lat = floatval($prm['latitude']);
155
                $lon = floatval($prm['longitude']);
156
                //$alt = $prm['altitude'];
157
                //$title = $prm['titre'];
158

    
159
                return array("type" => "Feature",
160
                             "geometry" => array(
161
                                                 "type" => "Point",
162
                                                 "coordinates" => [$lon, $lat]
163
                                                 ),
164
                             "properties" => array("name" => $name,
165
                                                   "type" => 'pano_point',
166
                                                   "view_url"  => $this->get_url())
167
                             );
168
  }
169

    
170

    
171
  public static function get_all($only_with_params=false) {
172
          /**
173
           * @param $only_with_params : filters out the panoramas which are not parametrized
174
           */
175
          $panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
176
          $pano_instances = array();
177

    
178
          foreach ($panos as $pano_name) {
179
                  $pano =  site_point::get($pano_name);
180
                  if (! $only_with_params || $pano->has_params() ) {
181
                          $pano_instances[] = $pano;
182
                  }
183
          }
184
          return $pano_instances;
185
  }
186

    
187
}