1
|
<?php
|
2
|
require_once(dirname(__FILE__).'/../constants.inc.php');
|
3
|
|
4
|
|
5
|
class PanoramaFormatException extends Exception {
|
6
|
|
7
|
|
8
|
}
|
9
|
|
10
|
class site_point {
|
11
|
|
12
|
|
13
|
private $base_dir;
|
14
|
private $name = false;
|
15
|
private $prefix = false;
|
16
|
private $params = false;
|
17
|
private $zooms;
|
18
|
|
19
|
public function __construct($dir) {
|
20
|
|
21
|
|
22
|
|
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
|
public function tiles_path() {
|
33
|
return $this->base_dir;
|
34
|
}
|
35
|
|
36
|
public function tiles_prefix() {
|
37
|
return $this->base_dir.'/'.$this->get_prefix();
|
38
|
}
|
39
|
|
40
|
private function parse_and_cache_params() {
|
41
|
if (is_file($this->params_path())) {
|
42
|
$params = parse_ini_file($this->params_path());
|
43
|
if ($params) {
|
44
|
$this->params = $params;
|
45
|
return $params;
|
46
|
}
|
47
|
}
|
48
|
return array();
|
49
|
}
|
50
|
|
51
|
public function get_params() {
|
52
|
|
53
|
if (isset($this->params) && $this->params) {
|
54
|
return $this->params;
|
55
|
} else {
|
56
|
return $this->parse_and_cache_params();
|
57
|
}
|
58
|
}
|
59
|
|
60
|
public function save_params() {
|
61
|
$o = '';
|
62
|
$p = $this->get_params();
|
63
|
foreach ($this->get_params() as $k => $v) {
|
64
|
$o.= "$k = $v\n";
|
65
|
}
|
66
|
file_put_contents($this->params_path(), $o);
|
67
|
}
|
68
|
|
69
|
public function set_param($key, $value) {
|
70
|
$p = $this->get_params();
|
71
|
$this->params[$key] = $value;
|
72
|
if ($key == 'titre') {
|
73
|
$this->name = $value;
|
74
|
}
|
75
|
}
|
76
|
|
77
|
public function has_params(){
|
78
|
$p = $this->get_params();
|
79
|
return (isset($p['latitude'], $p['longitude'],
|
80
|
$p['altitude'], $p['titre']));
|
81
|
}
|
82
|
|
83
|
public function get_name() {
|
84
|
return basename($this->base_dir);
|
85
|
}
|
86
|
|
87
|
public function get_prefix() {
|
88
|
return $this->prefix;
|
89
|
}
|
90
|
|
91
|
public function get_magnifications() {
|
92
|
$dir_fd = opendir($this->base_dir);
|
93
|
$zoom_array = array();
|
94
|
while (false !== ($file = readdir($dir_fd))) {
|
95
|
|
96
|
if (preg_match('/(.*)_([0-9]+)_([0-9]+)_([0-9]+)\.jpg$/', $file, $reg)) {
|
97
|
$prefix = $reg[1];
|
98
|
if ($prefix == $this->prefix) {
|
99
|
$zoom = (int)$reg[2];
|
100
|
$posx = (int)$reg[3]+1;
|
101
|
$posy = (int)$reg[4]+1;
|
102
|
if (!isset($zoom_array[$zoom]['nx']) || $zoom_array[$zoom]['nx'] < $posx) $zoom_array[$zoom]['nx'] = $posx;
|
103
|
if (!isset($zoom_array[$zoom]['ny']) || $zoom_array[$zoom]['ny'] < $posy) $zoom_array[$zoom]['ny'] = $posy;
|
104
|
}
|
105
|
}
|
106
|
}
|
107
|
$this->zooms = $zoom_array;
|
108
|
return $this->zooms;
|
109
|
}
|
110
|
|
111
|
public function coordsToCap($lat, $lon, $alt) {
|
112
|
$params = $this->get_params();
|
113
|
if (!isset($params['latitude']) || !isset($params['longitude'])) return false;
|
114
|
$rt = 6371;
|
115
|
$alt1 = isset($params['altitude']) ? $params['altitude'] : $alt;
|
116
|
$lat1 = $params['latitude']*M_PI/180;
|
117
|
$lon1 = $params['longitude']*M_PI/180;
|
118
|
$alt2 = $alt;
|
119
|
$lat2 = $lat * M_PI/180;
|
120
|
$lon2 = $lon * M_PI/180;
|
121
|
|
122
|
$dLat = $lat2-$lat1;
|
123
|
$dLon = $lon2-$lon1;
|
124
|
|
125
|
$a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);
|
126
|
$angle = 2 * atan2(sqrt($a), sqrt(1-$a));
|
127
|
$d = $angle * $rt;
|
128
|
|
129
|
$y = sin($dLon)*cos($lat2);
|
130
|
$x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
|
131
|
$cap = atan2($y, $x);
|
132
|
|
133
|
$e = atan2(($alt2 - $alt1)/1000 - $d*$d/(2*$rt), $d);
|
134
|
|
135
|
return array($d, $cap*180/M_PI, $e*180/M_PI);
|
136
|
}
|
137
|
|
138
|
public function get_url($cap=false, $ele=false) {
|
139
|
$o = sprintf('panorama.php?dir=%s&panorama=%s',
|
140
|
PANORAMA_FOLDER, $this->get_name());
|
141
|
if ($cap && $ele) {
|
142
|
$o .= sprintf("&to_cap=%.3f&to_ele=%.3f", $cap, $ele);
|
143
|
}
|
144
|
return $o;
|
145
|
}
|
146
|
|
147
|
public static function get($name) {
|
148
|
|
149
|
|
150
|
$pano_dir = PANORAMA_PATH.'/'.$name;
|
151
|
return new site_point($pano_dir);
|
152
|
}
|
153
|
|
154
|
|
155
|
public function to_geoJSON() {
|
156
|
$prm = $this->get_params();
|
157
|
$name = $this->get_name();
|
158
|
$lat = floatval($prm['latitude']);
|
159
|
$lon = floatval($prm['longitude']);
|
160
|
|
161
|
|
162
|
|
163
|
return array("type" => "Feature",
|
164
|
"geometry" => array(
|
165
|
"type" => "Point",
|
166
|
"coordinates" => [$lon, $lat]
|
167
|
),
|
168
|
"properties" => array("name" => $name,
|
169
|
"type" => 'pano_point',
|
170
|
"view_url" => $this->get_url())
|
171
|
);
|
172
|
}
|
173
|
|
174
|
|
175
|
public static function get_all($only_with_params=false) {
|
176
|
|
177
|
|
178
|
|
179
|
$panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
|
180
|
$pano_instances = array();
|
181
|
|
182
|
foreach ($panos as $pano_name) {
|
183
|
$pano = site_point::get($pano_name);
|
184
|
if (! $only_with_params || $pano->has_params() ) {
|
185
|
$pano_instances[] = $pano;
|
186
|
}
|
187
|
}
|
188
|
return $pano_instances;
|
189
|
}
|
190
|
|
191
|
}
|