-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprojection.php
101 lines (92 loc) · 2.14 KB
/
projection.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
<?php
#-------------------------------------------------------
# Translates between lat/long and the slippy-map tile
# numbering scheme.
#
# http://wiki.openstreetmap.org/index.php/Slippy_map_tilenames
#
# Written by Oliver White, 2008
# This file is public-domain
#-------------------------------------------------------
function degrees($rad){return($rad * 57.2957795);}
function radians($deg){return($deg / 57.2957795);}
function numTiles($z)
{
return(pow(2.0,$z));
}
function tileValid($x,$y,$z)
{
if($x < 0 or $y < 0 or $z < 0)
return(0);
$n = numTiles($z);
if($x >= $n or $y >= $n)
return(0);
return(1);
}
function sec($x)
{
return(1.0/cos($x));
}
function relativeTileEdges($x,$y,$z)
{
$n = numTiles($z);
$portion = 1.0 / $n;
$x1 = $x * $portion;
$y1 = $y * $portion;
return(array($x1,$y1,$x1+$portion,$y1+$portion, $portion, $portion));
}
function latlon2relativeXY($lat,$lon)
{
$x = ($lon + 180.0) / 360.0;
$y = (1.0 - log(tan(radians($lat)) + sec(radians($lat))) / M_PI) / 2.0;
return(array($x,$y));
}
function latlon2xy($lat,$lon,$z)
{
$n = numTiles($z);
list($x,$y) = latlon2relativeXY($lat,$lon);
return(array($n*$x, $n*$y));
}
function tileXY($lat, $lon, $z)
{
list($x,$y) = latlon2xy($lat,$lon,$z);
return(array(floor($x),floor($y)));
}
function xy2latlon($x,$y,$z)
{
$n = numTiles($z);
$relY = $y / $n;
$lat = mercatorToLat(M_PI * (1.0 - 2.0 * $relY));
$lon = -180.0 + 360.0 * $x / $n;
return(array($lat,$lon));
}
function mercatorToLat($mercatorY)
{
$val = degrees(atan(sinh($mercatorY)));
return($val);
}
function latEdges($y,$z)
{
$n = numTiles($z);
$unit = 1.0 / $n;
$relY1 = $y * $unit;
$relY2 = $relY1 + $unit;
$lat1 = mercatorToLat(M_PI * (1.0 - 2.0 * $relY1));
$lat2 = mercatorToLat(M_PI * (1.0 - 2.0 * $relY2));
return(array($lat1,$lat2));
}
function lonEdges($x,$z)
{
$n = numTiles($z);
$unit = 360.0 / $n;
$lon1 = -180.0 + $x * $unit;
$lon2 = $lon1 + $unit;
return(array($lon1,$lon2));
}
function tileEdges($x,$y,$z)
{
list($lat1,$lat2) = latEdges($y,$z);
list($lon1,$lon2) = lonEdges($x,$z);
return(array($lat2, $lon1, $lat1, $lon2)); # S,W,N,E
}
?>