1
|
<?php
|
2
|
require_once(dirname(__FILE__).'/../constants.inc.php');
|
3
|
require_once(dirname(__FILE__).'/utils.class.php');
|
4
|
utils::init();
|
5
|
|
6
|
class PanoramaFormatException extends Exception {
|
7
|
|
8
|
|
9
|
}
|
10
|
|
11
|
class site_point {
|
12
|
|
13
|
|
14
|
private $base_dir;
|
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
|
if (isset($params[self::$REF_KEY])) {
|
45
|
foreach ($params[self::$REF_KEY] as $ref => $vals) {
|
46
|
$bits = explode(',',$vals);
|
47
|
$this->params[self::$REF_KEY][$ref] = array(floatval($bits[0]),
|
48
|
floatval($bits[1]));
|
49
|
}
|
50
|
}
|
51
|
if (isset($params['image_loop'])) {
|
52
|
$this->params['image_loop'] = (bool)($params['image_loop']);
|
53
|
}
|
54
|
return $params;
|
55
|
}
|
56
|
}
|
57
|
return array();
|
58
|
}
|
59
|
|
60
|
public function get_params() {
|
61
|
|
62
|
if (isset($this->params) && $this->params) {
|
63
|
return $this->params;
|
64
|
} else {
|
65
|
return $this->parse_and_cache_params();
|
66
|
}
|
67
|
}
|
68
|
|
69
|
public function save_params() {
|
70
|
$o = '';
|
71
|
$p = $this->get_params();
|
72
|
foreach ($this->get_params() as $k => $v) {
|
73
|
if ($k == self::$REF_KEY) {
|
74
|
foreach ($v as $refk => $refv) {
|
75
|
$o.= sprintf("%s[\"%s\"] = %.5f,%.5f\n",
|
76
|
self::$REF_KEY, $refk,
|
77
|
$refv[0], $refv[1]);
|
78
|
}
|
79
|
} else {
|
80
|
$o.= "$k = ".utils::php2ini($v)."\n";
|
81
|
}
|
82
|
}
|
83
|
file_put_contents($this->params_path(), $o);
|
84
|
}
|
85
|
|
86
|
public function set_param($key, $value) {
|
87
|
$p = $this->get_params();
|
88
|
$this->params[$key] = $value;
|
89
|
if ($key == 'titre') {
|
90
|
$this->name = $value;
|
91
|
}
|
92
|
}
|
93
|
|
94
|
public function has_params(){
|
95
|
$p = $this->get_params();
|
96
|
return (isset($p['latitude'], $p['longitude'],
|
97
|
$p['altitude'], $p['titre']));
|
98
|
}
|
99
|
|
100
|
public function get_name() {
|
101
|
return basename($this->base_dir);
|
102
|
}
|
103
|
|
104
|
public function get_prefix() {
|
105
|
return $this->prefix;
|
106
|
}
|
107
|
|
108
|
public function get_magnifications() {
|
109
|
$dir_fd = opendir($this->base_dir);
|
110
|
$zoom_array = array();
|
111
|
while (false !== ($file = readdir($dir_fd))) {
|
112
|
|
113
|
if (preg_match('/(.*)_([0-9]+)_([0-9]+)_([0-9]+)\.jpg$/', $file, $reg)) {
|
114
|
$prefix = $reg[1];
|
115
|
if ($prefix == $this->prefix) {
|
116
|
$zoom = (int)$reg[2];
|
117
|
$posx = (int)$reg[3]+1;
|
118
|
$posy = (int)$reg[4]+1;
|
119
|
if (!isset($zoom_array[$zoom]['nx']) || $zoom_array[$zoom]['nx'] < $posx) $zoom_array[$zoom]['nx'] = $posx;
|
120
|
if (!isset($zoom_array[$zoom]['ny']) || $zoom_array[$zoom]['ny'] < $posy) $zoom_array[$zoom]['ny'] = $posy;
|
121
|
}
|
122
|
}
|
123
|
}
|
124
|
$this->zooms = $zoom_array;
|
125
|
return $this->zooms;
|
126
|
}
|
127
|
|
128
|
public function coordsToCap($lat, $lon, $alt) {
|
129
|
$params = $this->get_params();
|
130
|
if (!isset($params['latitude']) || !isset($params['longitude'])) return false;
|
131
|
$rt = 6371;
|
132
|
$alt1 = isset($params['altitude']) ? $params['altitude'] : $alt;
|
133
|
$lat1 = $params['latitude']*M_PI/180;
|
134
|
$lon1 = $params['longitude']*M_PI/180;
|
135
|
$alt2 = $alt;
|
136
|
$lat2 = $lat * M_PI/180;
|
137
|
$lon2 = $lon * M_PI/180;
|
138
|
|
139
|
$dLat = $lat2-$lat1;
|
140
|
$dLon = $lon2-$lon1;
|
141
|
|
142
|
$a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);
|
143
|
$angle = 2 * atan2(sqrt($a), sqrt(1-$a));
|
144
|
$d = $angle * $rt;
|
145
|
|
146
|
$y = sin($dLon)*cos($lat2);
|
147
|
$x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
|
148
|
$cap = atan2($y, $x);
|
149
|
|
150
|
$e = atan2(($alt2 - $alt1)/1000 - $d*$d/(2*$rt), $d);
|
151
|
|
152
|
return array($d, $cap*180/M_PI, $e*180/M_PI);
|
153
|
}
|
154
|
|
155
|
public function get_url($cap=false, $ele=false) {
|
156
|
$o = sprintf('panorama.php?dir=%s&panorama=%s',
|
157
|
PANORAMA_FOLDER, $this->get_name());
|
158
|
if ($cap && $ele) {
|
159
|
$o .= sprintf("&to_cap=%.3f&to_ele=%.3f", $cap, $ele);
|
160
|
}
|
161
|
return $o;
|
162
|
}
|
163
|
|
164
|
public function set_reference($ref_point, $x, $y) {
|
165
|
|
166
|
|
167
|
|
168
|
|
169
|
|
170
|
|
171
|
|
172
|
|
173
|
$p = $this->get_params();
|
174
|
|
175
|
if (!isset($this->params[self::$REF_KEY]) ||
|
176
|
!is_array($this->params[self::$REF_KEY])) {
|
177
|
$this->params[self::$REF_KEY] = array();
|
178
|
}
|
179
|
$ref_name = $ref_point->name;
|
180
|
$dict = $this->params[self::$REF_KEY];
|
181
|
|
182
|
$this->params[self::$REF_KEY][$ref_name] = array($x, $y);
|
183
|
}
|
184
|
|
185
|
public function unset_reference($ref_point) {
|
186
|
|
187
|
|
188
|
|
189
|
|
190
|
|
191
|
|
192
|
$p = $this->get_params();
|
193
|
$ref_name = $ref_point->name;
|
194
|
if (isset($p[self::$REF_KEY]) &&
|
195
|
isset($p[self::$REF_KEY][$ref_name])) {
|
196
|
unset($this->params[self::$REF_KEY][$ref_name]);
|
197
|
}
|
198
|
}
|
199
|
|
200
|
|
201
|
|
202
|
public static function get($name) {
|
203
|
|
204
|
|
205
|
$pano_dir = PANORAMA_PATH.'/'.$name;
|
206
|
return new site_point($pano_dir);
|
207
|
}
|
208
|
|
209
|
public static function create($filepath) {
|
210
|
|
211
|
|
212
|
$name = utils::strip_extension(basename($filepath));
|
213
|
$pano_dir = PANORAMA_PATH.'/'.$name;
|
214
|
$pano = new site_point($pano_dir);
|
215
|
if (!mkdir($pano->tiles_path())) {
|
216
|
return false;
|
217
|
} else {
|
218
|
return $pano;
|
219
|
}
|
220
|
}
|
221
|
|
222
|
public function to_geoJSON() {
|
223
|
$prm = $this->get_params();
|
224
|
$name = $this->get_name();
|
225
|
$lat = floatval($prm['latitude']);
|
226
|
$lon = floatval($prm['longitude']);
|
227
|
|
228
|
|
229
|
|
230
|
return array("type" => "Feature",
|
231
|
"geometry" => array(
|
232
|
"type" => "Point",
|
233
|
"coordinates" => array($lon, $lat)
|
234
|
),
|
235
|
"properties" => array("name" => $name,
|
236
|
"type" => 'pano_point',
|
237
|
"view_url" => $this->get_url())
|
238
|
);
|
239
|
}
|
240
|
|
241
|
|
242
|
public static function get_all($only_with_params=false) {
|
243
|
|
244
|
|
245
|
|
246
|
|
247
|
$panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
|
248
|
$pano_instances = array();
|
249
|
|
250
|
foreach ($panos as $pano_name) {
|
251
|
$pano = site_point::get($pano_name);
|
252
|
if (! $only_with_params || $pano->has_params() ) {
|
253
|
$pano_instances[] = $pano;
|
254
|
}
|
255
|
}
|
256
|
return $pano_instances;
|
257
|
}
|
258
|
|
259
|
}
|