Projet

Général

Profil

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

root / class / site_point.class.php @ 5119cb9e

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
    // si $dir n'est pas un répertoire il ne s'agit pas d'un panorama.
21
          if (!is_dir($dir)) {
22
                  throw new PanoramaFormatException("$dir does not contain a panorama");
23
          }
24
    $this->base_dir = $dir;
25
    $this->prefix = basename($dir);
26
  }
27

    
28
  public function params_path() {
29
          return $this->base_dir.'/'.$this->prefix.'.params';
30
  }
31

    
32
  private function parse_and_cache_params() {
33
    if (is_file($this->params_path())) {
34
            $params = parse_ini_file($this->params_path());
35
            if ($params) {
36
                    $this->params = $params;
37
                    return $params;
38
            }
39
    }
40
    return array();
41
  }
42

    
43
  public function get_params() {
44
          // the params are cached
45
          if (isset($this->params) && $this->params) {
46
                  return $this->params;
47
          } else {
48
                  return $this->parse_and_cache_params();
49
          }
50
  }
51

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

    
61
  public function set_param($key, $value) {
62
          $p = $this->get_params();
63
          $this->params[$key] = $value;
64
          if ($key == 'titre') {
65
                  $this->name = $value;
66
          }
67
  }
68

    
69
  public function has_params(){
70
          $p = $this->get_params();
71
          return (isset($p['latitude'], $p['longitude'],
72
                        $p['altitude'], $p['titre']));
73
  }
74

    
75
  public function get_name() {
76
    return basename($this->base_dir);
77
  }
78

    
79
  public function get_prefix() {
80
    return $this->prefix;
81
  }
82

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

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

    
113
    $dLat = $lat2-$lat1;
114
    $dLon = $lon2-$lon1;
115

    
116
    $a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);  //
117
    $angle = 2 * atan2(sqrt($a), sqrt(1-$a));
118
    $d = $angle * $rt;                    // distance du point en Kms
119

    
120
    $y = sin($dLon)*cos($lat2);
121
    $x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
122
    $cap = atan2($y, $x);                 // cap pour atteindre le point en radians
123

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

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

    
138
  public static function get($name) {
139
          /** Instantiate a site_point, given its name
140
           */
141
          $pano_dir = PANORAMA_PATH.'/'.$name;
142
          return new site_point($pano_dir);
143
  }
144

    
145

    
146
  public function to_geoJSON() {
147
          $prm = $this->get_params();
148
                $name = $this->get_name();
149
                $lat = floatval($prm['latitude']);
150
                $lon = floatval($prm['longitude']);
151
                //$alt = $prm['altitude'];
152
                //$title = $prm['titre'];
153

    
154
                return array("type" => "Feature",
155
                             "geometry" => array(
156
                                                 "type" => "Point",
157
                                                 "coordinates" => [$lon, $lat]
158
                                                 ),
159
                             "properties" => array("name" => $name,
160
                                                   "type" => 'pano_point',
161
                                                   "view_url"  => $this->get_url())
162
                             );
163
  }
164

    
165

    
166
  public static function get_all($only_with_params=false) {
167
          /**
168
           * @param $only_with_params : filters out the panoramas which are not parametrized
169
           */
170
          $panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
171
          $pano_instances = array();
172

    
173
          foreach ($panos as $pano_name) {
174
                  $pano =  site_point::get($pano_name);
175
                  if (! $only_with_params || $pano->has_params() ) {
176
                          $pano_instances[] = $pano;
177
                  }
178
          }
179
          return $pano_instances;
180
  }
181

    
182
}