forked from nickn17/evDashVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobal.php
108 lines (96 loc) · 2.43 KB
/
global.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* Get number parameter (prevents SQL injection)
*/
function getNum($key, $default = 0) {
if (!isset($_GET[$key]))
return ($default);
$ret = str_replace(",", ".", trim($_GET[$key]));
if (!is_numeric($ret))
return ($default);
return $ret;
}
/**
* Get string parameter (prevents SQL injection)
*/
function getStr($key, $default = "") {
global $database;
if (!isset($_GET[$key]))
return $default;
$ret = $_GET[$key];
// if (get_magic_quotes_gpc())
// $ret = stripslashes($ret); //json_encode($ret, JSON_HEX_APOS)), true);
// $ret = $database->encodeValue(addslashes($ret));
return trim($ret);
}
/**
* formatHourMin
*/
function formatHourMin($timeSec) {
$mask = "%2s%1s%2s%1s" ;
return sprintf($mask,
str_pad(intval($timeSec / 3600),2,"0",STR_PAD_LEFT),
"h",
str_pad(intval(($timeSec % 3600) / 60),2,"0",STR_PAD_LEFT),
"m"
);
}
/**
* lonToTile
*/
function lonToTile($long, $zoom) {
return (($long + 180) / 360) * pow(2, $zoom);
}
/**
* latToTile
*/
function latToTile($lat, $zoom) {
return (1 - log(tan($lat * pi() / 180) + 1 / cos($lat * pi() / 180)) / pi()) / 2 * pow(2, $zoom);
}
/**
* lonPerPixel
*/
function lonPerPixel($tileNo, $zoom) {
$a1 = ($tileNo / pow(2, $zoom) * 360 - 180);
$a2 = (($tileNo + 1) / pow(2, $zoom) * 360 - 180);
return abs($a2 - $a1) / 512;
}
/**
* latPerPixel
*/
function latPerPixel($tileNo, $zoom) {
$n = pi() * (1 - 2 * $tileNo / pow(2, $zoom));
$a1 = rad2deg(atan(sinh($n)));
$n = pi() * (1 - 2 * ($tileNo + 1) / pow(2, $zoom));
$a2 = rad2deg(atan(sinh($n)));
return abs($a2 - $a1) / 512;
}
/**
* fetch title
*/
function fetchTile($url) {
$cacheTileName = "cache/tile_" . md5($url) . ".jpg";
if (file_exists($cacheTileName) && filesize($cacheTileName) > 100) {
return file_get_contents($cacheTileName);
}
global $abc;
$abc++;
if ($abc >= 3) {
$abc = 0;
$url = str_replace("/a.", "/" . chr(97 + $abc), $url);
}
usleep(500000); //0.5s
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "TileProxy/1.0");
curl_setopt($ch, CURLOPT_URL, $url);
// Used to ensure that the corect URL are used
// if (PHP_SAPI == 'cli') {
// echo "$url\n";
// }
$tile = curl_exec($ch);
curl_close($ch);
file_put_contents($cacheTileName, $tile);
//die($url . "s");
return $tile;
}