1
|
<?php
|
2
|
require_once(dirname(__FILE__).'/../constants.inc.php');
|
3
|
require_once(dirname(__FILE__).'/utils.class.php');
|
4
|
require_once(dirname(__FILE__).'/GeoPoint.class.php');
|
5
|
require_once(dirname(__FILE__).'/Tile.class.php');
|
6
|
|
7
|
|
8
|
class PanoramaFormatException extends Exception {
|
9
|
|
10
|
|
11
|
}
|
12
|
|
13
|
class site_point extends GeoPoint {
|
14
|
|
15
|
|
16
|
private $base_dir;
|
17
|
private $name = false;
|
18
|
private $prefix = false;
|
19
|
private $params = false;
|
20
|
private $zooms;
|
21
|
|
22
|
public static $REF_KEY = 'reference';
|
23
|
|
24
|
public function __construct($dir) {
|
25
|
$this->base_dir = $dir;
|
26
|
$this->prefix = basename($dir);
|
27
|
}
|
28
|
|
29
|
public function params_path() {
|
30
|
return $this->base_dir.'/site.params';
|
31
|
}
|
32
|
|
33
|
|
34
|
|
35
|
|
36
|
|
37
|
|
38
|
|
39
|
|
40
|
|
41
|
|
42
|
private function look_for_params() {
|
43
|
if (is_file($this->params_path())) {
|
44
|
return $this->params_path();
|
45
|
}
|
46
|
$matches = glob($this->base_dir.'/*.params');
|
47
|
if ($matches and count($matches) > 0) {
|
48
|
return $matches[0];
|
49
|
} else {
|
50
|
return false;
|
51
|
}
|
52
|
}
|
53
|
|
54
|
public function tiles_url_prefix() {
|
55
|
return PANORAMA_FOLDER;
|
56
|
}
|
57
|
|
58
|
public function tiles_path() {
|
59
|
return $this->base_dir;
|
60
|
}
|
61
|
|
62
|
private function parse_and_cache_params() {
|
63
|
$params_path = $this->look_for_params();
|
64
|
|
65
|
if ($params_path) {
|
66
|
$params = parse_ini_file($params_path);
|
67
|
if ($params) {
|
68
|
$this->params = $params;
|
69
|
if (isset($params[self::$REF_KEY])) {
|
70
|
foreach ($params[self::$REF_KEY] as $ref => $vals) {
|
71
|
$bits = explode(',',$vals);
|
72
|
$this->params[self::$REF_KEY][$ref] = array(floatval($bits[0]),
|
73
|
floatval($bits[1]));
|
74
|
}
|
75
|
}
|
76
|
if (isset($params['image_loop'])) {
|
77
|
$this->params['image_loop'] = (bool)($params['image_loop']);
|
78
|
}
|
79
|
return $this->params;
|
80
|
}
|
81
|
}
|
82
|
return array();
|
83
|
}
|
84
|
|
85
|
public function get_params() {
|
86
|
|
87
|
if (isset($this->params) && $this->params) {
|
88
|
return $this->params;
|
89
|
} else {
|
90
|
return $this->parse_and_cache_params();
|
91
|
}
|
92
|
}
|
93
|
|
94
|
public function save_params() {
|
95
|
$o = '';
|
96
|
$p = $this->get_params();
|
97
|
foreach ($this->get_params() as $k => $v) {
|
98
|
if ($k == self::$REF_KEY) {
|
99
|
foreach ($v as $refk => $refv) {
|
100
|
$o.= sprintf("%s[\"%s\"] = %.5f,%.5f\n",
|
101
|
self::$REF_KEY, $refk,
|
102
|
$refv[0], $refv[1]);
|
103
|
}
|
104
|
} else {
|
105
|
$o.= "$k = ".utils::php2ini($v)."\n";
|
106
|
}
|
107
|
}
|
108
|
file_put_contents($this->params_path(), $o);
|
109
|
}
|
110
|
|
111
|
public function set_param($key, $value) {
|
112
|
$p = $this->get_params();
|
113
|
$this->params[$key] = $value;
|
114
|
if ($key == 'titre') {
|
115
|
$this->name = $value;
|
116
|
}
|
117
|
}
|
118
|
|
119
|
public function has_params(){
|
120
|
$p = $this->get_params();
|
121
|
return (isset($p['latitude'], $p['longitude'],
|
122
|
$p['altitude'], $p['titre']));
|
123
|
}
|
124
|
|
125
|
public function has_tiles(){
|
126
|
if (file_exists($this->tiles_path())) {
|
127
|
$pano_files = scandir($this->tiles_path());
|
128
|
foreach($pano_files as $filename) {
|
129
|
if (preg_match('/.*\.jpg/', $filename)) {
|
130
|
return true;
|
131
|
}
|
132
|
}
|
133
|
} else {
|
134
|
return false;
|
135
|
}
|
136
|
}
|
137
|
|
138
|
public function get_name() {
|
139
|
return basename($this->base_dir);
|
140
|
}
|
141
|
|
142
|
public function get_prefix() {
|
143
|
return $this->prefix;
|
144
|
}
|
145
|
|
146
|
public function get_lat() {
|
147
|
$p = $this->get_params();
|
148
|
return $p['latitude'];
|
149
|
}
|
150
|
|
151
|
public function get_lon() {
|
152
|
$p = $this->get_params();
|
153
|
return $p['longitude'];
|
154
|
}
|
155
|
|
156
|
public function get_magnifications() {
|
157
|
|
158
|
$zoom_array = array();
|
159
|
|
160
|
|
161
|
$stop = false;
|
162
|
$zoom_level = 0;
|
163
|
|
164
|
while (! $stop) {
|
165
|
$files = glob(sprintf('%s/%03d_*.jpg', $this->base_dir, $zoom_level));
|
166
|
sort($files);
|
167
|
$last_file = end($files);
|
168
|
if ($last_file) {
|
169
|
$last_tile = Tile::from_file($last_file, $this);
|
170
|
$zoom_array[$zoom_level] = array('nx' => $last_tile->x + 1,
|
171
|
'ny' => $last_tile->y + 1);
|
172
|
$zoom_level++;
|
173
|
} else {
|
174
|
$stop = true;
|
175
|
}
|
176
|
}
|
177
|
$this->zooms = $zoom_array;
|
178
|
return $this->zooms;
|
179
|
}
|
180
|
|
181
|
public function coordsToCap($lat, $lon, $alt) {
|
182
|
$params = $this->get_params();
|
183
|
if (!isset($params['latitude']) || !isset($params['longitude'])) return false;
|
184
|
$rt = 6371;
|
185
|
$alt1 = isset($params['altitude']) ? $params['altitude'] : $alt;
|
186
|
$lat1 = $params['latitude']*M_PI/180;
|
187
|
$lon1 = $params['longitude']*M_PI/180;
|
188
|
$alt2 = $alt;
|
189
|
$lat2 = $lat * M_PI/180;
|
190
|
$lon2 = $lon * M_PI/180;
|
191
|
|
192
|
$dLat = $lat2-$lat1;
|
193
|
$dLon = $lon2-$lon1;
|
194
|
|
195
|
$a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);
|
196
|
$angle = 2 * atan2(sqrt($a), sqrt(1-$a));
|
197
|
$d = $angle * $rt;
|
198
|
|
199
|
$y = sin($dLon)*cos($lat2);
|
200
|
$x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($dLon);
|
201
|
$cap = atan2($y, $x);
|
202
|
|
203
|
$e = atan2(($alt2 - $alt1)/1000 - $d*$d/(2*$rt), $d);
|
204
|
|
205
|
return array($d, $cap*180/M_PI, $e*180/M_PI);
|
206
|
}
|
207
|
|
208
|
public function get_generate_url($source_file) {
|
209
|
|
210
|
|
211
|
|
212
|
return sprintf('genererPano.php?wizard=1&name='.$source_file);
|
213
|
}
|
214
|
|
215
|
public function src_path(){
|
216
|
|
217
|
|
218
|
$extensions = array('jpg', 'tif', 'png', 'bmp', 'jpeg', 'pnm',
|
219
|
'JPG', 'TIF', 'PNG', 'BMP', 'JPEG', 'PNM');
|
220
|
|
221
|
foreach ($extensions as $ext) {
|
222
|
$tried_name = sprintf('%s/%s.%s', UPLOAD_PATH, $this->get_name(),$ext);
|
223
|
if (file_exists($tried_name)) {
|
224
|
return $tried_name;
|
225
|
}
|
226
|
}
|
227
|
return false;
|
228
|
}
|
229
|
|
230
|
public function get_url($cap=false, $ele=false) {
|
231
|
$o = sprintf('panorama.php?dir=%s&panorama=%s',
|
232
|
PANORAMA_FOLDER, $this->get_name());
|
233
|
if ($cap && $ele) {
|
234
|
$o .= sprintf("&to_cap=%.3f&to_ele=%.3f", $cap, $ele);
|
235
|
}
|
236
|
return $o;
|
237
|
}
|
238
|
|
239
|
public function get_map_url($cap=0) {
|
240
|
$encoded_title = base64_encode($this->get_name());
|
241
|
$script_name = 'show_capline.php';
|
242
|
$lat = $this->get_params()['latitude'];
|
243
|
$lon = $this->get_params()['longitude'];
|
244
|
|
245
|
$o = sprintf('%s?title=%s&cap=%s&org_lat=%.5f&org_lon=%.5f&dist=120000',
|
246
|
$script_name, $encoded_title, $cap, $lat, $lon);
|
247
|
return $o;
|
248
|
}
|
249
|
|
250
|
public function set_reference($ref_point, $x, $y) {
|
251
|
|
252
|
|
253
|
|
254
|
|
255
|
|
256
|
|
257
|
|
258
|
|
259
|
$p = $this->get_params();
|
260
|
|
261
|
if (!isset($this->params[self::$REF_KEY]) ||
|
262
|
!is_array($this->params[self::$REF_KEY])) {
|
263
|
$this->params[self::$REF_KEY] = array();
|
264
|
}
|
265
|
$ref_name = $ref_point->name;
|
266
|
$dict = $this->params[self::$REF_KEY];
|
267
|
|
268
|
$this->params[self::$REF_KEY][$ref_name] = array($x, $y);
|
269
|
}
|
270
|
|
271
|
public function unset_reference($ref_point) {
|
272
|
|
273
|
|
274
|
|
275
|
|
276
|
|
277
|
|
278
|
$p = $this->get_params();
|
279
|
$ref_name = $ref_point->name;
|
280
|
if (isset($p[self::$REF_KEY]) &&
|
281
|
isset($p[self::$REF_KEY][$ref_name])) {
|
282
|
unset($this->params[self::$REF_KEY][$ref_name]);
|
283
|
}
|
284
|
}
|
285
|
|
286
|
|
287
|
|
288
|
public static function get($name) {
|
289
|
|
290
|
|
291
|
$pano_dir = PANORAMA_PATH.'/'.$name;
|
292
|
return new site_point($pano_dir);
|
293
|
}
|
294
|
|
295
|
public static function create($filepath) {
|
296
|
|
297
|
|
298
|
$name = utils::strip_extension(basename($filepath));
|
299
|
$pano_dir = PANORAMA_PATH.'/'.$name;
|
300
|
$pano = new site_point($pano_dir);
|
301
|
if (!mkdir($pano->tiles_path())) {
|
302
|
return false;
|
303
|
} else {
|
304
|
return $pano;
|
305
|
}
|
306
|
}
|
307
|
|
308
|
public function to_geoJSON() {
|
309
|
$prm = $this->get_params();
|
310
|
$name = $this->get_name();
|
311
|
$lat = floatval($prm['latitude']);
|
312
|
$lon = floatval($prm['longitude']);
|
313
|
|
314
|
|
315
|
|
316
|
return array("type" => "Feature",
|
317
|
"geometry" => array(
|
318
|
"type" => "Point",
|
319
|
"coordinates" => array($lon, $lat)
|
320
|
),
|
321
|
"properties" => array("name" => $name,
|
322
|
"type" => 'pano_point',
|
323
|
"view_url" => $this->get_url())
|
324
|
);
|
325
|
}
|
326
|
|
327
|
|
328
|
public static function get_all($only_with_params=true) {
|
329
|
|
330
|
|
331
|
|
332
|
|
333
|
$panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
|
334
|
$pano_instances = array();
|
335
|
|
336
|
foreach ($panos as $pano_name) {
|
337
|
$pano = site_point::get($pano_name);
|
338
|
if (! $only_with_params || $pano->has_params() ) {
|
339
|
$pano_instances[] = $pano;
|
340
|
}
|
341
|
}
|
342
|
return $pano_instances;
|
343
|
}
|
344
|
|
345
|
}
|