Projet

Général

Profil

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

root / class / site_point.class.php @ c4802754

1
<?php
2
require_once(dirname(__FILE__).'/../constants.inc.php');
3
class site_point {
4
        /** Defines a point, with a panorama
5
         */
6
        private $base_dir;        // dir of tiles for that panorama
7
  private $name = false;
8
  private $prefix = false;
9
  private $params = false;
10
  private $zooms;
11

    
12
  public function __construct($dir) {
13
    // si $dir n'est pas un répertoire il ne s'agit pas d'un panorama.
14
    if (!is_dir($dir)) return;
15
    $this->base_dir = $dir;
16
    $dir_fd = opendir($this->base_dir);
17

    
18
    while (false !== ($file = readdir($dir_fd))) {
19

    
20
       if (preg_match('/(.*)_[0-9]+_[0-9]+_[0-9]+\.jpg$/', $file, $reg)) {
21
               $this->prefix = $reg[1];
22

    
23
               break;
24
       }
25
    }
26
    closedir($dir_fd);
27
    if ($this->prefix === false) return false;
28
    $this->parse_and_cache_params();
29
  }
30

    
31
  public function params_path() {
32
          return $this->base_dir.'/'.$this->prefix.'.params';
33
  }
34

    
35
  private function parse_and_cache_params() {
36
    if (is_file($this->params_path())) {
37
            $this->params = @parse_ini_file($this->params_path());
38
    }
39
  }
40

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

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

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

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

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

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

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

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

    
111
    $dLat = $lat2-$lat1;
112
    $dLon = $lon2-$lon1;
113

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

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

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

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

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

    
143

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

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

    
163

    
164
  public static function get_all() {
165
          $panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
166
          $pano_instances = array();
167
          foreach ($panos as $pano_name) {
168
                  $pano_instances[] = site_point::get($pano_name);
169
          }
170
          return $pano_instances;
171
  }
172

    
173
}