[go: up one dir, main page]

Menu

[r4]: / omgm / gm.php  Maximize  Restore  History

Download this file

179 lines (136 with data), 4.8 kB

  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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
function Google_Tile_Factors($zoom, $tileSize = 256){
if ( !isset($zoom) || $zoom < 0 ) {
$zoom = 0;
}
$value = array();
$value['zoom'] = $zoom ;
$value['PI'] = pi(); //3.1415926536 ;
$value['bc'] = 2 * $value['PI'] ;
$value['Wa'] = $value['PI'] / 180 ;
$value['cp'] = pow(2, ($value['zoom'] + 8) );
$value['max'] = pow(2, $value['zoom']) - 1 ; # Maximum Tile Number
$value['pixLngDeg']= $value['cp'] / 360;
$value['pixLngRad']= $value['cp'] / $value['bc'] ;
$value['bmO'] = $value['cp'] / 2 ;
$value['tileSize'] = $tileSize ;
return $value ;
}
function Google_Tile_Calc($value, $ty, $tx){
$result = array();
$result["NAMEX"] = $tx;
$result["NAMEY"] = $ty;
list( $result["PYN"], $result['PXW']) = Google_Tile_to_Pix( $value, $ty, $tx ) ;
// Convert Pixels to Coordinates (Upper Left Corner)...
list($result["LATN"],$result["LNGW"]) = PixtoCoordinate( $value, $result["PYN"], $result["PXW"] ) ;
$result["PYS"] = $result["PYN"] + 255 ;
$result["PXE"] = $result["PXW"] + 255 ;
// Convert Pixels to Coordinates (Lower Right Corner)...
list($result["LATS"],$result["LNGE"]) = PixtoCoordinate( $value, $result["PYS"], $result["PXE"] ) ;
return $result;
}
function Google_Tile_to_Pix($value, $y, $x){
return array( sprintf("%0.0f", $y * $value["tileSize"]), sprintf("%0.0f", $x * $value["tileSize"]) ) ;
}
function PixtoCoordinate($value, $y ,$x){
$e = (($y - $value["bmO"]) / $value["pixLngRad"]) * (-1) ;
$d = array();
$d[1] = sprintf("%0.6f",($x - $value["bmO"]) / $value["pixLngDeg"]) ;
$d[0] = sprintf("%0.6f", (2 * atan(exp($e)) - $value["PI"] / 2) / $value["Wa"]) ;
return $d;
}
function Google_RelPix($lat, $lng, $zoom){
$PI = pi(); //3.1415926536 ;
$bc = 2 * $PI;
$Wa = $PI / 180;
$cp = pow(2, ($zoom + 8) );
$pixLngDeg = $cp / 360;
$pixLngRad = $cp / $bc ;
$bmO = $cp / 2 ;
$d = array(2);
$d[1] = sprintf("%0.0f", $bmO + $lng * $pixLngDeg ) ;
$e = sin($lat * $Wa) ;
if( $e > 0.99999 ){
$e = 0.99999 ;
}
if( $e < -0.99999 ){
$e = -0.99999 ;
}
$d[0] = sprintf("%0.0f", $bmO + 0.5 * log((1 + $e) / (1 - $e)) * (-1) * $pixLngRad ) ;
// 0:x 1:y
return $d;
}
function PixtoTileName($value, $y, $x, $yd, $xd, $parwho){
$yn = floor($y / $value["tileSize"]);
$xn = floor($x / $value["tileSize"]);
if( strcmp( $parwho, "Partial") != 0 ){
if( strcmp( $yd, "N") == 0 ){
$yn++;
}
else{
$yn--;
}
if( strcmp( $xd, "W" ) == 0 ){
$xn++;
}
else{
$xn--;
}
}
if( $yn > $value["max"] ){
$yn = $value["max"];
}
elseif( $yn < 0 ){
$yn = 0;
}
if( $xn > $value["max"] ){
$xn = $value["max"];
}
elseif( $xn < 0 ){
$xn = 0;
}
return array($yn, $xn);
}
/*
# Call as: <array of Hashes> = Google_Tiles(<LatitudeS>, <LongitudeW>, <LatitudeN>, <LongitudeE>, <Zoom>, [<option: tileSize>], [<option:
Partial/Whole>]) ;
# Partial/Whole option: (Default: Partial)
# Partial: Include the edge to create partial tiles
# Whole: Include only tiles that are contained by the bounds
#
# Returned Array Specifications:
# Each element is a reference to a Hash:
# NAMEY - Tile Name y
# NAMEX - Tile Name x
# PYS - Pixel South
# PXW - Pixel West
# PYN - Pixel North
# PXE - Pixel East
# LATS - South Latitude
# LNGW - West Longitude
# LATN - North Latitude
# LNGE - East Longitude
#
# Note: X is width, Y is height...
*/
function Google_Tiles($latS, $lngW, $latN, $lngE, $zoom, $tileSize = 256, $parwho = "Partial"){
$value = Google_Tile_Factors($zoom, $tileSize);
$ret = array();
$first = array();
$last = array();
list( $first["NORTH"], $first["WEST"] ) = Google_RelPix($latN, $lngW, $zoom);
list( $first["NAMEY"], $first["NAMEX"] ) = PixtoTileName( $value, $first["NORTH"], $first["WEST"], 'N', 'W', $parwho );
list( $last["SOUTH"], $last["EAST"] ) = Google_RelPix($latS, $lngE, $zoom ) ;
list( $last["NAMEY"], $last["NAMEX"] ) = PixtoTileName( $value, $last["SOUTH"], $last["EAST"], 'S', 'E', $parwho ) ;
for ( $ty = $first["NAMEY"] ; $ty <= $last["NAMEY"] ; $ty++ ){
for ( $tx = $first["NAMEX"] ; $tx <= $last["NAMEX"] ; $tx++ ){
$ret[] = Google_Tile_Calc( $value, $ty, $tx );
}
}
$ret[0]["NORTH"] = $first["NORTH"] ;
$ret[0]["WEST"] = $first["WEST"] ;
$ret[ count($ret) - 1 ]["SOUTH"] = $last["SOUTH"] ;
$ret[ count($ret) - 1 ]["EAST"] = $last["EAST"] ;
return $ret;
}
?>